Solved

stack

Posted on 2013-12-31
22
156 Views
Last Modified: 2015-05-28
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

0
Comment
Question by:perlperl
  • 7
  • 5
  • 3
  • +3
22 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 500 total points
ID: 39749065
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 39749083
@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
 
LVL 45

Expert Comment

by:Kdo
ID: 39749103
@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
 

Author Comment

by:perlperl
ID: 39785738
So the bottomline, there is no way to determine runtime the stack size due to compiler placement?
0
 
LVL 45

Expert Comment

by:Kdo
ID: 39785795
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
 
LVL 12

Expert Comment

by:satsumo
ID: 39795463
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
 

Author Comment

by:perlperl
ID: 39825344
I tried but it doesnt gave me correct stack size maybe the compiler is not placing the variables in continous memory location
0
 
LVL 45

Expert Comment

by:Kdo
ID: 39825364
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
 
LVL 12

Expert Comment

by:satsumo
ID: 39825367
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
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 39834415
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 84

Expert Comment

by:ozo
ID: 39834606
Is there something you wish to accomplish by computing the stack size?
0
 

Author Comment

by:perlperl
ID: 39895840
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
 
LVL 45

Expert Comment

by:Kdo
ID: 39895860
Stack overflow is something that many/most run-time libraries check.

What compiler/environment are you using
0
 

Author Comment

by:perlperl
ID: 39895878
Linux (CentOS)
0
 
LVL 45

Expert Comment

by:Kdo
ID: 39895914
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
 
LVL 12

Expert Comment

by:satsumo
ID: 39895983
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
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 39896497
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
 
LVL 45

Expert Comment

by:Kdo
ID: 39896598
On a 16-bit OS, the default stack size is likely to be about 8K.
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 39897952
@Kdo:Yes u r right. I forgot it. 1MB is the total memory that a program uses.
0
 

Author Comment

by:perlperl
ID: 39911281
I agree Subrat2009,

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now