Eradicate all dynamic memory allocations

My predecessor in my current job wrote an RS232 command manager for the sam3x8e Arduino Due, and he populated a map like this:

    commandMap["CMD1"] = new CCmd1Handler;

Today I changed the code as follows:

    static CCmd1Handler obj_CCmd1Handler;
    commandMap["CMD1"] = &obj_CCmd1Handler;

I want to go through the entire codebase and eradicate as much heap allocation as I can.

Can heap allocation be avoided completely if you use I2C and RS232 on the sam3x8e?

Does the 'String' class use dynamic allocation? If so I might write my own alternative.

By the way I realise that std::map uses the heap but I'll write my own map that uses static memory.

It does

A permanent heap allocation on a small machine with no OS, uses no more RAM than a static allocation.

I'm weary of two things happening:
(1) Running out of free space on the heap
(2) Having enough free space on the heap but it's too badly fragmented

My first course of action to avoid both of these outcomes is to eradicate as much heap allocation from the codebase as I can.

You won't see any fragmentation from permanent heap allocation at start up. That can only happen with deletion.

The available heap space decreases with static allocations, so you can't win anything that way. There is no free lunch.

Yeah I realise that getting rid of the permanent heap allocations won't make a big difference, it's somewhere to start though in getting tighter control over the heap.

There are deletions when 'String' objects get deleted so I might write my own String class that uses static buffers.

Why? What do you need String objects for? There is an existing library that does what you say, SafeString.

If this is really happening to you, it's some general use of memory that needs attention, not the allocation method.

End of soapbox.

It's not happening yet.

Why? It may cause problems if you allocate deallocate repeatedly but if you do it once and never have to deallocate, why bother?

There once was a time in the recent past where what you are suggesting would have brought cheers from all in the forum especially the Uno folks.

But times have changed, memory resources have increased, and the ARM uC's such as the Due are 32-bit beasts with very clever compiler.
Arduino15/packages/arduino/tools/arm-none-eabi-gcc/

I have worked extensively with the STM 32-bit hardware lots and dynamic allocation has not posed any issues. My experience, others here may have a different story to tell.

I agree with aarg, but if you have spare time for the rewrite, go for it but you are taking ownership for the code your x-coworker wrote and after rewrite, you have no one to blame :grimacing:

And if the existing code creates no issue then your time might be better spent on something else (and definitely not rewriting yet another String library…)

AFAIK, neither Serial nor I2C uses dynamic allocation. (USB might, though.)

You can always search your binary (.elf) file for the presence of malloc() or new()

Whatever inherits from Stream has readStringUntil() and readString() that would perform dynamic allocation, so those would need to be searched for.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.