I have read somewhere that you should first create a memory heap
but I can't find how to accomplish that.
/* .heap section which is used for memory allocation */
.heap (NOLOAD) :
__heap_start__ = .;
. = MAX(__heap_start__ + _HEAPSIZE , .);
__heap_end__ = __heap_start__ + SIZEOF(.heap);
register char *stack_ptr asm ("sp");
caddr_t _sbrk_r(void *reent, size_t incr)
extern char end asm ("end"); // Defined by the linker
//extern char end asm ("__heap_start__"); // Defined by the linker
static char *heap_end;
if( heap_end == NULL )
heap_end = &end;
prev_heap_end = heap_end;
if(( heap_end + incr ) > stack_ptr )
/* Some of the libstdc++-v3 tests rely upon detecting
out of memory errors, so do not abort here. */
//write( 1, "sbrk: Heap and stack collisionn", 32 );
uart_puts("HALT - heap exhaustedn");
//errno = ENOMEM;
return (caddr_t) -1;
heap_end += incr;
return (caddr_t) prev_heap_end;
I only have malloc() so I can use printf() for debugging.
The reason I want to use malloc is that I don't know how many I get, the other way around would be using a large array that can be filled with data but you will never use the whole array or if a mistake is made and the size of the array is too small the array will overflow.
in my opinion when there is a unknown amount of data to store malloc is used
You might say that you can check the return value of malloc
if you do you must test the application in "worst case" scenario
to be clear I never said you shouldn't check the return value of malloc.
this was not easy too define and yes using malloc makes testing (and especailly debugging) more difficult.
Retrieving data ...