cancel
Showing results for 
Search instead for 
Did you mean: 

Hard Fault error while using NEW operator in cpp

thomas blarmin
Associate
Posted on June 19, 2018 at 12:10

Hi All,

   I'm working on STM32L4 MCU  in CPP project(cubeMX code that c code i convert to cpp), i have one doubt in NEW operator,  i  create array of pointer object  and i build code no error but while debugging i got Hard Fault error .

code:

 header.h:

class cRcdMaker 

{

protected:

cRecorder* mRecorders[3 ];  //

cRecorder  class

};

code.cpp:

for ( i = 0; i < mNumSimultaneousRecorders; i++ )

{

mRecorders[1] = new cRecorder();

3 REPLIES 3
Posted on June 19, 2018 at 13:52

So sufficiently large heap, or enough memory generally?

Like all Hard Faults, go look at the processor registers, and the disassembled code at the fault

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Alan Chambers
Associate II
Posted on June 19, 2018 at 14:11

As Clive says, check the heap size. How large is cRecorder? Are these the only dynamic allocations in the program? Is it the first allocation which fails (this might indicate a fault other than heap exhaustion)? Assuming sufficient memory operator new should work just fine, but it's worth stepping into the implementation to see what it actually does.

Personally, I rarely use dynamic allocation partly because of concerns about heap fragmentation. I suppose it depends on the application, but I don't generally have non-local objects with limited lifetimes anyway. Do your objects genuinely have lifetimes shorter than the application?
Posted on June 19, 2018 at 14:08

So sufficiently large heap, or enough memory generally?

I have often seen a default heap size of zero generated by toolchains.