0

Suppose a block is allocated on the heap to hold an array of integers. While de-allocating, should I use the delete operator once on the entire block or once on each byte? As an example, consider

int *ptr=new int[n]; //n is a natural number 
.....
delete[] ptr;   //Is this the right approach? or 
for(unsigned int i=0;i<n;i++) delete[] (ptr+i); //this one? 

To me, the second approach seems more natural. Otherwise, when passed an address, how does the delete operator know how far along the line should it de-allocate? Or does it just continue to de-allocate as far as it can see an allocated byte?

Shreyash S Sarnayak
  • 2,231
  • 19
  • 23
Della
  • 1,043
  • 2
  • 14
  • 27

0 Answers0