Avatar of perlperl
perlperl
 asked on

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

C

Avatar of undefined
Last Comment
perlperl

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Kent Olsen

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
kaufmed

@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!)
Kent Olsen

@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
perlperl

ASKER
So the bottomline, there is no way to determine runtime the stack size due to compiler placement?
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Kent Olsen

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);
}
Member_2_5069294

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.
perlperl

ASKER
I tried but it doesnt gave me correct stack size maybe the compiler is not placing the variables in continous memory location
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Kent Olsen

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
Member_2_5069294

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.
Subrat (C++ windows/Linux)

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.
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
ozo

Is there something you wish to accomplish by computing the stack size?
perlperl

ASKER
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
Kent Olsen

Stack overflow is something that many/most run-time libraries check.

What compiler/environment are you using
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
perlperl

ASKER
Linux (CentOS)
Kent Olsen

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
Member_2_5069294

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.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Subrat (C++ windows/Linux)

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.
Kent Olsen

On a 16-bit OS, the default stack size is likely to be about 8K.
Subrat (C++ windows/Linux)

@Kdo:Yes u r right. I forgot it. 1MB is the total memory that a program uses.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
perlperl

ASKER
I agree Subrat2009,

I don't remember how i started in this stupid  direction of calculating stack size in between. crazy