473,544 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4002
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.n ews.prodigy.com >,
peter <on********@yah oo.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*******@hotm ail.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
1580
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
13452
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 getting second highest value in an integer array. One method I know of is to make the 1st pass through the array and find the highest number. In the...
22
4154
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. How can I delete the node from DOM in other way? Thanks.
5
8072
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 assigned to <pointerby "new". Does "new" create a list of pointer names and the size of the memory array they point to? I also read that some compilers...
1
2317
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 parent node has 100 nodes, I want to delete nodes 10 through 75, and not nodes 5, 10, 25 etc. I have a reference to the first and the last node in...
6
6409
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. Here is a description of the three functions: Insert: Accepts as input the array and the element you want to insert into the array. The function...
5
12022
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 the function i 've written: void table::delel( int index ) { vector< record >::iterator rm = db.begin(); for( int i = 0; i < index; i++ )
13
5018
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 implement operator new/delete I can't see a way to indicate, on delete, how many objects must be destroyed (or how big the space is) - alternatively I...
29
2226
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 learn and refresh my memory? :-)
0
7445
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7637
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
5311
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4934
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3427
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1853
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
998
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
682
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.