| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"SpreadTooThin" |
| Date: |
30 Mar 2007 12:11:43 PM |
| Object: |
delete and delete [] |
I understand that if you allocate an array using new as follows
object * b = new object[n];
then you must free the object as follows:
delete [] b;
however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)
.
|
|
| User: "Mike Wahler" |
|
| Title: Re: delete and delete [] |
30 Mar 2007 12:18:29 PM |
|
|
"SpreadTooThin" <bjobrien62@gmail.com> wrote in message
news:1175274703.360909.214000@p77g2000hsh.googlegroups.com...
I understand that if you allocate an array using new as follows
object * b = new object[n];
then you must free the object as follows:
delete [] b;
Correct.
however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?
No, you must still use delete[]. It's still an
array (albeit whose objects are of a different type).
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)
The array's element type has no bearing on this.
If you use 'new', you must deallocate with 'delete'.
If you use 'new[]', you must deallocate with 'delete[]'.
Simple, huh?
-Mike
.
|
|
|
|
| User: "Gavin Deane" |
|
| Title: Re: delete and delete [] |
30 Mar 2007 12:29:30 PM |
|
|
On 30 Mar, 18:11, "SpreadTooThin" <bjobrie...@gmail.com> wrote:
I understand that if you allocate an array using new as follows
object * b = new object[n];
then you must free the object as follows:
delete [] b;
however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?
Who told you that? It certainly is relevant. The behaviour of mixing
new [] with delete is undefined.
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)
As ever with undefined behaviour, whether you have issues, what those
issues are, whether they are different for different types etc. is
outside the scope of the language definition.
Gavin Deane
.
|
|
|
|
| User: "" |
|
| Title: Re: delete and delete [] |
30 Mar 2007 12:19:19 PM |
|
|
On Mar 30, 1:11 pm, "SpreadTooThin" <bjobrie...@gmail.com> wrote:
however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?
No, not irrelevant. If you allocate with [], delete with [].
.
|
|
|
|
| User: "James Curran" |
|
| Title: Re: delete and delete [] |
30 Mar 2007 03:47:03 PM |
|
|
On Mar 30, 1:11 pm, "SpreadTooThin" <bjobrie...@gmail.com> wrote:
however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?
You seem to be misunderstanding what delete & delete[] do.
basically, they functions like:
delete pObject; =
pObject->~object();
free_n_bytes_of_memory(pObject, sizeof(object));
delete[] arrObject; =
for(int i =0; i < N; ++i)
arrObject[i].~object();
free_n_bytes_of_memory(arrObject, sizeof(object)* N );
Nw, you are correct in surmizing that, as a char dtor is empty, the
different in the first part of each is irrelevent. However, the
difference in the second half is still significant.
To further confuse the issue, on most platforms,
free_n_bytes_of_memory could probably be implemented as:
void free_n_bytes_of_memory(void* ptr, int )
{
free(ptr);
}
with the size parameter ignored, but that would be an implementation
detail, which you can't and shouldn't depend on.
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: delete and delete [] |
30 Mar 2007 12:19:34 PM |
|
|
SpreadTooThin wrote:
I understand that if you allocate an array using new as follows
object * b = new object[n];
then you must free the object as follows:
delete [] b;
however if you are allocating a char buffer as follows:
char * b = new char [n];
then isn't it irrelavant if you use
delete b or delete [] b ?
No, of course not. Why do you think there might be a difference?
It's an array. You use 'new[]' to allocate it, you use 'delete[]'
to free it.
(Then only thing I can think of is that if you change the object type
from char to some other object then you might have some issues...)
No, you can't change the object type. That's nonsense, sorry.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
|

|
Related Articles |
|
|