CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2020
    Posts
    46

    Too memory used by the stack

    Hi everyone
    I use VS Community 17.8.3 on a Hp Pc i7-9700 32Gb Ram W10pro 64

    compiling the attached code I received a warning with code C6262:
    "The function uses '4321508' bytes of stack. It is recommended to move some data to the heap."
    The program crashed so I set the value you see in the image and now the program runs but it takes more than an hour to analyze 21000KB of text files

    Does anyone have any suggestions for further settings or code modifications to optimize everything ?

    Thank you
    Attached Images Attached Images
    Attached Files Attached Files

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,903

    Re: Too memory used by the stack

    The sizes of the used arrays are large. That's why it's exceeding the stack limit as these are allocated on the stack. Probably the easiest way to allocate these off the stack is to make them global rather than local to main(). Move the lines starting with const int nGroup and ending with int r5data to above int main(). These variables won't then be allocated on the stack and you shouldn't then need to change the stack dimensions.

    Try putting a cout statement before while (ifs >> buf[0]) to display a message and see how long it takes to show the message and then how long to finish the program. This could give some indication as to which part of the program is taking the long time to complete.
    Last edited by 2kaud; January 7th, 2024 at 11:31 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    Feb 2020
    Posts
    46

    Re: Too memory used by the stack

    Hi 2kaud

    thanks for your suggestion, I'll try to modify the code as you told me and I'll update you,

    Thanks again

  4. #4
    Join Date
    Apr 2024
    Posts
    6

    Re: Too memory used by the stack

    You can also allocate your arrays on the heap. Create pointers in your stack (the automatic area in your braces) and then call new to allocate space. Don't forget to check for NULL on the return from new and also to delete (delete or delete[]) the array when you are done.

    You can check for memory leaks by adding the following to your program at the beginning...

    Code:
    	// Setup to check for memory leaks. (Debug only.)
    	#ifdef _DEBUG
    	_CrtMemState sOld;
    	_CrtMemState sNew;
    	_CrtMemState sDiff;
    	_CrtMemCheckpoint(&sOld); //take a snapshot
    	#endif // _DEBUG
    and the following code at the end of your program...

    Code:
    	// Check for memory leaks. (Debug only.)
    	#ifdef _DEBUG
    	_CrtMemCheckpoint(&sNew); //take a snapshot 
    	if (_CrtMemDifference(&sDiff, &sOld, &sNew)) // if there is a difference
    	{
    		MessageBeep(MB_ICONEXCLAMATION);
    		MessageBox(NULL, _T("MEMORY LEAK(S) DETECTED!\n\nSee debug log."), szTitle, MB_OK | MB_ICONEXCLAMATION);
    		OutputDebugString(L"-----------_CrtMemDumpStatistics ---------\n");
    		_CrtMemDumpStatistics(&sDiff);
    		OutputDebugString(L"-----------_CrtMemDumpAllObjectsSince ---------\n");
    		_CrtMemDumpAllObjectsSince(&sOld);
    		OutputDebugString(L"-----------_CrtDumpMemoryLeaks ---------\n");
    		_CrtDumpMemoryLeaks();
    	}
    	#endif // _DEBUG
    Then you will get a message at the end if there are any leaks, and you can check the debug output in VS to see details.
    Good luck.

  5. #5
    Join Date
    Apr 2024
    Posts
    6

    Re: Too memory used by the stack

    p.s. VS 2022 Community Edition is now at version 17.9.6. You might want to consider updating.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,903

    Re: Too memory used by the stack

    Don't forget to check for NULL on the return from new
    By default new raises an exception if an allocation issue arises. To have new return nullptr (nullptr in C++ not NULL) you need to indicate this by using std::nothrow. eg

    Code:
    auto p {new (std::nothrow) int};
    
    if (p == nullptr) {
        // deal with new issue here
    }
    Better yet, use std::unique_ptr which is RAII so allocated memory will automatically be deleted when it goes out of scope.
    https://en.cppreference.com/w/cpp/memory/unique_ptr
    https://en.cppreference.com/w/cpp/me...tr/make_unique
    Last edited by 2kaud; April 14th, 2024 at 04:31 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Apr 2024
    Posts
    6

    Re: Too memory used by the stack

    Instead of declaring and and allocating variable on the heap, using new and delete, you can declare them global, at file scope. You can also declare them static. If you do this to local function variables (normally allocated on the stack) they have file persistance.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured