Implementing malloc with gcc in an arm7 problems : malloc return NULL?

Up vote 0 down vote favorite share g+ share fb share tw.

I am adding malloc support in my firmware and I think I'm missing something! I use code sourcery g++ lite library for an arm7tdmi processor and my code is based on the example found in this link : e2e.ti.com/support/microcontrollers/stel... I added my version of _sbrk : char * _sbrk(int incr) { //extern char _end; /* Defined by the linker */ static char *heap_end; char *prev_heap_end; register char* stackPtr; if (heap_end == 0) { // first allocation heap_end =HEAP_END; } prev_heap_end = heap_end; // get current stack pointer asm ("mov %0, sp\n\t" : "=r" (stackPtr) ); if (heap_end + incr > stackPtr) { return NULL;// error - no more memory //write (1, "Heap and stack collision\n", 25); //abort (); } heap_end += incr; return (char*) prev_heap_end; } Some defines used by sbrk : #define SDRAM_SIZE 16*1024*1024 #define HEAP_BASE _ebss #define HEAP_END ((_stext + SDRAM_SIZE) -1) #define HEAP_SIZE HEAP_END - HEAP_BASE (_ebss and _stext come from my linker file) Here's my main where I did a simple malloc/free call : void C_main ( void) { char * testmalloc=0; /* Initialize "Heap Descriptor" pointer */ pHeapDescriptor = __rt_embeddedalloc_init ((void*)HEAP_BASE,HEAP_SIZE); testmalloc = malloc(2048); free(testmalloc); } I run this program in step mode. When I call malloc, it eventually call my _sbrk implementation, the return value (prev_heap_end) have an expected value, but when the program return to the main, the testmalloc value is NULL (somewhere in the gcc library, the prev_heap_end is lost).

Does someone have an idea of what I'm doing wrong? Don't know if that will help, but this is my gcc compilation parameter : arm-none-eabi-gcc -march=armv4t -mcpu=arm7tdmi -dp -c -Wa,-adhlns="../../Base/Lib/Pa/main.o. Lst" -fmessage-length=0 -fno-zero-initialized-in-bss -MMD -MP -MF"../../Base/Lib/Pa/main.

D" -MT"../../Base/Lib/Pa/main. D" -fpic -mlittle-endian -Wall -g3 -gdwarf-2 ../../Base/Hardintrf/Mezzanine/main. C -o"../../Base/Lib/Pa/main.

O" Thanks in advance for any help! Gcc malloc arm link asked 3 mins agoMartin Allard384 64% accept rate.

Your first global vars are not initialized, so they might not be guaranteed to be NULL initially like you assume. Have you tried that? – SB.

57 secs ago.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions