Reputation:
How is a static variable "retained" from one call to another? Does it use something like malloc
to put it in the program memory, or is there other way that it's stored for all the program to view that variable, for example:
static int counter = 4;
[similar to:]
int counter = malloc(sizeof(int));
counter = 4
Upvotes: 0
Views: 72
Reputation: 608
Static scalar variables (float, int, byte) are stored in the data segment of the memory. the compiler assigns an address in this memory area that is allocated to the program when it is loaded into memory. other areas : stack (for pushing parameters to function, methods) heap : dynamic memory used by malloc, new.
https://en.wikipedia.org/wiki/Data_segment
Upvotes: 1