Use of malloc in a constructor?

10 May 2012

I have never written a class before but am working on one for FIFO queues. I hope to be able to allocate the buffer for each instance (I am not sure of terminology) so that different queues using this class can have different size buffers.

I ran into symptoms that appear to be a problem with using malloc inside the constructor for the class. I blindly put an interrupt disable/enable around the malloc and this really seems to make the difference between the mallocs success and failure (returning NULL).

As long as I use the interrupt wrapper, I can declare multiple objects using my Fifo class and they seem to work properly.

I realize the problem could be something other than this and the slight changes in code just happen to appear to fix it, but I wonder if anyone knows if this could be a problem?

I find suggestions to use new/delete in place of malloc/free in C++. (In my application I never need to free up the allocation of the bufers.) I will look into trying new instead of malloc and see if that makes any difference.

Thanks,

Chuck

11 May 2012

Is it possible that you are doing memory allocations from both the main line code and an ISR at the same time? This could lead to heap corruption and the interrupt disable/enable code would fix it.

11 May 2012

beware malloc() free() on microprocessors. garbage collection (coalescing free'd blocks) is not done well, and is time consuming if done at all. And deterministic control programs shun such.

best to instantiate classes and data structures at compile time in small micros. C++ if used as if the micro were a gigabyte PC, by naive programmers, will krump.

Serious system programming and real time/control, mission critical, most often on micros use C or very carefully crafted C++.

It's a grey area and widely debated - how much RAM one needs before going berserk with dynamic memory allocation and garbage collection.

Of course, in an ISR, one should't use malloc() or new()

11 May 2012

Adam and Steve,

Thanks for the comments. My allocation of the storage for the Fifo objects, done in the constructor, is only done during initialization, and is the only allocation done. This is not done in an ISR. My primary purpose experimenting with this was as a learning experience while building my application. Since I will use four Fifos, and only one needs to be large, it would be nice to be able to save memory by letting each object allocate the appropriate size buffer, but I am certain I could just use the max size for all of them and still have plenty memory available. If new() seems to work reliably (so far it does), I will stick with it, but I can always fall back to using fixed storage assignments.

In my working life, I stayed away from C++ for our embedded projects and never used any dynamic allocation. With the mbed I finally decided to try to learn some OOP basics, and it looked like a good occasion to try dynamic allocation at the same time. So it has been interesting to try to write this simple class and learn a bit about OOP.

Thanks again.