How do I determine how much stack and heap space my C++ program compiled with vs2012 uses?

I think I may have a heap of stack overflow problem in my code: is there a way to determine the maximum amount of stack space each thread in my code uses?

My code is native MFC C++ code (not .net code) compiled with Visual Studio 2012.


Christopher ScheneSystem Engineer/Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Teh stack size is determined when 'CreateThread()' is called, when you provide '0' as the stack size argument, the default is used, see ("Thread Stack Size").

If you are thinking about the current stack usage, that'd only be able by using some odd tricks like reading ESP and comparing the address of your 1st local variable to it...

To determine the cause for that, I'd rather run your code under the debugger and set 'EXCEPTION_STACK_OVERFLOW' to 'Break when thrown' in "Debug|Exceptions...".
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I don't see a "createThread" call any place explicitly in my code.

Is that possibly something that is automatically done by the MFC framework?
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I did find a create thread, but my initial question is really the one I need an answer to: How much stack I am using. I really don't want to do it by trial and error and wait until the stack overflows as I am really curious as to how close to "full" I am approaching.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
Hello: Any ideas as to how I can determine stack size.
Hey Chris,

I don't have any great insight for you other than to say I tried to find a profiling tool that could report this for me in a Java application and eventually had to give up the search.

I do think profilers are your best bet as this would need to be determined dynamically at runtime as you exercised the app.  There may be better tools in the C++ space - but when I looked for this exact thing in Java land I couldn't find it.

My approach ultimately was to just run manual experiments - setting the max thread size to <n>, run the app, see if it looked stable, if so reduce by 25% and repeat.  Once I found the rough limit this way, I then raised it back up a couple of notches to give myself a decent cushion.

FYI - if you take this approach you will also want to make sure there's no recursive code in the codebase or you may get burned - since stack size there will be proportional to problem size and may suddenly blow up.

Sorry that's not a more helpful answer,

I looked it up and here's what I found (but never did this in Windows). Hope it helps.
VCLinkerTool::StackCommitSize Property
Gets or sets a value indicating the total stack allocation size in physical memory.
property int StackCommitSize {
      int get ();
      void set (int value);
StackCommitSize exposes the functionality of the /STACK (Stack Allocations) linker option.

To change your stack size, goto Project Properties -> Linker -> System -> Stack Reserve Size (and enter your value). (I entered 4000000.)
There is also a Heap Reserve Size.
Hit Apply. Then goto Command Line, and you should see this:
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I am going to look around the web and see if I can find something to tell me the stack and heap consumption. I appreciate the suggestions, I need the exact numbers so that as I add features to my code I can see how it grows.

I think I have an approach that I think will work though it is a bit clunky.
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
This is the approach I was thinking of to determine stack usage
1) Add a MACRO that is enabled by a #define SOMEMACRO after local variables are in a method
2) This macro will create variable that is theoretically lower on the stack than the other variables I am of course assuming the pushes down in memory.

Since I have all the stack values I can calculate how far down the stack I currently am.

I cannot think of anyway to do this without instrumenting.
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I am coding up an example to determine how much stack I have used. One of the problems I ran into is that there are multiple threads running and thus I have to keep track of the stack usage on a per thread basis.
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
Here is some code I wrote to determine stack depth.

While I cannot tell the absolute depth, I can tell how much the stack grows after the first time I check it.

here is what the string looks like:

thread 17056, stack depth = 50,

The code captures relative stack depth for each thread in the process
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I plan to give myself credit for this since I actually wrote the code to check stack depth.

I am open to suggestions ior improvement of this code.

Could you add StackChecker.h to your solution?
Also, if you use the Code button to insert your code, it makes it simpler to review.

Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
Ok, here are both files
// StackChecker.cpp : implementation file

#include "stdafx.h"
#include "StackChecker.h"

// StackChecker


    this->id = STACK_CHECKER_ID;
    currentThreadCount = 0;
    for(int i = 0; i <  MAX_THREADS_FOR_STACK_CALCULATION; ++i)
        pStartStackForThread[i] = 0;
        pLowestStackForThread[i] = 0;
        maxStackUsageForThread[i] = 0;




// StackChecker message handlers
boolean StackChecker::ck_id() 
    return true;

CString StackChecker::calculateAndRecordStackUsageForThisThread(long *currentPointer) 
    CString result="";
    DWORD threadID = GetCurrentThreadId();
    result = calculateAndRecordStackUsageForThread(threadID,  currentPointer);
    return result;

CString StackChecker::calculateAndRecordStackUsageForThread(DWORD threadId,long *currentPointer) 
    CString result="";

    //Find the index for this threadID and if it doe snot exist, then craete a new entry
    int thisThreadIndex = getIndexForThisThead(threadId);
    if(pStartStackForThread[thisThreadIndex] == 0) 
        //this is the first time we have recorded stack information for this thraed
        pLowestStackForThread[thisThreadIndex] = pStartStackForThread[thisThreadIndex]  = currentPointer;
    if(currentPointer <= pLowestStackForThread[thisThreadIndex])
        pLowestStackForThread[thisThreadIndex] = currentPointer;
    long stackDepth = pStartStackForThread[thisThreadIndex] - pLowestStackForThread[thisThreadIndex];
    if (stackDepth > maxStackUsageForThread[thisThreadIndex]) 
        maxStackUsageForThread[thisThreadIndex] = stackDepth;
    result.Format("thread %d, stack depth = %ld", threadId, maxStackUsageForThread[thisThreadIndex]);
    return result;

int StackChecker::getIndexForThisThead(DWORD id)
    int result = -1;
    if (this->currentThreadCount > 0) {
        for(int i = 0; i <  currentThreadCount; ++i) 
            if(this->ThreadID[i] == id) {
                result = i;
        if(result == -1)
            //if we get here we did not find an entry for this thread, so add one.

            this->ThreadID[currentThreadCount++] = id;
            result = currentThreadCount - 1;
        } else 
        //just allocate a slot to this thread
        this->ThreadID[currentThreadCount++] = id;
        result = 0;
        return result;

Open in new window

#pragma once

// StackChecker


class StackChecker : public CWnd

        virtual ~StackChecker();
        CString calculateAndRecordStackUsageForThisThread(long *currentPointer);
         CString calculateAndRecordStackUsageForThread(DWORD threadId,long *currentPointer);
        CString getThisThreadStackUsage(long *currentPointer);
        CString getThisThreadStackUsage(DWORD threadId,long *currentPointer);
        int getIndexForThisThead(DWORD threadID);

        boolean ck_id();
        long id;
#define STACK_CHECKER_ID   818181
        long  *pStartStackForThread[MAX_THREADS_FOR_STACK_CALCULATION];
        long  *pLowestStackForThread[MAX_THREADS_FOR_STACK_CALCULATION];
        long  maxStackUsageForThread[MAX_THREADS_FOR_STACK_CALCULATION];
        int currentThreadCount;


Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
My solution was not especially pretty code but I was looking for functionality as I need to get some idea as to how much stack I am using: It turns out only about 10,000 bytes based on the output of my stack checker
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I realized that since this code processes multiple threads it requires  a critical section. I will update the code with a critical section.
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I provided the solution myself and the code works.
Thanks for sharing. :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.