stack and heap size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anuvanand1
    New Member
    • Oct 2007
    • 22

    stack and heap size

    I want to calculate the maximum stack and heap size required by my C program?

    weather it is possible ? if so how can i find it?
  • arnaudk
    Contributor
    • Sep 2007
    • 425

    #2
    Just account for all your variables and memory allocations. The sizeof() operator can come in handy here. You can also profile the memory usage of your program in real time by using tools such as valgrind.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      There is nothing in the C Standard about how to find out stack and heap depth. Although stack and heap are ubiquituous, I'm not even sure the C Standard mandates their existence.

      You have to look for special-purpose tools. A good place to start is your compiler vendor.

      Comment

      • arnaudk
        Contributor
        • Sep 2007
        • 425

        #4
        Originally posted by donbock
        There is nothing in the C Standard about how to find out stack and heap depth. Although stack and heap are ubiquituous, I'm not even sure the C Standard mandates their existence.

        You have to look for special-purpose tools. A good place to start is your compiler vendor.
        On a 32bit machine the following program uses a stack of 8 bytes and a heap of 2 bytes:
        [code=c]
        int main()
        {
        int a = 5; // 4 bytes for int
        char * b = new char[2]; // 4 bytes for pointer, 2 bytes for array
        delete [] b;
        return 0;
        }
        [/code]Now, how much system memory a program actually uses is a different matter. Additionally, libraries may certainly use stack and heap space which can be hard to account for, that's why I suggest using one of the many memory profilers out there like valgrind.

        Comment

        Working...