Storage allocation puzzles ... heap vs static ...
Posted: Sat Dec 17, 2016 12:28 am
I'm a dummy in this area but keen to learn. I hear phrases like IRAM, cache and heap and am trying to build a mental picture.
I think of the heap as a chunk of memory (RAM) from which data can be carved. In simple terms, this is where malloc() storage comes from.
The program stack is where local variables are pushed. For example:
both the variable "a" and "b" are local to "myFunc" and placed on the stack to be automatically deleted as the stack unwinds from the function return. I also believe that the "size" of the stack is specified in the FreeRTOS stack size parameter when one creates a task. I also believe that the memory for THAT stack is carved from the heap.
Now, and assuming the above is correct ... I wondered where "global" data is stored? What follows are two program fragments that are nearly identical with the distinction of the storage classifier.
and
In the first program, the size of the free heap did *NOT* decrease. While in the second program the size of the free heap decreased by the expected 10K. I would have naively expected that in both cases, the "buffer" variable of size 10K would have been carved from RAM (heap) ... while in the first case, it would have been initialized from its initial value (which I would assume is stored in flash). However, in my test, the first program did NOT decrease heap size ... but yet (unless I am horribly mistaken) is still writable. So where then is the RAM storage for the first buffer coming from?
Again ... I'm a dummy here ... so if we have explanation to share, please don't assume anything and if there are assumptions being made on interpreting an answer, ideally lets try and call those out.
I think of the heap as a chunk of memory (RAM) from which data can be carved. In simple terms, this is where malloc() storage comes from.
The program stack is where local variables are pushed. For example:
Code: Select all
void myFunc() {
int a;
char b[100];
...
}
Now, and assuming the above is correct ... I wondered where "global" data is stored? What follows are two program fragments that are nearly identical with the distinction of the storage classifier.
Code: Select all
static char buffer[10*1024];
void myFunc() {
…
}
Code: Select all
char buffer[10*1024];
void myFunc() {
…
}
Again ... I'm a dummy here ... so if we have explanation to share, please don't assume anything and if there are assumptions being made on interpreting an answer, ideally lets try and call those out.