1

I am programming some embedded device which has 64 MB SDRAM. (C is used as programming language). Is it possible to make a guess (maybe even a rough guess) about the possible size of the stack of this device? Referring to memory which gets used when we make allocations such as, e.g.,

char s[100];
int t[50];

etc.

e.g., will it be more than 50KB? etc. that is what I mean with rough

plus when I have variables inside some function f

f()
{
  int p;
}

when f() exists, this variable dies right?

So when I call f2():

void f2()
{
  char t[100];
}

Only the size of a 100 element char array will be added to the stack size right?? size of int p from previous function is not considered anymore.

3

4 Answers 4

5

All sorts of guesses can be made :) Most (all?) embedded development environments provide mechanisms for allocating device memories (read-only, stack, heap, etc.) This is commonly done through linker directive files or C #pragmas placed in a setup source file. Without more information on your development environment, no accurate guess can be made.

In function f(), variable p will exist on the stack. When the function exits, that location on the stack will likely be used for something else.

As for function f2(), you can expect that 100 bytes from the stack will be assigned to t while this function is executing. The size of p will not be considered.

Note that the stack can be used for other information, so you cannot reliably estimate stack usage without considering other factors. For example, do you expect recursion? The stack can be used to store function call/return information - thereby reducing the amount of space you have for local (stack) variables.

Lastly, I've worked with devices operating with less than 1KB of stack, so assumptions should be made carefully.

2
  • ok but it should be more than 10-20 KB I guess.... (what about second part of my question?)
    – user2568508
    Commented Jan 24, 2014 at 15:56
  • 1
    Agree with @Throwback1986. You need to read through the documentation of your build system (compiler/linker/locator tools) to find out how and were the stack size is defined. Keep in mind there may be some default value if a size is not explicitly defined. Just because you have a lot of DRAM doesn't mean there is a big stack. While you are looking through your documentation for stack size, you should also find how/where the heap size is set...especially if you are using dynamic memory allocation.
    – semaj
    Commented Jan 24, 2014 at 16:16
1

your question looks like ,"Guessing the stack size"

Why guess when you can know it exactly its not from the sky ! :)

For an embedded programmer the stack size is always his in hands,one has to handle it through the linker command file that he submit to a loader

some thing like this as below

Linker.cmd

MEMORY
{

 .
 .
    SARAM2  (RWIX) : origin = 0x039000, length = 0x010000 /*64KB*/  
 .
 .    
 }

 SECTIONS
 {
 .
 .
   .stack     > SARAM2 
   .sysstack  > SARAM2 
 .
 .
 }

so its clear that you can set your own stack size provided "the stack size is limited to stack pointer bound"

so it is completely depends on your stack pointer range, if your stack pointer is 16bit you stack size is limited to 2^16 which is 64KB

for instance moving away from firmware programming, in a standard linux machine also if you go for typing

ulimit -a

you will get your limited stack size,and its extendable up to the boundary where Stack Pointer can point to

BTW These may further help you

Direction of Stack Growth

When Does Stack Really Over Flow

and i also suggest its not a bad idea to monitor your stack size , in other words trying to find Stack pointer value which can make you clear 'what is your stack status ?', especially for an embedded programmer a stack overflow cause severe damage :)

3
  • I believe that what he intended was related to determining the stank requirement of a thread or process rather than what actual the stack allocation is set to.
    – Clifford
    Commented Jan 25, 2014 at 17:07
  • @Clifford, Shyam: I have some variables like char c[100], etc. and I am trying to be careful not to overflow stack, that is what I was interested in.
    – user2568508
    Commented Jan 25, 2014 at 22:50
  • @user2568508 ok then try to use malloc or your 'custom malloc' which will allocate memory for you in a specified location like your own defined heap and free it after your usage is over,i think this will really solve your problem even i have used this technique many a times
    – kakeh
    Commented Jan 26, 2014 at 16:14
0

There is a script in the Linux kernel sources that can help you find the functions that heavily allocate on the stack. The script is named checkstack.pl and is located in the scripts directory.

Here is a sample invocation:

$ objdump -d $BUILDDIR/target-isns | perl checkstack.pl x86_64
0x00403799 configfs_handle_events []:                   4384
0x004041d6 isns_attr_query []:                          4176
0x00404646 isns_target_register []:                     4176

The script displays the functions that consumes the most space on the stack. The script can help you make a guess for the stack size required. Also, it helps you determine which functions should be optimized with regard to their stack usage.

4
  • Do we know he is using Linux? That would probably be the minority of embedded systems.
    – Clifford
    Commented Jan 25, 2014 at 17:08
  • No, we don't know if Linux is used. Linux powering a "minority of embedded systems" is a subject of debate ;) Today, there is fair share of smartphones, in-vehicle infotainment systems, NAS devices, TVs, running Linux. And that share is increasing... Commented Jan 25, 2014 at 17:30
  • Linux may be a fair guess, but if it is wrong the answer will not apply. To be clear, I mean the majority of projects rather than deployed units; one high volume consumer application may outnumber thousands of separate bespoke applications in terms of units. I have developed embedded systems on trains, industrial machinery, marine navigation, test equipment, and robotics on 8, 16 and 32bit targets and never deployed Linux. You may "see" a lot of Linux, but there is much more you don't see.
    – Clifford
    Commented Jan 25, 2014 at 17:42
  • Yes, that's true: there is much more I don't know about. Commented Jan 25, 2014 at 17:48
0

You may find that your linker is capable of performing stack requirement analysis - normally by command line option to specify map file content. If you could add information about your toolchain, you will get better advice.

What such analysis gives you is the worst case stack usage and the call path involved for any particular function - for the process total you need to look at the stack usage for main() and any other thread entry points (each of which will have a separate stack). You may need also to consider ISRs which on some targets may use the interrupted process or thread stack, on others there may be a separate interrupt stack.

What linker stack analysis cannot determine is stack usage in the case of recursion or call through function pointers, both of which depend on the prevailing runtime conditions that cannot be determined statically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.