Object lifetimes
stefanct opened this issue · 1 comments
Hi,
for my Embedded System students I was looking for some decent (concise but correct and helpful) documentation about stack usage in C and CBC looked most promising (which is quite a compliment from me :). However, there are a few nitpicks that I have to criticize because I think they can easily confuse readers.
You write in the 7th chapter
As a side note, there is a way to tell C to keep a stack variable
around, even after its creator function exits, and that is to use the
=static= keyword when declaring the variable.
I would strongly recommend not call it a stack variable but a (function-)local variable in this sentence because declaring it as static
actually prohibits it from becoming a true stack variable.
Besides that I would have wished that the related topic of object lifetimes and global variables are discussed in vicinity of these words. This topic is shortly covered in chapter 5
If you want local variables to persist, you can declare them as
static local variables. You simply insert the word =static= in front
of the variable type when you declare it inside your function. When
declared in this way, the variable will not be destroyed when the
function exits, but it (and its value) will persist.
This is also a bit misleading because the static variables are actually allocated by the startup code before the main function is called and thus cannot really be "destroyed". If one would reword that to mean that the content of the variable is not destroyed and add some brief explanation how and where this kind of variables are allocated it would be way more clear IMHO.
Later in the same section
When would you want to use static variables? One general case, like
above, is when you want to keep track of the number of times a
function has been called. Another reason has to do with
efficiency... if for example your function declares a large local
variable whose values don't change from one function call to the next,
it may be more efficient to declare it as static, so that it is
created and initialized only once.
Another reason to use global or static variables is the very limited size of the stack (a few MB usually). Even on PCs they are quite limited but on microcontrollers this is even more true. There the stack is often less than 1kB and that easily becomes a problem and is very hard to debug for novices.
So my wish list would be
- Changing the wording about the static variable in chapter 7
- Adding hyperlinks between the respective sections in chapter 5 and 7
- Explaining the allocation of static and global variables a bit more explicitly (probably in chapter 5)
- Note the limited size of the stack (and thus a reason to use static variables)
- Possibly a dedicated summary of the object lifetime scheme in C in chapter 7, making it become 'Heap vs. Stack vs. Globals' or similar :)
I don't know if i can find the time but if you agree with the above in generals I could look into implementing that myself and do a PR.
Cheers!
Dear Stefan,
Thank you for your nitpicks! I truly appreciate it. I will have a look over this in the next few days.
If you have any specific suggestions about which pieces of text to change, and how, by all means let me know.
Perhaps we can decide together how best to modify the material so that it’s most accurate.
-Paul