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 ?
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!
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.
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
}
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!
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.