Hello,
How is it possible to see if a heap buffer overflow occured?
I just took a look into the .map file and the size of the .bss section is 0x00003c28. I've 16kB internal RAM. The size of the stack is 0x00000488, which is enough for my application.
In the startup code the heap size is
Heap_Size EQU 0x00000000 AREA HEAP, NOINIT, READWRITE, ALIGN=3 __heap_base Heap_Mem SPACE Heap_Size __heap_limit
Does this means, that the heap size is the rest of the internal memory (up to the stack size)? And so everthing is correct (in my settings)?
best regards Bernd
Heap_Size defines the size of heap which is placed in memory after variables and before stack.
It is also registered by the run time library and the available heap size is checked for example when malloc is used. If not enough memory is available a NULL pointer will be returned for malloc (but no overflow will occur).
You need to configure Heap_Size in accordance to your application requirements.
thanks for your reply;
.bss section = heap size
(at the moment) my heap size is zero; and I have to change the size of the heap from Heap_Size EQU 0x00000000
to
Heap_Size EQU 0x00003c28
But what happened when the heap size is too small? Then the variables (which are not initialized) are stored within the stack?
bss is uninitialized variables, i.e. variables that should be cleared to zero when the program starts.
It has nothing with the heap to do. The heap, is a memory area for dynamic memory allocation, i.e where your program creates new variables while running, using calls to malloc() and releasing the new variables with free().
If you don't use any dynamically allocated variables, then you don't need any heap, and can leave the constant set to zero.
Obviously, you do not set the size of the heap to the size of your bss segment (from your map file), since they are two separate concepts. If the heap is too small, it only affects calls to malloc(), i.e. if malloc() will be able to locate an available memory block for a dynamically created variable, or if it will return NULL.
and if I use printf("hello world"); in my code - is the argument stored in the heap? Does printf() use malloc() to locate the memory block for "hello world".
No!
thanks Per - but which things are located in the heap?
e.g. the hardware buffer for the usart? Because in embedded systems I've nearly never dynamic created variables.
Bernd
Nothing in located in the heap, unless you use an RTOS or a library that places data on the heap - or you write own code that places data on the heap.
An RTOS may use the heap to allocate new threads.
A TCP stack may allocate buffers or new connects on the heap.
A communication library that supports runtime-configurable receive/transmit buffers may create these buffers on the heap.
Code that configures their buffer sizes/buffer counts etc as constants (enum or #defined) can statically allocate their buffers in the bss segment, and then the compiler/linker will take care of the memory allocation (hence static memory allocation).
The heap is for data structures that does not have a known size when the program is built. You might get the size information from a serial port, from configuration in an EEPROM memory or similar after the program has started.