Using delete instead of delete[] for 1 integer

M

Money

If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.
 
J

Jim Langston

Money said:
If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.

Undefined. It may work, it may not. It all depends on how the compiler/OS
is handling new/new[] and delete/delete[]. It may even appear to work and
have side effects you're not aware of. In other words, I wouldn't do it.
 
M

Money

Jim said:
Money said:
If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.

Undefined. It may work, it may not. It all depends on how the compiler/OS
is handling new/new[] and delete/delete[]. It may even appear to work and
have side effects you're not aware of. In other words, I wouldn't do it.

But why would it be undefined?
delete ptr; will release memory for atleast 1 integer and that's what I
want.
 
G

Greg

Money said:
Jim said:
Money said:
If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.

Undefined. It may work, it may not. It all depends on how the compiler/OS
is handling new/new[] and delete/delete[]. It may even appear to work and
have side effects you're not aware of. In other words, I wouldn't do it.

But why would it be undefined?
delete ptr; will release memory for atleast 1 integer and that's what I
want.

One reason is that arrays may be allocated differently than objects.
For example, the runtime may store information about an allocation
before the start of the block - and conceivably the format of this
information could differ for an array allocation than an allocation of
a non-array, single object. (Note that the size of the array makes no
difference to the format selected, all arrays of any size are treated
alike).

So if the form of (delete or delete[]) when deleting an array or object
does not match the form of the new operator (new or new[]) that was
used to allocate it, then runtime would interpret the block's stored
information incorrectly - by assuming the data is stored in a format
that is not the same as the format it was stored in - and whatever
happens after the point is uncertain (that is, undefined), and is
unlikely to be good.

Greg
 
B

Bo Persson

Money said:
Jim said:
Money said:
If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.

Undefined. It may work, it may not. It all depends on how the
compiler/OS is handling new/new[] and delete/delete[]. It may
even appear to work and have side effects you're not aware of. In
other words, I wouldn't do it.

But why would it be undefined?
delete ptr; will release memory for atleast 1 integer and that's
what I want.


Because the standard says so. :)

The compiler is allowed to make it NOT work if it feels like. Or some other
compiler you try, or the next release.

Just don't do it!


Why would you use new to allocate a single int anyway?


Bo Persson
 
T

terminator

Jim said:
Money said:
If I allocate memory like this
int *ptr = new int[1];
Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.
Undefined. It may work, it may not. It all depends on how the compiler/OS
is handling new/new[] and delete/delete[]. It may even appear to work and
have side effects you're not aware of. In other words, I wouldn't do it.But why would it be undefined?
delete ptr; will release memory for atleast 1 integer and that's what I
want.
C++,s new/new[] and delete/delete[] are overloadable operators they
might be instructed to place arrays on different heap than single
objects.So your program
might simplly crash.You are programing with high risk.
 
S

stork

Money said:
If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.

It may or may not work, depending on the implementation, and even the
version. But, it would not be portable code. You should ALWAYS use
delete [] to delete something that was allocated with new [].
 
G

Glen Dayton

Money said:
If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.

For Plain Old Datatype (POD) like an int, it likely not to
matter. delete[] also destructs each of the elements of the
array. If you use 'delete', you'll bypass destructing the
elements of the array.

Also, naked pointers are obscene. Never declare a unwrapped int
*. Wrap it with a boost:scoped_array<> or boost:shared_array<>.
This way you won't need to worry about whether to call
delete[] or delete. Make the object take care of itself.

/Glen Dayton
 
S

Salt_Peter

Money said:
If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.

You can't because its not guaranteed. Although on most compilers the
above might work you still are left with the uncertainty that it may
fail. That is, an implementation of a primitive array is not required
to make the above delete correctly (what the specific details of such
an implementation might be doesn't matter). You've got other things to
worry about.

Whats relevent to you is the fact that if one day somebody decides that
they need new int[2] instead of new int[1], your deletion should still
work. And it will if you follow the standard, delete[] ptr is
guarenteed.
 
A

Andre Kostur

Money said:
If I allocate memory like this

int *ptr = new int[1];

Can I apply delete ptr; instead of delete[] ptr; since I am only
allocating memory for 1 integer.

For Plain Old Datatype (POD) like an int, it likely not to
matter. delete[] also destructs each of the elements of the
array. If you use 'delete', you'll bypass destructing the
elements of the array.

Not necessarily true. An array may be allocated differently than a
single object (and we're not talking about construction/destruction).

I could see one potential implementation as follows:

- When allocating memory for a single object, allocate sizeof(int) +
sizeof(object). Write the size of the object in the first sizeof(int)
bytes, invoke the constructor on the sizeof(object) bytes, return a
pointer to the object. (Perhaps there's customized allocators for
different-sized objects)

- When allocating memory for an array of objects, allocate sizeof(int) +
sizeof(int) + arraysize * sizeof(object). Write the arraysize in the
first sizeof(int) bytes, write sizeof(object) in the second sizeof(int)
bytes, invoke the constructor on each of the sizeof(object) bytes.
Return a pointer to the first object.

- When using delete, invoke the destructor on the sizeof(object) bytes
starting at the pointer, back up sizeof(int) bytes, hand this pointer
back to wherever the memory came from (perhaps some global
allocator/deallocator)

- When using delete[], back up sizeof(int) + sizeof(int). Copy out the
first sizeof(int) as the number of objects, copy the 2nd sizeof(int) for
the object size. Loop over each of the sizeof(object) bytes (starting
from the _end_ of the array) and invoke the destructor on each object in
turn. Finally hand the pointer - sizeof(int) - sizeof(int) back to
wherever the memory came from.


As a result, if you allocate with:

int * p = new int[1];

You get 12 bytes allocated, starting at p - 8. (Let's assume no padding
and sizeof(int) == 4)

When you call:

delete p;

You end up attempting to hand p - 4 back to the OS. Since it didn't
allocate that pointer, who knows what it's going to do. And in the
theoretical case where different-sized allocations come from different
pools, this memory block would be handed back to the wrong pool too (say,
the 8-byte allocator instead of the 12-byte allocator).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top