Thread: Question = malloc()

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    1

    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?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There's always a reason.

    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.

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by hgedek View Post
    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?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dave_Sinkula View Post
    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.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by matsp View Post
    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'.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. malloc + segmentation fault
    By ch4 in forum C Programming
    Replies: 5
    Last Post: 04-07-2009, 03:46 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  4. malloc, calloc question
    By chen1279 in forum C Programming
    Replies: 12
    Last Post: 09-07-2006, 05:54 PM
  5. Question about malloc()
    By cdalten in forum C Programming
    Replies: 6
    Last Post: 05-12-2006, 10:57 AM