8

I want to delete a dynamically-allocated array by looping through all the elements and calling delete on each of them.
(I am doing this because I have to "move" an array to another location, that is, to copy the original array and then delete it, but that would take 2x the time than simultaneously copying each element 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 am getting a Segmentation error each time i run this....

Although, this works fine -:

int main()
{
 int *n=new int[1];
 delete n;
} 

So, my guess is that delete is somehow deleting the whole array instead of a single element!

Could anyone please explain if my guess is right, and if it is, suggest a possible workaround?

I am using GCC 4.7.3 on Ubuntu 13.04

Unihedron
11.1k13 gold badges65 silver badges72 bronze badges
asked Aug 2, 2013 at 11:55
6
  • The most simplest way to do that is using C++ standard containers (std::vector in this case) instead of dynamic arrays, and simply use the erase method of the container. But, on the other hand, what are you trying to do? Why you want to delete elements one-by-one? Commented Aug 2, 2013 at 12:13
  • Actually, I was trying to make another vector-like container of my own. [ Just to gain a better understanding of how std::vectors work under the hood ] Commented Aug 2, 2013 at 12:15
  • @Ammol to learn C++? ok. So your question is about the destructor of the vector? This is why you were trying to delete one element by one? Commented Aug 2, 2013 at 12:18
  • Ummm.. kind of! .. As mentioned in the question, I was trying to copy the array to another location and deallocate the original array... [ I was trying to mimic std::vector::resize() operation ] Commented Aug 2, 2013 at 12:31
  • Yeah, I did the same thing earlier. Thanks for the help anyways. :) Commented Aug 2, 2013 at 12:42

2 Answers 2

7

You cannot delete the elements individually. When you allocate with new [] you must deallocate with delete []. What you are doing here:

int *n=new int[1];
delete n; // ERROR: should be delete []

it not correct. You are invoking undefined behaviour. It seems to work by pure chance and cannot be relied on.

As for a workaround, it isn't clear what the problem is, but if you want to "move" the array, you could just assign it to a different pointer:

int* n=new int[2];
...
int* m = n;
n = nullptr;
....
delete [] m;
answered Aug 2, 2013 at 11:56
3
  • Yes, but what was wrong in delete (n+1)? Aren't the individual memory blocks allocated with new only??! Commented Aug 2, 2013 at 12:00
  • @AnmolSinghJaggi It is not allowed by the C++ standard. Commented Aug 2, 2013 at 12:01
  • 3
    @AnmolSinghJaggi the point is that new [] allocates a whole array. It doesn't make sense to de-allocate single elements of the array. What could the OS possibly do with them? Commented Aug 2, 2013 at 12:10
1

To delete

int *n=new int[2];

use

delete [] n;

the code

delete n;

is wrong when you allocate using new [].

answered Aug 2, 2013 at 12:01

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.