Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ delete[] operator [duplicate]

Is this the right way to use delete[] operator?

int* a=new int[size];
delete[] a;

If yes, Who (compiler or GC or whoever) will determine the size of the newly created array? and where will it store the array size?

Thanks

like image 495
Betamoo Avatar asked Apr 06 '26 10:04

Betamoo


1 Answers

For each chunk of memory allocated, the memory allocator stores the size of the chunk (that's why it is inefficient to allocate many small blocks compared to one big one for example). When delete frees the memory, the allocator knows how large the memory chunk is the pointer points to.

like image 65
Gene Vincent Avatar answered Apr 09 '26 00:04

Gene Vincent