I want to delete a dynamically allocated array by going through all the elements and calling delete for each of them.
(I do this because I need to "move" the array to another location, that is, copy the original array and then delete it, but it will take 2x time than copying each element at once and calling delete on them individually)
I have the following code:
int main() { int *n=new int[2]; delete n; delete (n+1); }
But I get a segmentation error every time I run this ....
Although this works great -:
int main() { int *n=new int[1]; delete n; }
So, I assume that delete somehow deletes the whole array, not just one element!
Can someone explain if I guess correctly, and if so, suggest a possible workaround?
I am using GCC 4.7.3 on Ubuntu 13.04
c ++ memory-management pointers dynamic-arrays
Anmol singh jaggi
source share