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

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    10

    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. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    a) it depends on your compiler as to whether any (or all) of your local variables end up in registers.
    b) it depends on your compiler as to whether it provides you with some information to determine stack usage.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    It depends on the compiler you are using since different compilers may optimize it differently.
    Look at the assembly generated by the compiler to figure out the size of the stack frame for that function.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Also, there may not even be a "stack" at all.

    i.e the standard only defines "automatic storage", "static storage" and "allocated storage".

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Well 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.

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Your assumptions are only valid for the systems and compilers that conform to them. Therefore, you may or may not get very far with your tests, due to the need to understand the implementation, which has very little to do with C itself.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by blernblan View Post
    Well 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.
    Why would you assume that? That seems almost certainly to never be true!

    That the code is reasonably likely to take zero bytes of stack space. In fact it probably even uses zero registers, and zero machine instructions as well. Welcome to optimisation!

    Seriously, there is no useful number one can provide as an answer to your question. It's like asking how fast does a car go and expecting an exact number, and telling us to assume that cars can fly.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I suspect this is a school exercise in "understanding local stack storage". If that is the case, you have to show us that you are trying, not just post the exercise text.

    If you are actually thinking that we can answer this for a non-academic exercise, then you are wrong. As has been stated, it depends on the compiler implementation, it may have an arbitrary number of temporary values that it stores on the stack, it may need to store registers on the stack, exception frames. So in a "real" system, the number of bytes [assuming we modify the code so that the calculations are actually needed for something and can't be optimized away completely as in the current case] would be almost anything between zero and hundreds of bytes.

    Just ONE possible variable is "how long do you keep a temporary around for". A very trivial compiler may produce a temporary to hold a + 3 before assigning it to b. Now, once it's been stored in b, do we still need the space? No. Does that absolutely mean that the compiler re-uses the space for something else? No. But it could (and any decently clever compiler WILL reuse stack-space that is no longer needed).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    So it seems what you're REALLY asking us to do is guess how your teacher has "assumed" the process would work.

    No doubt you could come up with a plausible explanation, which would be no more or less valid than any other explanation. But if it doesn't match teachers', then you lose marks for no good reason (don'tcha just love these kinds of problems).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Ok yes, I understand it's different for every compiler. But can we assume that certain things fall within certain ranges of bits or bytes taken up in the stack space? I mean, if ints take up 4 bytes in one compiler, they're not going to take up 40 in another... right?

    So is there a BALL PARK range of amounts of bits or bytes that say the temporary (a * b) might take up? I understand it depends on the compiler.

    Thank you for any answers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You can't assume ANYTHING.

    Sure, the standard requires an int to have a specific number of bits, which takes a specific amount of memory when stored.

    But for any given example you can post, it's a crap-shoot to guess how much memory is actually used for a stack frame.

    Sure, if all you want is a "BALL PARK", then just add up the sum of sizes of all the variables. It'll be close enough for what you seem to want it for.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by blernblan View Post
    So is there a BALL PARK range of amounts of bits or bytes that say the temporary (a * b) might take up? I understand it depends on the compiler.
    I could easily be wrong about this, but I don't think an operation like that will require any stack space in excess of the size of the variables, since they will be copied into CPU registers, and new values written out.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #13
    Registered User
    Join Date
    Apr 2009
    Posts
    10
    Quote Originally Posted by Salem View Post
    You can't assume ANYTHING.

    Sure, the standard requires an int to have a specific number of bits, which takes a specific amount of memory when stored.

    But for any given example you can post, it's a crap-shoot to guess how much memory is actually used for a stack frame.

    Sure, if all you want is a "BALL PARK", then just add up the sum of sizes of all the variables. It'll be close enough for what you seem to want it for.
    Thank you Salem. So the sum size of all the variables.... this means all 4 ints, and then all of the temporaries correct? Can you just tell me if temporaries usually take up a small or large amount of stack space? Do they take up more than variable declarations? Thanks again.

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A rough estimation could be to use sizeof of each variable and add them together.
    It won't give you a concrete example, but it might give you an estimate.
    Don't count on it, however. And definitely never rely on it.

    Temporaries are just normal variables in the sense when they take up space, if any.
    A temporary can be stored on the stack, but might as well end up in a processor register, if possible.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by blernblan View Post
    Thank you Salem. So the sum size of all the variables.... this means all 4 ints, and then all of the temporaries correct? Can you just tell me if temporaries usually take up a small or large amount of stack space? Do they take up more than variable declarations? Thanks again.
    See now you've gone and used the word "usually", implying that you're after a real-world answer from an actual compiler again. There is nothing "usual" about what you're looking for.

    How can you possibly expect to come up with an answer? Well perhaps there is no wrong answer, as long as you state all of your assumptions and that your final result agrees with those assumptions. This may be all you teacher is loking for.
    Any of these might be acceptable answers (0, 8, 16, 24, 44, 48 ...) provided you specify the right assumptions.
    For starters, how big are you assuming an int is?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. infix evaluation using stack
    By lewissi in forum C++ Programming
    Replies: 0
    Last Post: 11-03-2005, 02:56 AM
  2. stack implementation problem-help needed
    By sanju in forum C Programming
    Replies: 1
    Last Post: 12-10-2002, 07:29 AM
  3. socket question
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 07-19-2002, 01:54 PM
  4. stack and recursion help needed!
    By LouB in forum C++ Programming
    Replies: 3
    Last Post: 07-01-2002, 02:19 PM
  5. Relocatable code and Symbol Table
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 06-10-2002, 11:05 AM