I’ve found out that the C++ compiler for AVR uCs doesn’t support the new and delete operators, but also that there is a quick fix:
void * operator new(size_t size)
{
return malloc(size);
}
void operator delete(void * ptr)
{
free(ptr);
}
I’m assuming that it would now be possible to call new ClassName(args);.
However, I am not really sure how this works. For example, what actually returns a size_t here? I thought that constructors don’t return anything…
Could it be that new is now supposed to be used differently (in conjunction with sizeof())?
new T(args);is roughly equivalent to the following.(Here
call_constructoris supposed to call the constructor† ofTmakingstoragebe thethispointer within that constructor.)The
operator newpart obtains the requested amount of raw storage, and the constructor call is the one that actually makes an object, by invoking the constructor.The code in the question only replaces the
operator newpart, i.e. the retrieval of storage. Both thesizeofpart and the constructor invocation are done automatically by the compiler when you usenew T(args).† The language has a way to express this direct constructor invocation called “placement
new“, but I omitted it for clarity.