0

What is the difference between doing:

int* I = new int[100];
for (int J = 0; J < 100; ++J)
{
    delete I++;
}

//and

int* I = new int[100];
delete[] I;

I know that the first is wrong. I know how to use delete[] vs. delete correctly. I just want to know why these are any different. Like figure out the real difference between delete[] and delete in a loop. So what is the difference?

Brandon
  • 22,723
  • 11
  • 93
  • 186

7 Answers7

3

Both versions of new and delete each have two tasks: allocation/deallocation and construction/destruction.

  • new will allocate memory and call a constructor.

  • delete will call a deconstructor and deallocate memory.

  • new [] allocates single chunk of memory and then calls a constructor possibly several times.

  • delete [] calls a deconstructor possibly several times and then deallocates a single chunk of memory.

So using delete multiple times means deallocating multiple chunks of memory whereas using delete[] will deallocate a single chunk of memory; using delete multiple times is not equivalent to using delete [].

bames53
  • 86,085
  • 15
  • 179
  • 244
2

The difference is that in the first, you're deleting pointers that you didn't get back from new.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
2

There's no point of comparision

Use deletes for all news

and delete []s for all new []s

The first one simply deletes pointer not coming from new

P0W
  • 46,614
  • 9
  • 72
  • 119
2

When you use new Foo[n], you're making a single allocation for a chunk of memory big enough to hold an array of n contiguous elements of type Foo. This is not the same as allocating n contiguous chunks of memory, one for each Foo.

From the point of view of the memory allocator, it's really only one big allocation. When you do delete array or delete (array + 42), the memory allocator is basically asked to delete the part of the big allocation that holds a specific item, which it cannot do. It's like trying to free up a single member of a new'ed object by doing delete (&(new Foo())->bar) - what happens to the rest of the object?

Even on a single-element array, delete array will not work because the allocator uses different bookkeeping logic for arrays and single objects (for example, storing the number of elements in the array). So you really do have to use delete[] with new[] and delete with new.

Trillian
  • 6,207
  • 1
  • 26
  • 36
0

This is declaring an array of integers:

int* I = new int[100];

This is iterating through an array of integers and trying to delete them:

for (int J = 0; J < 100; ++J)
{
    delete I++; // bad
}

This is deleting the array of integers:

delete [] I; // correct

Since you allocated the array with [], you deallocate it with []. You do not deallocate memory you allocated with [] without [].

Zac Howland
  • 15,777
  • 1
  • 26
  • 42
0

The difference between delete and delete[] is that delete will invoke the destructor of one object, while delete[] will invoke the destructor of every object in the array. In the case of ints, the difference isn't noticable, but if you anything of consequence in your destructor, you'll problems since you won't be properly destroying all of your objects.

That said, you still shouldn't use delete instead of delete[] for simple types since the compiled code may be different for the two operators (for example, delete[] may be expecting an integer to be stored somewhere adjacent to the array to indicate the number of objects to delete). So, the general rule is if you used new, always use delete, and if you used new[], always use delete[].

Darius Makaitis
  • 880
  • 1
  • 5
  • 5
  • Well, the difference here *is* noticeable: calling `delete` on every element in the array will result in 99 invalid `free`s. – greyfade Sep 20 '13 at 18:35
  • Correct. I meant only that the "destructor" for an int doesn't really do anything, and wasn't really addressing the issue of properly deallocating the memory. – Darius Makaitis Sep 20 '13 at 21:07
0

The first example yields undefined behavior, because a plain delete expression (as opposed to delete[]) can only be applied to an operand that is either:

  • a null pointer value
  • a pointer to a non-array object created by a previous new-expression
  • or a pointer to a subobject

Calling delete on an individual element of an array allocated with new[] does not fall in either of these categories, because the elements of your array are non-array objects that have NOT been created with an individual new expression.

(C++ standard 5.3.5)