C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

delete [ ] fp qn

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
mescaline
Guest





PostPosted: Mon Dec 29, 2003 2:44 pm    Post subject: delete [ ] fp qn Reply with quote



Hi,

suppose I do:

class One{};

int main(){
One * A = new One[100];
// an array of objects on the heap ...
delete A; // instead of delete []A;
return 0;
}

.... I read in Eckel, that "this releases the proper amount of storage"
-- How?

Also, then there is no memory leak, right?
What are the disadvantages of doing the above then instead of "delete []A;"?

thanks
m
Back to top
Jeff Schwab
Guest





PostPosted: Mon Dec 29, 2003 2:45 pm    Post subject: Re: delete [ ] fp qn Reply with quote



mescaline wrote:
Quote:
Hi,

suppose I do:

class One{};

int main(){
One * A = new One[100];
// an array of objects on the heap ...
delete A; // instead of delete []A;
return 0;
}

... I read in Eckel, that "this releases the proper amount of storage"
-- How?

Also, then there is no memory leak, right?
What are the disadvantages of doing the above then instead of "delete []A;"?

thanks
m


Use delete[]. I don't know why Eckel would say differently.



Back to top
Ron Natalie
Guest





PostPosted: Mon Dec 29, 2003 2:52 pm    Post subject: Re: delete [ ] fp qn Reply with quote





Quote:
... I read in Eckel, that "this releases the proper amount of storage"
-- How?

Also, then there is no memory leak, right?
What are the disadvantages of doing the above then instead of "delete []A;"?

The disadvantage is that it is undefined behavior. If Eckel said that, he's wrong.

There's no guarantee that anything specific happens. It's quite possible you'll
hose the allocators.


Back to top
Martijn Lievaart
Guest





PostPosted: Mon Dec 29, 2003 3:41 pm    Post subject: Re: delete [ ] fp qn Reply with quote

On Mon, 29 Dec 2003 06:44:36 -0800, mescaline wrote:

Quote:
Hi,

suppose I do:

class One{};

int main(){
One * A = new One[100];
// an array of objects on the heap ...
delete A; // instead of delete []A;
return 0;
}

... I read in Eckel, that "this releases the proper amount of storage"
-- How?

I haven't read Eckel, but given how highly regarded he is, I doubt he
wrote that, or not in this context.

Quote:
Also, then there is no memory leak, right? What are the disadvantages of
doing the above then instead of "delete []A;"?

It invokes undefined behaviour. Anything may happen. Anything may even
include works, seems to work but it might also blow up.

HTH,
M4


Back to top
Unforgiven
Guest





PostPosted: Mon Dec 29, 2003 4:12 pm    Post subject: Re: delete [ ] fp qn Reply with quote

mescaline wrote:
Quote:
Hi,

suppose I do:

class One{};

int main(){
One * A = new One[100];
// an array of objects on the heap ...
delete A; // instead of delete []A;
return 0;
}

... I read in Eckel, that "this releases the proper amount of storage"
-- How?

Also, then there is no memory leak, right?
What are the disadvantages of doing the above then instead of "delete
[]A;"?

If you say:
One *B = new One;
Memory is allocated and the constructor is called.
If you say:
One *A = new One[100];
Memory is allocated and 100 constructors are called.

Similarly if you say:
delete[] A;
All 100 destructors are called and memory is deallocated
But if you say:
delete A;
Only 1 destructor is called and memory is deallocated. While at the very
least all compilers I know (I'm not sure if the standard has anything to say
about this) will deallocate all memory, only the first of the in this case
100 objects is properly destructed. If the other 99 objects manage any
resources (like memory) that rely on the destructor to be released, those
will leak if you don't use delete[].

--
Unforgiven
"Most people make generalisations"
Freek de Jonge


Back to top
Ron Natalie
Guest





PostPosted: Mon Dec 29, 2003 4:16 pm    Post subject: Re: delete [ ] fp qn Reply with quote


"Unforgiven" <jaapd3000 (AT) hotmail (DOT) com> wrote


Quote:
But if you say:
delete A;
Only 1 destructor is called and memory is deallocated.

Nope, it's undefined behavior. It's quite possible that the memory
doesn't get deallocated properly either.

It's worse than a leak.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.