Closed Thread
Results 1 to 7 of 7

Thread: Measuring amount of bytes of stack space needed for code?

  1. #1
    blernblan is offline Newbie
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Question Measuring amount of bytes of stack space needed for code?

    Hey guys, I'm trying to count the amount of bytes of stack space that the following chunk of code will need. All local variables and temp results have their own location on the stack and their space is allocated at the beginning of the program and will not be reclaimed until the program exits:

    Code:
    {
       int a;
       int b;
       int c;
       int x;
       a = 5;
       b = a + 3;
       c = a * b / a;
       x = c - (a + a) / (b - c);
    }
    I know this isn't a complete program, but I'm really curious how to measure the amount of bytes in stack space that this chunk of code will need. Any help is greatly appreciated.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Join Date
    Apr 2009
    Location
    Trapped in my own little world.
    Posts
    2,487
    Rep Power
    33

    Re: Measuring amount of bytes of stack space needed for code?

    Each int I believe is 32 bytes so 32*4=128 bytes?

  4. #3
    blernblan is offline Newbie
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Re: Measuring amount of bytes of stack space needed for code?

    Thank you very much for the answer.

    So just to clarify: the assignment statements do not require extra bytes on the stack? Nor the brackets? So in this chunk of code, only variable declarations require stack space?

    Thanks again for the answer.

  5. #4
    blernblan is offline Newbie
    Join Date
    Apr 2009
    Posts
    9
    Rep Power
    0

    Re: Measuring amount of bytes of stack space needed for code?

    Also I'm assuming all local variables and temporary results will be given their own location on the stack and all locals and temporaries will have their space allocated at the beginning of the program and this space will not be reclaimed until the program exits.

    Not sure if that changes anything...

  6. #5
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Measuring amount of bytes of stack space needed for code?

    It depends on how many bytes an int is on your compiler, how the memory is arranged, etc, etc, etc. There isn't a simple answer to your question other than "It's implementation defined".
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  7. #6
    PythonPower's Avatar
    PythonPower is offline Programming Professional
    Join Date
    Feb 2009
    Posts
    228
    Rep Power
    13

    Re: Measuring amount of bytes of stack space needed for code?

    WingedPanther has it exactly.

    Different compilers do it differently. GCC for instance may try to optimize some variables by keeping a value in a CPU register instead of on the stack.

    You can make rough guesses though but you'll never know unless you check out the assembly code (use the -S flag in GCC). In that code above, I would expect it to be removed at compile time unless you did something with one of the variables; if you did, then the values would likely be precomputed.

  8. #7
    Join Date
    Oct 2007
    Location
    /dev/null
    Posts
    4,513
    Blog Entries
    8
    Rep Power
    59

    Re: Measuring amount of bytes of stack space needed for code?

    Clarification: An int is typically 32 bits NOT 32 bytes. That would be absolutely massive. You also have to keep in mind that variables are aligned for hardware compatibility, so 4-byte variables are aligned on addresses that are multiples of 4 bytes, 2-byte variables on 2-byte boundaries, etc. So if you have this:

    Code:
    char a;
    int b;
    the answer is eight--yes, eight--bytes. Only variables (and two other things you don't need to worry about at this point) take up stack space; the code is stored in the code section of the program, which is separate from the stack.
    sudo rm -rf /

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Stack allocation and stack size
    By mircan in forum C and C++
    Replies: 3
    Last Post: 03-17-2010, 06:53 PM
  2. Help needed with code.
    By nikifi in forum PHP Development
    Replies: 1
    Last Post: 01-06-2010, 10:21 AM
  3. Measuring Amount of Web Traffic
    By uberlax in forum C# Programming
    Replies: 3
    Last Post: 08-31-2009, 12:11 AM
  4. Code Needed
    By AfTriX in forum General Programming
    Replies: 0
    Last Post: 01-25-2007, 08:47 AM
  5. Replace any amount of space
    By Frantic in forum C# Programming
    Replies: 4
    Last Post: 08-16-2006, 07:23 PM

Tags for this Thread

Bookmarks

Posting Permissions

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