Project Home
Project Home
Documents
Documents
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - C++: delete vs. delete[]: (3 Items)
   
C++: delete vs. delete[]  
Hi there,
I have a compiler specific question regarding delete[](). 

Sample code:
 [...] 
 datatype* pType = new datatype;
 [...]
 delete [] pType; 

I observed, that delete[] crashes for QNX6.4.0 on an ARM-CPU. A callstack is at the end of this post.

My question: is there a compiler-internal difference between "new datatype" and "new [1] datatype" and therefore for 
delete, too? 

No question: one should only delete an array if one created an array, too. But I thought that "new datatype" would 
create an "array" with length of 1 and that I could therefor use delete[] by default... The MS compiler (mea culpa) has 
no problem running sample code.

With kind regards,
 Michael

Callstack:
 This GDB was configured as "--host=i386-mingw32msvc --target=arm-unknown-nto-qnx6.4.0"...

 Reading symbols from libc.so.3...done.
 Loaded symbols for libc.so.3 
 Program terminated with signal 11, Segmentation fault.
 [New process 380966 thread 8]
 #0  0x01027938 in __init_flist_bins () from libc.so.3
 (gdb) bt
 #0  0x01027938 in __init_flist_bins () from libc.so.3
 #1  0x01026588 in _list_release () from libc.so.3
 #2  0x01028740 in valloc () from libc.so.3
 #3  0x010288b0 in __prelocked_free () from libc.so.3

RE: C++: delete vs. delete[]  

> -----Original Message-----
> From: michael baudisch [mailto:community-noreply@qnx.com]
> Sent: Monday, January 25, 2010 5:33 AM
> To: ostech-core_os
> Subject: C++: delete vs. delete[]
> 
> Hi there,
> I have a compiler specific question regarding delete[]().
> 
> Sample code:
>  [...]
>  datatype* pType = new datatype;
>  [...]
>  delete [] pType;
> 
> I observed, that delete[] crashes for QNX6.4.0 on an ARM-CPU. A
> callstack is at the end of this post.
> 
> My question: is there a compiler-internal difference between "new
> datatype" and "new [1] datatype" and therefore for delete, too?

Sure there is, one is an object the other is an array of object.

> 
> No question: one should only delete an array if one created an array,
> too. But I thought that "new datatype" would create an "array" with
> length of 1 and that I could therefor use delete[] by default... 

You though wrong.

>The MS compiler (mea culpa) has no problem running sample code.

I think the rules says it's undefined behavior.

> 
> With kind regards,
>  Michael
> 
> Callstack:
>  This GDB was configured as "--host=i386-mingw32msvc --target=arm-
> unknown-nto-qnx6.4.0"...
> 
>  Reading symbols from libc.so.3...done.
>  Loaded symbols for libc.so.3
>  Program terminated with signal 11, Segmentation fault.
>  [New process 380966 thread 8]
>  #0  0x01027938 in __init_flist_bins () from libc.so.3
>  (gdb) bt
>  #0  0x01027938 in __init_flist_bins () from libc.so.3
>  #1  0x01026588 in _list_release () from libc.so.3
>  #2  0x01028740 in valloc () from libc.so.3
>  #3  0x010288b0 in __prelocked_free () from libc.so.3
> 
> 
> 
> 
> 
> _______________________________________________
> 
> OSTech
> http://community.qnx.com/sf/go/post45841
> 
Re: C++: delete vs. delete[]  
On Mon, 2010-01-25 at 05:32 -0500, michael baudisch wrote:
> My question: is there a compiler-internal difference between "new
> datatype" and "new [1] datatype" and therefore for delete, too? 

Yes, different compilers may handle these differently.

> No question: one should only delete an array if one created an array,
> too. But I thought that "new datatype" would create an "array" with
> length of 1 and that I could therefor use delete[] by default... The
> MS compiler (mea culpa) has no problem running sample code.

The standards does not permit mixing array and non-array form
new/delete.  From a practical standpoint, different compilers may
arrange to call the destructors for array members in a variety of
different ways, not all of which are compatible with the destruction of
a single object.

Regards,
Neil