user98188
user98188

Reputation:

What is the array form of 'delete'?

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, what's the array form of delete?

Upvotes: 31

Views: 59121

Answers (6)

hot3dx
hot3dx

Reputation: 11

Fixes C4154 and C4156 warnings

float AR[5] = { 1.0f, 2.0f, ..., ..., ...};

delete [] & AR;

Upvotes: 0

DannyT
DannyT

Reputation: 14299

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.

Upvotes: 5

RichieHindle
RichieHindle

Reputation: 281415

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];

Upvotes: 67

RBerteig
RBerteig

Reputation: 43296

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.

Upvotes: 23

Nikolai Fetissov
Nikolai Fetissov

Reputation: 84159

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.

Upvotes: 2

Sergio
Sergio

Reputation: 4567

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).

Upvotes: 0

Related Questions