473,387 Members | 1,542 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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
Jul 22 '05 #1
7 3987
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.

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

Jul 22 '05 #2
In article <vN***************@newssvr29.news.prodigy.com>,
peter <on********@yahoo.com> 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?


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.

Jul 22 '05 #3
peter wrote:
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
Jul 22 '05 #4
Jeff Schwab wrote:
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.

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
Jul 22 '05 #5
Default User wrote:
peter wrote:

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


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

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


Jul 22 '05 #6
peter wrote:
Jeff Schwab wrote:
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).
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.

Jul 22 '05 #7
Jeff Schwab wrote:
peter wrote:
Jeff Schwab wrote:
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 <ja*******@hotmail.com>":

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



Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kevin | last post by:
Hi, How can I enumerate a linked list while being hable to delete any number of elements while enumerating ? I was using: struct st { ... struct st *prev;
21
by: Jaspreet | last post by:
I was working on some database application and had this small task of getting the second highes marks in a class. I was able to do that using subqueries. Just thinking what is a good way of...
22
by: Cylix | last post by:
I have a 4row x 1col table, I would like to drop all the content of row three. Since Mac IE5.2 does not suppport deleteRow method, I have also try to set the innerHTML=''; but it does not work. ...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
1
by: Daniel Rucareanu | last post by:
Hello, Does anybody knows how can you delete, in just one step, not using a loop, a subset of the child nodes of a given DOM parent node? The subset will be continous, so for example, if the...
6
by: flash | last post by:
write a program that manipulates arrays of integers. The main program should call three functions: Insert, Delete, and Search. The Insert function should call a function Sort that sorts the array. ...
5
by: streamkid | last post by:
i have a class table, which has a vector of records(-db). i 'm trying to remove an element, but it doesn't seem to work.. i read this http://www.cppreference.com/cppvector/erase.html] and that's...
13
by: Tristan Wibberley | last post by:
Hi I've got implementing overloaded operator new and delete pretty much down. Just got to meet the alignment requirements of the class on which the operator is overloaded. But how does one...
29
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, I remembered delete is implemented through operator overloading, but I am not quite clear. Could anyone recommend some links about how delete is implemented so that I can...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.