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
  • using dynamically allocated memory comes with a cost - memory fragmentation which slows memory access over time is one of them. if a path in the program fails to release it, you are in serious trouble. don't forget that you are not working on a powerful PC. better to allocate a large buffer statically and limit the maximum amount of incoming data, or ignore it is it exceeds maximum allowed size. but if you choose to use it anyway, don't forget to check the return value of the allocating function!

Reply
  • using dynamically allocated memory comes with a cost - memory fragmentation which slows memory access over time is one of them. if a path in the program fails to release it, you are in serious trouble. don't forget that you are not working on a powerful PC. better to allocate a large buffer statically and limit the maximum amount of incoming data, or ignore it is it exceeds maximum allowed size. but if you choose to use it anyway, don't forget to check the return value of the allocating function!

Children
  • Thank you. Think i see what you say. unnowm size, unnowm memory leek. Bad for us. dangerus for hole viallage.We speak to boss to tell him limits.but need know which mefod is more effishient?

  • 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...