Stack And Buffer Issues

I've got a variable declared inside a functions like so:

char dump[1024];

When the function executes and makes a call to another function from within, my processor goes off to never never land. I can reduce the buffer size from 1024 to say 64 and the problem goes away (my stack is 256).

Now, if I declare that same buffer with the static keyword then I can use my original size of 1024 without problems.

Can someone explain to me why declaring it as static effects the stack size on my ARM processor?

Thanks!

Reply to
eeboy
Loading thread data ...

buffer

use

Because when you declare it as 'static' it does not go on the stack.

Reply to
srl100

Local variables are allocated on the stack, which is obviously too small in your case. Declaring the local variable static moves the variable to 'normal' memory while it is still only known within the function where it is declared. It also means that everytime you enter that function, the static variable still contains whatever you put in it the last time you called that function, while a normal (=volatile) local variable contains random data when the function is entered.

This static feature can be very useful to keep state variables or other stuff you need to save between invocation of that particular functions.

And here endeth the lesson,

Meindert

Reply to
Meindert Sprang

Mote that the use of the work "volatile" above is informal; C has a "volatile" keyword which means something completely different. A local variable which isn't "static" is called an automatic variable.

You can make this explicit with the "auto" qualifier, e.g.:

auto char dump[1024];

However, I've never seen this keyword used (outside of language tutorials) in ~20 years of C programming. As automatic storage is the default, and far more common than static storage, there really isn't much point in specifying it explicitly.

OTOH, it makes the function non-reentrant. This means that you can't call it recursively, or from more than one thread, or from an interrupt handler. With an automatic variable, each invocation of the function gets a separate copy of the variable.

A "static" local variable behaves exactly like a global variable at run time, but at compile time the variable's scope is limited to the block in which it is declared.

Reply to
Nobody

You seem to be using C, and it doesn't. static variables do not disappear when you exit the function. They are assigned globally, but the name is only known within that function. Just read your C book.

--
 [mail]: Chuck F (cbfalconer at maineline dot net) 
 [page]: 
 Click to see the full signature
Reply to
CBFalconer

And since we're pointing out the otherwise obvious, the tutorial would not be complete without mentioning the validity of returning a pointer to the auto. It's Jason's turn to speak now, I think. Why is it not valid to return a pointer to the stack var?

Reply to
MikeWhy

ElectronDepot website is not affiliated with any of the manufacturers or service providers discussed here. All logos and trade names are the property of their respective owners.