Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Using delete instead of delete[] for 1 integer

Reply
Thread Tools

Using delete instead of delete[] for 1 integer

 
 
Money
Guest
Posts: n/a
 
      12-24-2006
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.

 
Reply With Quote
 
 
 
 
Jim Langston
Guest
Posts: n/a
 
      12-24-2006
"Money" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
 
Money
Guest
Posts: n/a
 
      12-24-2006

Jim Langston wrote:
> "Money" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) 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.

 
Reply With Quote
 
Greg
Guest
Posts: n/a
 
      12-24-2006

Money wrote:
> Jim Langston wrote:
> > "Money" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) 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

 
Reply With Quote
 
Bo Persson
Guest
Posts: n/a
 
      12-24-2006
Money wrote:
> Jim Langston wrote:
>> "Money" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed) 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.



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


 
Reply With Quote
 
terminator
Guest
Posts: n/a
 
      12-24-2006


On Dec 24, 2:04 pm, "Money" <(E-Mail Removed)> wrote:
> Jim Langston wrote:
> > "Money" <(E-Mail Removed)> wrote in message
> >news:(E-Mail Removed) 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.

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.

 
Reply With Quote
 
stork
Guest
Posts: n/a
 
      12-24-2006

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 [].

 
Reply With Quote
 
Glen Dayton
Guest
Posts: n/a
 
      12-24-2006
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
 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      12-24-2006

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.

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      12-24-2006
Glen Dayton <(E-Mail Removed)> wrote 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).

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Overloaded global delete is used instead of orignal global delete[] Alex Vinokur C++ 3 06-15-2012 05:12 PM
How to make integer to store 01 instead of 1 ? gopesh patel C++ 5 09-16-2010 12:53 PM
Using Constrained Integer instead of SLV rickman VHDL 14 05-15-2008 02:59 AM
Overwriting the Integer class for method succ! (instead of just succ) paul Ruby 10 11-10-2006 07:29 PM
check to see if value can be an integer instead of string nephish@xit.net Python 11 02-06-2006 05:04 PM



Advertisments