delete [ ] fp qn

M

mescaline

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
 
J

Jeff Schwab

mescaline said:
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.
 
R

Ron Natalie

... 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.
 
M

Martijn Lievaart

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.
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
 
U

Unforgiven

mescaline said:
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[].
 
R

Ron Natalie

Unforgiven said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top