- Local variables (declared within a function) are stored on the stack, provided they have not been declared with the static attribute.
- Local, non-static variables are sometimes called “automatic” variables. (There is even a C keyword, auto, but it is almost never used in practice.)
- Local variables (declared within a function) that have the static attribute are not stored on the stack; they have fixed addresses in memory.
- Variables with file scope (declared outside of any function) are not stored on the stack; they also have fixed addresses in memory. The static attribute is irrelevant here (it merely affects the visibility (scope) of the variable).
- Arguments passed to a function by value are stored on the stack. The calling function makes a copy of the data and makes that copy available to the called function.
- Arguments passed to a function by reference require special consideration. The data pointed to by the reference has whatever storage the variable in the calling program segment has. The pointer itself (being passed to the function) is on the stack.
April 1, 2009
Storage rules for C variables
Filed under: Embedded Design — paritycheck @ 6:59 am
No Comments Yet »
No comments yet.
RSS feed for comments on this post. TrackBack URI