0

When I declare an array in C in the following way, all 24 bytes are put in to stack:

void func(void){char array[24];} 

However, when I use declaration like this, the compiler puts the array in BSS RAM:

void func(void){static char array[24];}

Is there a rule what is the longest array that I can put in to stack or when I have a local array it should be always declared as static?

4
  • 3
    Do you know about malloc et al.? They are the usual methods of moving data off the stack, since static has other implications. Commented Jun 14, 2012 at 14:02
  • This is highly platform-dependent. It also depends on the nature of your program; library code shouldn't over-use the stack since you never know how deep in the call chain it's going to be used. Commented Jun 14, 2012 at 14:02
  • 2
    With and without static are completely different beasts! For example, if your function is recursive, using static array as if it were auto will most probably lead to data corruption. So the compiler will pick different storage schemes for these two cases, no matter what the array size is. Commented Jun 14, 2012 at 14:03
  • Just to be specific, in case you don't realise: if the array is static then it is shared between all concurrently-executing calls to your function. If the array is automatic, each call to the function has its own instance of the variable. That's why you can't say, "it should always be declared as static", because in many situations that sharing is not acceptable. Commented Jun 14, 2012 at 15:06

1 Answer 1

3
void func(void){char array[24];} 

all 25 bytes are put in to stack.

24 bytes, not 25.

Is there a rule what is the longest array that I can put in to stack or when I have a local array it should be always declared as static?

There is no such rule, you just have to be reasonable, where "reasonable" depends on your platform (eg. it would be less in the kernel with 4k stacks than in userspace application with typically ~1MB stacks).

However, I would advise against changing local arrays to static arrays for speed reasons. Function static variables still have the vices of global variables, that they make the function not reentrant. This might or might not be an issue, but the first choice solution when dealing with excessive stack usage should be moving the variable to the free store:

void func(void){
  char* array=malloc(24); 
  /* do something */ 
  free(array); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

I write cgi aplication for uclinux.My longest local array is 512 bytes and I wonder is it better to declare it static.
@Lambov: what's the stack limit for the threads that call your function? Is your function recursive, used from multiple threads, or used from signal handers? If the answers are "a few multiples bigger than 512 bytes" and "no", then you might be alright with the array on the stack. If the answers are "not much bigger than 512" and "no", then making it static might help. If the function is recursive (and the array is used across the recursive call) then you cannot make it static. Since we don't know whether your function needs to be re-entrant, we can't tell you what to do.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.