undefined reference to operator delete(void*, unsigned int)

Hello everyone!

  1. I switched from avr-gcc-7.2.0 to avr-gcc-7.3.0 ( IDE 1.8.10) recently
  2. Set -std=gnu++1z in CPP compiler options in platform.txt to avoid warnings about inline variables and so on.

Got compilation failed with " ... undefined reference to operator delete(void*, unsigned int) " error
Yes, I'm using virtual destructors.
Fixed it by adding delete(void * ptr, unsigned int) and delete(void * ptr, unsigned int) into new.cpp

Still curious, what this second parameter(unsigned int) was for?

I found some answers in ISO-IEC_14882-2017 but still uncertain if there is no risk of memory leak.

6 . 7. 4. 2 Deallocat ion funct ions [basic. stc.dynamic.deallocat ion]
1 Deallocat ion funct ions shall b e class member funct ions or global funct ions ; a program is ill- formed if
deallocat ion funct ions are declared in a namespace scop e other than global scop e or declared stat ic in global
scop e .
2 Each deallocat ion funct ion shall return void and it s first parameter shall b e void*. A deallocat ion funct ion
may have more than one parameter . A usual deallocation function is a deallocat ion funct ion that has :
(2 . 1 ) — exact ly one parameter ; or
(2 . 2 ) — exact ly two parameters , the typ e of the second b eing either s td: : align_val_t or st d: : s ize_t 39 ; or
(2 . 3 ) — exact ly three parameters , the typ e of the second b eing st d: : s ize_t and the typ e of the third b eing
st d: : align_val_t .


Homebrewed deallocators are following, got in from arduino.ru, still anxious if they are safe enough.

void operator delete(void * ptr, size_t size){
free(ptr);
}

void operator delete(void * ptr, size_t size){
free(ptr);
}

This what use: Arduino-Helpers/new.cpp at master · tttapa/Arduino-Helpers · GitHub

This is what cppreference has to say about the sized deallocators:

Called instead of (1-2) if a user-defined replacement is provided, except that it's unspecified whether (1-2) or (5-6) is called when deleting objects of incomplete type and arrays of non-class and trivially-destructible class types. A memory allocator can use the given size to be more efficient. The standard library implementations are identical to (1-2).

(1-2) is the normal deallocator operator delete(void *) and (5-6) is the sized deallocator operator delete(void *, size_t).

The size parameter is just for custom allocator efficiency, and the standard library just ignores it, and uses the same implementation.

Just calling free should be fine, as malloc already keeps track of the sizes of the heap blocks.

Pieter