This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

heap

Simple question probably..

I have define my heap in the startup.s
Heap_Size EQU 0x00001032

In a function I use:
void SST25_EditEXTStr(...)
{ unsigned char *buffer;

buffer = malloc(0x1000);

if (buffer != NULL) {

//do my stuff

free(buffer);

}
}

How come I can not set my Heap_Size to 0x00001000. If I do the buffer will be NULL

Thnx

  • You can not allocate all memory from a heap. First off, the heap may need a number of bytes on the heap for global heap state. But every memory block you allocate will also be increased with a number of bytes to store hidden information about the memory block - the size and how to find the next block on the heap.

    Most heap managers will allocate 8 to 16 bytes of extra data that they initialize to form some kind of linked list. They will then step forward the pointer 8 to 16 bytes before returning it from malloc(). When you call free, they can then step back 8 to 16 bytes to get this magic extra.

  • Thank you for your reply.

    I have tried to minimalize the heap size by trie and error :-S and a oversize of 32 bytes was the minimun on which it still worked.

    My conclusion was the same as yours, I hope someone can tell me how much overhead it uses per malloc().

    I now think I have to use 32bytes extra on every malloc()

    meaning:
    buffer = malloc(0x100);
    buffer = malloc(0x1000);

    needs a heap_size of 0x1164.

    could this be correct? Or am I thinking crappy..

  • 0x1164 would represent an overhead of 0x64 which in decimal is 100 bytes.

    But do you really need a heap? Can't your program perform static allocations on startup, i.e. normal zero-initialized variables of potentially large size? Then you will not need any overhead at all, and the linker will not need to link in the heap code.