Dynamic Memory in C++
Posted: Fri Aug 01, 2014 2:11 pm
Hi,
I've been attempting to play with C++ support in ChibiOS. I am working with a STM32F407 based discovery kit. Using ChibiOS-2.6.3's ARMCM4_STM32F407-DISCOVERY-G++ as a starting point.
Let me preface this with, yes I realize that typically one only uses a subset of C++ in embedded software. I am just playing with the possibilities of porting an existing codebase which makes use of such things as RTTI, exceptions, STL, and dynamic memory.
My toolchain is a Gentoo crossdev based chain using:
gcc/g++-4.7.3
newlib-2.0.0
binutils-2.23.2
gdb-7.6.2
I am attemping first to add the (seemingly) simple lines of code:
However, when the constructor of the vector attempts to allocate memory, I wind up in SIGTRAP in the bowels of newlib (do_AngelSWI).
I have attempted to implement (unsuccessfully) new/delete by enabling heap/malloc in chconf.h, and then adding:
to my project, but to no avail. The code compiles, but after loading the board locks up and I cannot even use the debugger.
I have tried upping the stack/process stack sizes from 0x0400 to 0x0800 and even 0x1000 to no avail.
Do I need to add something in the linker script to support new/delete and/or exceptions? I've seen several bits online alluding to this, but all seem like shot in the dark hacks, I was wondering what the recommended way is?
I've been attempting to play with C++ support in ChibiOS. I am working with a STM32F407 based discovery kit. Using ChibiOS-2.6.3's ARMCM4_STM32F407-DISCOVERY-G++ as a starting point.
Let me preface this with, yes I realize that typically one only uses a subset of C++ in embedded software. I am just playing with the possibilities of porting an existing codebase which makes use of such things as RTTI, exceptions, STL, and dynamic memory.
My toolchain is a Gentoo crossdev based chain using:
gcc/g++-4.7.3
newlib-2.0.0
binutils-2.23.2
gdb-7.6.2
I am attemping first to add the (seemingly) simple lines of code:
Code: Select all
#include <vector>
...
main( ... ) {
std::vector< int > vi( 10 );
....
}However, when the constructor of the vector attempts to allocate memory, I wind up in SIGTRAP in the bowels of newlib (do_AngelSWI).
I have attempted to implement (unsuccessfully) new/delete by enabling heap/malloc in chconf.h, and then adding:
Code: Select all
#include <new>
void *operator new(size_t size) _GLIBCXX_USE_NOEXCEPT { return malloc(size); }
void *operator new[](size_t size) _GLIBCXX_USE_NOEXCEPT { return malloc(size); }
void operator delete(void *p) _GLIBCXX_USE_NOEXCEPT { free(p); }
void operator delete[](void *p) _GLIBCXX_USE_NOEXCEPT { free(p); }
to my project, but to no avail. The code compiles, but after loading the board locks up and I cannot even use the debugger.
Program received signal SIGTRAP, Trace/breakpoint trap.
0x00000000 in ?? ()
I have tried upping the stack/process stack sizes from 0x0400 to 0x0800 and even 0x1000 to no avail.
Do I need to add something in the linker script to support new/delete and/or exceptions? I've seen several bits online alluding to this, but all seem like shot in the dark hacks, I was wondering what the recommended way is?