• C

stack

Is there a way I can compute the stack size of a fucntion in the middle of it

Example)

void func() {
   // declare some variables
   // do some processing
   // Calculate the stack size used so far

   // declare some more variables
   // Calculate the stack size again used so far
}

Does the compiler stores all the variables sequentially in the stack? 

Open in new window

perlperlAsked:
Who is Participating?
 
Kent OlsenConnect With a Mentor Data Warehouse Architect / DBACommented:
Hi perl,

The variables will be stored consecutively on the stack in the order that they are declared.  Depending on word size, compiler options, etc.  there may be padding between the items to align them on byte, word, superword, or other boundaries.


Kent
0
 
käµfm³d 👽Commented:
@Kdo

The variables will be stored consecutively on the stack in the order that they are declared.
Is that accurate? I thought the compiler was free to rearrange allocations to prevent padding when possible. (Comp Org was a few years ago, and I wasn't the most attentive, so I may certainly be off-base!)
0
 
Kent OlsenData Warehouse Architect / DBACommented:
@kaufmed,

I don't know of a rule that requires the parameters to be on the stack in any given order.  That said, I'm not aware of any compiler rearranging the variables.  (They may exist.  I'd love to know.)  I have seen code that required the variables in declared order as the program used array overruns (instead of union/struct) to access adjacent items.

Kent
0
Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

 
perlperlAuthor Commented:
So the bottomline, there is no way to determine runtime the stack size due to compiler placement?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
I don't know of a direct way.  You may be able to write some assembly code.

Or some odd trick like this:

int F (char *x)
{
  char Name[10];
  int x1, x2;
  float z;
  char buffer[100];

  if (x = =NULL)
    F(Name);
  else
    fprintf (stdout, "Stack entry is %d bytes\n", (int)(Name - x));
}

main ()
{
  F(NULL);
}
0
 
satsumoSoftware DeveloperCommented:
Use braces to control scope, which controls stack allocation. You can get the size of variable by using addresses. Example:

void func (void)
{
    int numStackBytes;
    {
         // declare a bunch of variables
        {
             int topOfStack;
             numStackBytes = (((char *) &topOfStack) - ((char *) &numStackBytes)) - sizeof (numStackBytes);
        }
    }
}

Open in new window


With this method it doesn't matter what order the compiler places the variables on the stack. Note that once you exit the innermost scope shown, topOfStack is out of scope and numStackBytes is still in scope.

You could also do this by writing some machine code to store the value of the stack pointer into a variable before and after the variables are allocated, but you still need to be certain of the scope. Be aware that on some processors, the stack grows downwards in memory.
0
 
perlperlAuthor Commented:
I tried but it doesnt gave me correct stack size maybe the compiler is not placing the variables in continous memory location
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi perl,

Can you post the test code and describe what you're seeing?  The rules are pretty simple (all local variables are going to be on the stack).  We just have to analyze what you're seeing.


Kent
0
 
satsumoSoftware DeveloperCommented:
Are you sure its wrong size? What size are expecting and what size are you getting? It seems unlikely that the compiler is not putting them contiguously, because they are pushed on to the stack.
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
Compiler does optimization. If you are not using a variable, compiler may ignore that. So address diff which ur looking mayn't be correct.

So initialize variables at the time of declaration. and then start your calculation logic.or you may use a print statement to access all the declared variables.
0
 
ozoCommented:
Is there something you wish to accomplish by computing the stack size?
0
 
perlperlAuthor Commented:
I was debugging some crash and as thinking to add some debug statements by checking the stack size of the function to make sure I am not running out of stack limit and overwriting on some other functions stack space
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Stack overflow is something that many/most run-time libraries check.

What compiler/environment are you using
0
 
perlperlAuthor Commented:
Linux (CentOS)
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi perl,

I doubt that you're getting a stack overflow.  You might be getting an overrun (the function accessing memory outside of it's current stack variables), but not an overflow (where the entire stack is used up).

I'm pretty sure that the stack on CentOS C runtime is hardware protected so that you can't read/write past the end.


Kent
0
 
satsumoSoftware DeveloperCommented:
The only way you are likely to get stack overflow is if the function allocates a large array locally and you call it recursively. A recursion loop is generally the only thing that causes the stack to overflow and most debuggers will catch that and allow you to see the problem.
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
I don't thin anyone does calculation of stack during runtime.Rather smartly define the variable and it's scope. Decide which is highly necessay to be define in large scope.

For a 16bit OS like in DOS, stack size is 1MB but it varies in 32bit OS.
0
 
Kent OlsenData Warehouse Architect / DBACommented:
On a 16-bit OS, the default stack size is likely to be about 8K.
0
 
Subrat (C++ windows/Linux)Software EngineerCommented:
@Kdo:Yes u r right. I forgot it. 1MB is the total memory that a program uses.
0
 
perlperlAuthor Commented:
I agree Subrat2009,

I don't remember how i started in this stupid  direction of calculating stack size in between. crazy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.