Is it possible to delete a C POD using delete in C ++?

Having structures like

struct ifoo_version_42 {
   int x, y, z;
   char *imageData;
};

where imageDatais something likeimageData = new char[50000];

Can we do something like:

template< typename T >
void del( T a ) // we promise to use this only on C Plain Old data structs=)
{
  delete a;
}

in this structure will be enough to clear the shape of the memory if?

+3
source share
3 answers

Deleting a structure does not recursively delete any pointers in it and therefore does not free the char array that it points to imageData.

delete[]. ( new[]) delete[] ( new) delete. , , ifoo_version_42. , undefined:

ifoo_version_42 *x = new ifoo_version_42;
del(x);

:

ifoo_version_42 *x = new ifoo_version_42[1];
del(x);
+5

"" -POD-. delete[] a;.

, imageData. , .

+5

del ifoo_version_42, , data, ; delete, delete[] .

delete[] arrays; , imageData new[], delete[].

delete : , , ifoo_version_42 new, delete.

( , delete - malloc() free() -, new.)

: RAII , STL Boost; .

+3

Source: https://habr.com/ru/post/1787278/


All Articles