how C++ telsl the number of element in delete []

P

peter

Hello,

for a C++ delete:

class Test{};
Test * A = new Test[50];
delete [] A;

We don't use delete [50] A;
How can C++ compiler can tell there are 50 destructor to deallocate?

The same if I have an array
char *b = new char[40];
delete [] b;

how C++ compiler can know the right number of 40 to deallocate?

Thanks,

Peter
 
J

Jeff Schwab

peter said:
Hello,

for a C++ delete:

class Test{};
Test * A = new Test[50];
delete [] A;

We don't use delete [50] A;
How can C++ compiler can tell there are 50 destructor to deallocate?

The same if I have an array
char *b = new char[40];
delete [] b;

how C++ compiler can know the right number of 40 to deallocate?

Thanks,

Peter

It usually allocates a little extra space before the array, in which it
puts housekeeping data like the size of the array.

Way back when, you actually did have to cite the number of elements as
part of the delete statement.
 
C

Clark Cox

peter said:
Hello,

for a C++ delete:

class Test{};
Test * A = new Test[50];
delete [] A;

We don't use delete [50] A;
How can C++ compiler can tell there are 50 destructor to deallocate?

The same if I have an array
char *b = new char[40];
delete [] b;

how C++ compiler can know the right number of 40 to deallocate?

Magic. :)

But seriously, the compiler is free to do whatever housekeeping it needs
to do behind your back, as long as it compiles the code in a manner
specified by the standard.
 
D

Default User

peter said:
How can C++ compiler can tell there are 50 destructor to deallocate?


Magic. The runtime system uses some way to keep a count of the number of
bytes associated with a dynamic allocation. When the deallocation is
performed, it uses that number. A typical way is to record that number
preceeding the allocated block. IT IS NOT A NUMBER YOU CAN ACCESS.

So the bottom line is, don't worry about it. As far as you are concerned
it is magic. It just happens, in whatever way it happens.



Brian Rodenborn
 
P

peter

Jeff said:
peter said:
Hello,

for a C++ delete:

class Test{};
Test * A = new Test[50];
delete [] A;

We don't use delete [50] A;
How can C++ compiler can tell there are 50 destructor to deallocate?

The same if I have an array
char *b = new char[40];
delete [] b;

how C++ compiler can know the right number of 40 to deallocate?

Thanks,

Peter

It usually allocates a little extra space before the array, in which it
puts housekeeping data like the size of the array.

It might break all other code.
Way back when, you actually did have to cite the number of elements as
part of the delete statement.

If I don't cite the number of elements, it works fine to call
the deconstructor 50 times for delete [] A.

Thanks,

Peter
 
P

peter

Default said:
peter wrote:





Magic. The runtime system uses some way to keep a count of the number of
bytes associated with a dynamic allocation. When the deallocation is

Not sure where to keep it internally for a pointer. As you said, just
treat it as a magic for C++ compiler to make it work.

Thanks,

Peter
 
J

Jeff Schwab

peter said:
Jeff said:
peter said:
Hello,

for a C++ delete:

class Test{};
Test * A = new Test[50];
delete [] A;

We don't use delete [50] A;
How can C++ compiler can tell there are 50 destructor to deallocate?

The same if I have an array
char *b = new char[40];
delete [] b;

how C++ compiler can know the right number of 40 to deallocate?

Thanks,

Peter

It usually allocates a little extra space before the array, in which
it puts housekeeping data like the size of the array.


It might break all other code.

What, the compiler might? I'm not sure what you mean... It really does
work this way (for many implementations).
Way back when, you actually did have to cite the number of elements as
part of the delete statement.


If I don't cite the number of elements, it works fine to call
the deconstructor 50 times for delete [] A.

That doesn't free the memory, it just deconstructs the objects.
 
O

one2001boy

Jeff said:
peter said:
Jeff said:
peter wrote:

Hello,

for a C++ delete:

class Test{};
Test * A = new Test[50];
delete [] A;

We don't use delete [50] A;
How can C++ compiler can tell there are 50 destructor to deallocate?

The same if I have an array
char *b = new char[40];
delete [] b;

how C++ compiler can know the right number of 40 to deallocate?

Thanks,

Peter



It usually allocates a little extra space before the array, in which
it puts housekeeping data like the size of the array.



It might break all other code.


What, the compiler might? I'm not sure what you mean... It really does
work this way (for many implementations).

I do not understand how compiler allocates extra space before the array
and then access it at the run time. Maybe that is the way it works.
Way back when, you actually did have to cite the number of elements
as part of the delete statement.



If I don't cite the number of elements, it works fine to call
the deconstructor 50 times for delete [] A.


That doesn't free the memory, it just deconstructs the objects.

I took it for granted that we don't need to specify the number of
elements for delete. Maybe I am wrong.

I just checked the previous thread discussing about delete [].

here is the quote from "Unforgiven <[email protected]>":

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
 

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

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,414
Latest member
GayleWedel

Latest Threads

Top