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

it is wong use malloc?

Dear Sir madams,

we wish use linklist to effishiently sort data then build tree in are controllar.

In your humble oppionion,it is wong use malloc,calloc and realloc in 8051?

Can you advise? It first year in industry after study in Beijing.

Worry I make mistak

Xin Nian Kuai Le

Parents Reply Children
  • Malloc can be useful if you do not know the size that something needs to be at compile time, and you have two competing needs for memory.

    If you have to free allocated memory then fragmentation becomes a problem.

    As with lots of C code care is need.

    Efficiency depends on what you are trying to do.

  • But you do still need to know the maximum size at compile time - because you have to define the heap size!

    So, given that you have to fix the heap size at build time anyhow, you might just as well define the data structures "statically".

    As has already been mentioned, it is not necessary to use dynamic allocation in order to use linked lists!

    It is perfectly fine to define a static set of list entries, and simply link them into an "in-use" list or a "free" list as & when required at runtime.

    That gives you all the convenience of linked lists with none of the risks & overheads of dynamic allocation.

    Of course, the 8051 is still not a great choice for a "data processing" task like this; an ARM would probably be a better choice - and may well be no more expensive...