Skip to main content
thomas blarmin
Visitor II
June 19, 2018
Question

Hard Fault error while using NEW operator in cpp

  • June 19, 2018
  • 2 replies
  • 1047 views
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();

    This topic has been closed for replies.

    2 replies

    Tesla DeLorean
    Guru
    June 19, 2018
    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 VenmoUp vote any posts that you find helpful, it shows what's working..
    AvaTar
    Senior III
    June 19, 2018
    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.

    Alan Chambers
    Associate III
    June 19, 2018
    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?