malloc() - is it really necessary?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

malloc() - is it really necessary?

1,674 Views
FridgeFreezer
Senior Contributor I

Before you all say "yes of course it is" I'd better explain myself:

 

In our application we are using a micro "bare" - no RTOS, no other processes etc. just one main() loop going round and round as is traditional. All data which is not transient is in a global data structure passed by pointer to any functions.

 

The code we inherited does not use malloc() at all and works fine, but developing for a newer micro, some of the Freescale drivers/example code etc. use malloc() to setup buffers and so on. Now, this is clearly of use where you are calling different programs and returning and so on, but in a dedicated environment it seems rather pointless and a potential source of bugs to be including more libraries than we have to.

 

Anyone care to expand - is it necessary / required / useful / non-essential / totally useless in our application?

Labels (1)
0 Kudos
3 Replies

669 Views
jbezem
Contributor III

As I have written a full-fledged, preemptive multitasking kernel myself, without using dynamic memory management at all, I'd say you don't need it per sé.

If you have multiple dynamic operations going on (like communication protocols and the like), you may very well need more memory in total, since you need to pre-allocate the worst-case memory needs for every operation separately, whereas dynamic allocation lets you spread the load.

And of course, you gain flexibility when using dynamic allocation: Assuming that you consistently check for unsuccessful allocations, you could implement a strategy to limit the use of dynamism in the case of high memory load, whereas expanding your buffers/cache/... when memory is plentiful. That's something you cannot do with pre-allocation.

 

It's an architectural decision, for which it is good to know the alternatives and their respective consequences on the basis of your environment.

I've always taken the stance that, given a sound design, strict error control and a set of decent tools, dynamic memory is not a big problem. Encapsulation helps, be it in C functions or in C++ classes.

 

FWIW,

 

Johan

0 Kudos

669 Views
ralu
Contributor III

 

Using malloc() is inheriteted from developing on non RTOS such as linux/windows. Even if you are using any kind of OS you have to consider heap allocating, since you usually can not afford of having that much of stack. (keep in mind that each process has to have its own stack). So if you have for instance non preemptive OS and you free your heap before switching to another process there can be much effective use of limited memory resources and also maintain "provability of stability" even in mission critical applications.

0 Kudos

669 Views
FridgeFreezer
Senior Contributor I

Thanks guys, I think I can take that as "no you don't need it" :smileytongue: since our app is a dedicated one with no OS. I think all the malloc() stuff will just be adding overhead & complexity to the code.

0 Kudos