Consider following code:
unsigned char* a = new unsigned char[100];
void* a1 = reinterpret_cast<void*>(a);
operator delete[](a1);
short* b = new short[100];
void* b1 = reinterpret_cast<void*>(b);
operator delete[](b1);
It seems to me to be a valid syntax for scalar types as delete-expression is equivalent to calling destructor and operator delete. Same for a POD type:
struct POD {
int i;
float f;
char c;
};
POD* c = new POD[100];
void* c1 = reinterpret_cast<void*>(c);
operator delete[](c1);
Another thing with scalar and POD types allocated using operator new and deallocated with delete expression:
void* d = operator new[](sizeof(unsigned char) * 100);
unsigned char* d1 = reinterpret_cast<unsigned char*>(d);
delete[] d1;
void* e = operator new[](sizeof(short) * 100);
short* e1 = reinterpret_cast<short*>(e);
delete[] e1;
void* f = operator new[](sizeof(POD) * 100);
POD* f1 = reinterpret_cast<POD*>(f);
delete[] f1;
All these examples seem to be well-formed, but I didn't manage to find if it really is. Can someone confirm it or tell me where I am wrong? Especially I am concerned by the fact that all these types have different alignment and it should be passed to operator new/delete:
alignof(unsigned char) 1
alignof(short) 2
alignof(POD) 4
UPD. Some seem to confuse this question with Is it safe to delete a void pointer?. I am asking about mixing expression new
with operator delete
and operator new
with expression delete
. According to standard expression forms are implemented with operator forms. And the question is how valid would be mixing them for scalar and POD types?