Hard Fault error while using NEW operator in cpp
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-06-19 3:10 AM
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(); }- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-06-19 4:52 AM
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
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-06-19 5:11 AM
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?- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2018-06-19 7:08 AM
So sufficiently large heap, or enough memory generally?
I have often seen a default heap size of zero generated by toolchains.
