By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,118 Members | 853 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,118 IT Pros & Developers. It's quick & easy.

Using delete instead of delete[] for 1 integer

P: n/a
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.

Dec 24 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
"Money" <sp*********@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
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.
Dec 24 '06 #2

P: n/a

Jim Langston wrote:
"Money" <sp*********@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
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.

Dec 24 '06 #3

P: n/a

Money wrote:
Jim Langston wrote:
"Money" <sp*********@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
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

Dec 24 '06 #4

P: n/a
Money wrote:
Jim Langston wrote:
>"Money" <sp*********@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegr oups.com...
>>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
Dec 24 '06 #5

P: n/a


On Dec 24, 2:04 pm, "Money" <spicymon...@gmail.comwrote:
Jim Langston wrote:
"Money" <spicymon...@gmail.comwrote in message
news:11**********************@a3g2000cwd.googlegro ups.com...
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.

Dec 24 '06 #6

P: n/a

Money wrote:
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 [].

Dec 24 '06 #7

P: n/a
Money wrote:
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
Dec 24 '06 #8

P: n/a

Money wrote:
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.

Dec 24 '06 #9

P: n/a
Glen Dayton <RE**********@ieee.orgwrote in news:FnAjh.44$ji1.9
@newssvr12.news.prodigy.net:
Money wrote:
>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).

Dec 24 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.