Thread: What is the alternative to malloc?

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    30

    What is the alternative to malloc?

    Hi all,

    As we all know, one of the MISRA C rule discourages the use of malloc for dynamic memory allocation.

    So, does anyone know is there any alternatives for malloc which do not break this MISRA C rule?

    Hope to hear from you guys!
    Thank you!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think this is because embedded systems usually have there own specific means of dealing with memory management.

    After a little google I noticed that MISRA's own stance on this is that they will not recommend anything, and in particular they do not recommend malloc.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > As we all know, one of the MISRA C rule discourages the use of malloc for dynamic memory allocation.
    There are too many variables:
    - where it get memory from
    - how blocks are allocated
    - how blocks are freed
    - how fragmentation is dealt with
    - is it even thread safe

    I've seen one malloc/free in the past, where if you freed memory in the "wrong" order, then it would take 1000's of times longer to free the memory than if you freed it in the "right" order.
    This kind of random behaviour is something you don't want in an embedded system.

    You start with something like
    char pool[1000000];
    and you write myMalloc etc yourself.

    There are all sorts of strategies you can take when you're tailoring the solution to a particular problem (eg. using a pool approach where only fixed sized blocks are used).
    Allocating and freeing may waste a bit of memory, but they become very deterministic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    In high criticality code (which is what Misra C aims to support) there is generally a requirement for determinism i.e. complete predictability. Dynamic memory allocation and deallocation is not completely predictable for the sorts of reasons (like those mentioned by Salem).

    The reason that Misra do not recommend anything is that whatever is done is required to be justified by analysis suitable to the application and approved within some specific context.

    The most common strategy I've seen in high criticality systems is that all memory required by a program is allocated during program initialisation/startup and, once initialisation is complete, memory allocation is forbidden. Within that, the memory used by any particular function is predetermined at program startup. Some specific justification may sometimes be put forward for approval to use dynamic memory allocation (eg malloc()) during program startup and subsequently forbidding its usage.

    If a MyMalloc() function to access some global pool is used, there are very specific restrictions on its behaviour and usage. It can't be used as a "poor man's" malloc()/free(). Specifically, code could only use it to grab a specific block that is fixed at program design time (i.e. a function could not obtain some arbitrary block at run time and later release it). Otherwise, it would be forbidden, as it has potential to exhibit the same type of non-determinism associated with malloc()/free().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    30
    Thank you so much guys!
    Your inputs/information really "brighten" my brain!

    So, at the first place we must be sure of how many/much memory our program needs?but this could probably leads to memory waste/insufficiency if our estimation is wrong, am I correct?

    Between, are there any predictable memory allocation strategies such that the memory we allocated is sufficient througout the whole program?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by huwan View Post
    So, at the first place we must be sure of how many/much memory our program needs?but this could probably leads to memory waste/insufficiency if our estimation is wrong, am I correct?
    Where/what is this suppose to run on? I don't do embedded programming, but I presume you should know everything that is going on there, how much memory there is, and be able to decide how much memory a process will need. If you are going to run processes in parallel, calling one within another, maybe you need to take that into account too.

    But it is not really waste IF you take all available memory when no other possible process could have needed it, right? That memory would have just been unused.

    Vis, insufficiency, yeah, that would be a problem, so don't under estimate. Over estimating won't matter as long as you don't ask for more than what exists (which I guess should fail immediately).
    Last edited by MK27; 03-20-2010 at 10:58 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by huwan View Post
    So, at the first place we must be sure of how many/much memory our program needs?but this could probably leads to memory waste/insufficiency if our estimation is wrong, am I correct?
    For programs of significant criticality, it is necessary to perform whatever analysis and supporting measurements are needed to properly characterise memory usage (and usage of other resources) by each critical function. It is also necessary to provide sufficient evidence to satisfy an independent reviewer (or regulator) that the analysis provides sufficient assurance for the intended application.
    Quote Originally Posted by MK27 View Post
    But it is not really waste IF you take all available memory when no other possible process could have needed it, right?
    It is if there is a requirement to make provision for the system to evolve in future. If existing processes consume all available resources, then it is not possible to provide any assurance that a proposed new process will receive the resources it needs. It is also more difficult to give assurance that modifications of existing processes will not require more resources than made available to them.

    Incidentally, not all embedded systems require rigorous analysis to ensure their correctness. The process of design, implementation, and assurance for an MP3 player is generally less rigorous technically than a pace-maker.
    Last edited by grumpy; 03-20-2010 at 05:11 PM.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  8. #8
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    free() is used and alternate of malloc, used to deallocate memory....
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mr.777 View Post
    free() is used and alternate of malloc, used to deallocate memory....
    Another old thread....

    What you been smoking, son?

  10. #10
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Yeah was just checking out....
    Does this really bad to reply to older threads??
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Mr.777 View Post
    Yeah was just checking out....
    Does this really bad to reply to older threads??
    Well, think of it this way...

    1) It's almost certain the OP isn't reading anymore.

    2) It pops up as a new thread on everyone's lists, invoking time spent to discover its age.

    3) How and Who is this helping? It's very likely that old thread is not relevent to anything happening today...

    So... the question becomes... Why are you wasting our time?

  12. #12
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Okay... Gotcha... Peace.... :-)
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

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. Alternative to malloc
    By stellastarr in forum C Programming
    Replies: 13
    Last Post: 04-30-2007, 04:10 PM
  3. Is there a limit on the number of malloc calls ?
    By krissy in forum Windows Programming
    Replies: 3
    Last Post: 03-19-2006, 12:26 PM
  4. Malloc and calloc problem!!
    By xxhimanshu in forum C Programming
    Replies: 19
    Last Post: 08-10-2005, 05:37 AM
  5. malloc() & address allocation
    By santechz in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 09:08 AM