3

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())?

corazza
  • 31,222
  • 37
  • 115
  • 186
  • don't confuse new and operator new. Yes it's slightly tricky. operator new which you are overloading just allocates memory. You can use malloc or a special memory pool and you can decorate it too to help with memory leak detection and invalid deletes etc. – CashCow Jan 09 '13 at 12:31
  • Does this mean that it doesn't actually instantiate the class at all or what? I'm a bit confused by what you said. – corazza Jan 09 '13 at 12:34
  • always keep in mind that the operator new (that you can overload) and the new expression (that you normally use to create new objects in code) are two different things. – PlasmaHH Jan 09 '13 at 12:39
  • @PlasmaHH, OK, I didn't know that. Hopefully the compiler supports the new expression. – corazza Jan 09 '13 at 12:41
  • 1
    @Yannbane: If it does not, it is not a C++ compiler. – PlasmaHH Jan 09 '13 at 12:51
  • In http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=59453&start=all&postdays=0&postorder=asc you can found the answer. – Mihai8 Jan 09 '13 at 12:53

2 Answers2

9

new T(args); is roughly equivalent to the following.

void* storage = operator new(sizeof(T)); // obtain raw storage
call_constructor<T>(storage, args); // make an object in it

(Here call_constructor is supposed to call the constructor of T making storage be the this pointer within that constructor.)

The operator new part 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 new part, i.e. the retrieval of storage. Both the sizeof part and the constructor invocation are done automatically by the compiler when you use new T(args).


† The language has a way to express this direct constructor invocation called "placement new", but I omitted it for clarity.

R. Martinho Fernandes
  • 228,013
  • 71
  • 433
  • 510
0

From the compiler name (uC), I presume it's for embedded controller. This would make sense as you rarely require dynamic memory management with embedded devices, but might benefit from 'C with classes'. Hopefully it supports 'placement new' so you can actually use C++.

If your compiler doesn't support new & delete, it's not really much of a a C++ compiler is it!

I think the keyword 'new' effectively gets converted to:

Object* pointer = (Object *)new(sizeof Object);
pointer->Object_Constructor(args);
Neil
  • 11,059
  • 3
  • 31
  • 56