Question = malloc()

This is a discussion on Question = malloc() within the C Programming forums, part of the General Programming Boards category; Hi.That is my first message.
I have got a malloc problem.Using IAR embedded workbenc we are trying to develop programs.We ...

Question = malloc()

Hi.That is my first message.
I have got a malloc problem.Using IAR embedded workbenc we are trying to develop programs.We count the used memory when calling malloc function and free.Although we know there is free memory , malloc returns null. do you think any reason?

Memory fragmentation could be one cause. In fact, it's the most LIKELY cause.

This happens when a large block of memory is needed, and the free memory is only available in small lumps, all of which are smaller than the memory needed. Does this seem sensible for what you are doing?

Some possibilities follow. Without more information, it's not possible to eliminate any of these (and there may be other possibilities too).

malloc() calls may allocate more memory than you ask for (eg round up to some multiple of a defined block size, so subsequent calls to realloc() that increase size don't necessarily need to do anything).

It's possible that your counting scheme is using memory.

If the order of free()'s differ from the malloc() calls, then there may be memory fragmentation (eg small blocks that have been free()'d, but are smaller than that requested by a subsequent call to malloc()). If you allocate small blocks early, and large blocks later, this can mean memory that has been freed, but cannot be grabbed by a subsequent malloc() call.

I have got a malloc problem.Using IAR embedded workbenc we are trying to develop programs.We count the used memory when calling malloc function and free.Although we know there is free memory , malloc returns null. do you think any reason?

For what target micro? In the smaller embedded systems, is malloc even implemented? What are the related setup issues?

For what target micro? In the smaller embedded systems, is malloc even implemented? What are the related setup issues?

Most systems where you have more than a few KB of RAM, you would have malloc() and free() [or some other functions that can me translated to malloc() / free() via some simple wrapper function or a pair of macros]. Seeing as the original poster says that he's using malloc() and free() to account for memory usage one would assume that it's being implemented.

Most systems where you have more than a few KB of RAM, you would have malloc() and free() [or some other functions that can me translated to malloc() / free() via some simple wrapper function or a pair of macros]. Seeing as the original poster says that he's using malloc() and free() to account for memory usage one would assume that it's being implemented.

I've used Embedded Workbench on some small systems. What I remember of it in regard to malloc was that it was a PITA to make it actually work beyond a trivial 'malloc returns NULL'.