| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"VSP" |
| Date: |
17 Aug 2007 02:06:33 AM |
| Object: |
Reg. delete of an array of objects |
Hi,
I have a basic doubt.
Test *t = new Test[10];
....
delete t;
Will the "delete t" deallocates the memory for all the 10 Test objects or it
just deallocates for the first Test object?
In some article I have read, it deallocates the memory for all the 10 Test
objects and the destructor will be called for the first Test object. Is it
correct?
In case of Primitive types, is the behavior same or different?
Regards,
VSP
.
|
|
| User: "Ondra Holub" |
|
| Title: Re: Reg. delete of an array of objects |
17 Aug 2007 02:13:54 AM |
|
|
On 17 Srp, 09:06, "VSP" <a...@abc.com> wrote:
Hi,
I have a basic doubt.
Test *t = new Test[10];
...
delete t;
Will the "delete t" deallocates the memory for all the 10 Test objects or it
just deallocates for the first Test object?
In some article I have read, it deallocates the memory for all the 10 Test
objects and the destructor will be called for the first Test object. Is it
correct?
In case of Primitive types, is the behavior same or different?
Regards,
VSP
Hi.
Use of delete for data allocated with new[] is wrong. You cannot
assume any behaviour, since it is error. It may work for particular
compiler, but it would be non-portable program.
Ondrej
.
|
|
|
|
| User: "Obnoxious User" |
|
| Title: Re: Reg. delete of an array of objects |
17 Aug 2007 02:00:03 AM |
|
|
On Fri, 17 Aug 2007 12:36:33 +0530, VSP wrote:
Hi,
I have a basic doubt.
Test *t = new Test[10];
...
delete t;
delete[] t;
Will the "delete t" deallocates the memory for all the 10 Test objects or it
just deallocates for the first Test object?
In some article I have read, it deallocates the memory for all the 10 Test
objects and the destructor will be called for the first Test object. Is it
correct?
In case of Primitive types, is the behavior same or different?
You should always match 'new' to 'delete', and 'new[]' to 'delete[]'.
--
Obnoxious User
.
|
|
|
|
| User: "Robert Bauck Hamar" |
|
| Title: Re: Reg. delete of an array of objects |
17 Aug 2007 02:15:34 AM |
|
|
VSP wrote:
Hi,
I have a basic doubt.
Test *t = new Test[10];
...
delete t;
Will the "delete t" deallocates the memory for all the 10 Test objects or
it just deallocates for the first Test object?
Actually, it's undefined behaviour, so anything could happen.
In some article I have read, it deallocates the memory for all the 10 Test
objects and the destructor will be called for the first Test object. Is it
correct?
For some compiler, it might do. On other compilers, it might crash your
system.
In case of Primitive types, is the behavior same or different?
The behaviour is undefined in both cases. Use 'delete [] t' as your
teacher/book/... hopefully told you.
--
rbh
.
|
|
|
|

|
Related Articles |
|
|