tags:

views:

762

answers:

5

When I compiled a code using the array name as a pointer, and I deleted the array name using delete, I got a warning about deleting an array without using the array form (I don't remember the exact wording).

the basic code was:

int data[5];
delete data;

so whats the array form of delete?

+20  A: 

The array form of delete is:

delete [] data;

Edit: But as others have pointed out, you shouldn't be calling delete for data defined like this:

int data[5];

You should only call it when you allocate the memory using new like this:

int *data = new int[5];
RichieHindle
But his int data[5] was not allocated by operator new(), so there is no correct form of operator delete() that can be called.
RBerteig
Interesting... the answer is correct, but oh so wrong. How did he get 17 votes already?
Jared Oberhaus
It is so easy to answer the question actually asked, instead of the question that should have been asked. Especially when its repeated both in the title and as the last sentence.
RBerteig
Jared - confining myself only to your question, and not the question of whether this answer is worthy or not, perhaps it's the case that answers marked as the accepted answer tend to attract more votes. It'd be interesting to see if the SO folks do any statistical analysis on the rates of voting for all answers before and after one is marked accepted.
JamieH
A: 

Just as RichieHindle stated above when you want to free the space dynamically allocated for an array pointed by data you have to put two brackets [] between the reserved word delete and the pointer to the beginning of the allocated space. Since data can point to a single int in memory as well as to the first element in the array this is the only way you let the compiler know that you want to delete the whole chunk of memory. If you don't do it the proper way the behaviour is "undetermined" (Stroustrup, The C++ Programming Language).

Sergio
+2  A: 

The code as shown has the array either on the stack, or in initialized part of the data segment, i.e. you don't deallocate it (which, as mentioned by others, would be "undefined behavior".) Were it on the "free store", you'd do that with delete [] data.

Nikolai N Fetissov
+8  A: 

You either want:

int *data = new int[5];
... // time passes, stuff happens to data[]
delete[] data;

or

int data[5];
... // time passes, stuff happens to data[]
// note no delete of data

The genera rule is: only apply delete to memory that came from new. If the array form of new was used, then you must use the array form of delete to match. If placement new was used, then you either never call delete at all, or use a matching placement delete.

Since the variable int data[5] is a statically allocated array, it cannot be passed to any form of the delete operator.

RBerteig
+2  A: 

As the other have said, you must use the vector form of delete:

void some_func(size_t n)
{
  int* data = new int[n];

  . . . // do stuff with the array

  delete [] data; // Explicitly free memory
}

Be very wary of this, because some compilers will not warn you.

Even better, there is very rarely any need for using vector new/delete. Consider whether your code can be altered to make use of std::vector:

void some_func(size_t n)
{
  std::vector<int> data(n);

  . . . // do stuff with the array

} // memory held by data will be freed here automatically

And if you are dealing with the memory in a local scope, consider using STLSoft's auto_buffer, which will allocate from an internal buffer (held on the stack, as part of the instance) if possible, only going to the heap if it cannot:

void some_func(size_t n)
{
  stlsoft::auto_buffer<int, 10> data(n); // only allocates if n > 10

  . . . // do stuff with the array

} // memory held by data will be freed here automatically, if any was allocated

Read more about auto_buffer.

DannyT