I'm confused about this. Is it that case that all local variables not allocated on the heap have stack-dynamic lifetime while global variables have static lifetime? Is the data segment of the executable used only for global variables while local variables live and die on the program stack? Or are variables local to the main() function static while variables local to loops are stack-dynamic? Or is it some other scheme? I noticed in writing a program that an uninitialized character array local to a for loop that I was using to buffer input had detritus from previous programs in it and was not properly null-terminated, suggesting that it was part of the stack. In Unix, uninitialized variables declared in the bss segment of an executable are initialized to zero (though it is possible that it was indeed initialized to zero, since the variable in question is a pointer to the first array element, rather than any of the array elements themselves). I hope I'm making sense here.
C++ pointers, references and Dynamic Memory... oh my :(
Siten0308 - Jul 31 2018 02:08 PM
Recent Blog Entries
Recent Status Updates
- Managed C++
- Visual Basic 4 / 5 / 6
- linked list
- hello world
Posted 26 January 2015 - 02:49 PM
It seems you're confusing several concepts.
1) The .data segment of an executable is for read-only data like string constants. For example, if somewhere in your program you have printf("Hello, World!\n") then the string "Hello, World!\n" would show up in your .data segment.
2) Local variables aren't allocated on the heap. They're always allocated on the stack, and will usually contain garbage when first instantiated. However, you can have a local pointer to something on the heap. The heap isn't guaranteed to be zeroed out when first created (and oftentimes isn't).
3) The .bss segment contains reserved space for statically-allocated variables, like globals or static local variables. This is guaranteed to be initialized to zeroes.
Edited by dargueta, 26 January 2015 - 02:50 PM.
sudo rm -rf / && echo $'Sanitize your inputs!'