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 7 4017
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.
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.
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
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
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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;
|
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 second pass we can find the
highest number which is less than the number we obtained in the 1st
pass.
|
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.
|
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 may store
the number of consec mem locations a pointer points to, just before the
first data...
|
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 the list that has
to be removed. Also it's position in the list, if that helps.
| |
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 should insert the element at the end of the array and should call the function Sort to sort the...
|
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++ )
|
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 can't figure out what are the alignment
requirements so that the implementation, after calling my...
|
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? :-)
|
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 usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |