473,473 Members | 2,026 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How delete works!

Neo
Hi freinds,
How delete operator comes to know , how much memory it has to
deallocate?

Neo

May 26 '06 #1
9 4115
Neo wrote:
Hi freinds,
How delete operator comes to know , how much memory it has to
deallocate?


Any implementation is free to devise its own magic. Possible approaches
include:

a) allocate a little more and store the size within the allocated region.
b) store the size within the pointer variable.
c) use the low-order bits of a pointer to keep track of the size (only
supports very few different sizes and need some overhead for
dereferencing).
d) use different chunks of memory to allocate objects of different sizes.
This way, the location of an object allows you to tell its size.
Best

Kai-Uwe Bux

May 26 '06 #2
Neo wrote:
How delete operator comes to know , how much memory it has to
deallocate?


It depends on the implementation. Memory allocation systems in general
can be quite subtle and complex, so there is no easy answer. Generally
speaking, though, when memory is allocated a small amount of
"housekeeping data" is used along with it (note I did not say it is
co-located with the allocated memory; it needn't be). The delete
operator has access to this data and uses it accordingly.

For example, one implementation would be a hash table mapping pointers
(addresses to the first byte of each allocated block of memory) to
sizes. The delete operation would look up the size based on the value
of its argument. This is only part of the picture, of course -- the
next question is, how does the new operator know what memory is
available to allocate when a request comes in for a (contiguous) block
of a particular size? There's a great deal of reading material
available on this topic; if you're interested, search the web, visit a
library, or patronize a bookseller such as Amazon.com*.

Luke

* ObDisclosure: I work there. Oh, and I guess I own some stock, or
will once it vests.

May 26 '06 #3
Neo
Hie,
Thanx for reply.Please see my comments...

a) allocate a little more and store the size within the allocated
region.
==> This seems to be okey.

b) store the size within the pointer variable.

==>If pointer variable is holding the sizeof object which is too big
then we are in truoble.See that is why we have virtual table pointers
placed inside the object and pointer dont really knows which type of
object we are pointing.

c) use the low-order bits of a pointer to keep track of the size (only

supports very few different sizes and need some overhead for
dereferencing).

==>same as disscussed in point (b)

d) use different chunks of memory to allocate objects of different
sizes.
This way, the location of an object allows you to tell its size.
==>can u be more precise on this? seems to be interesting ...
Neo


Kai-Uwe Bux wrote:
Neo wrote:
Hi freinds,
How delete operator comes to know , how much memory it has to
deallocate?


Any implementation is free to devise its own magic. Possible approaches
include:

a) allocate a little more and store the size within the allocated region.
b) store the size within the pointer variable.
c) use the low-order bits of a pointer to keep track of the size (only
supports very few different sizes and need some overhead for
dereferencing).
d) use different chunks of memory to allocate objects of different sizes.
This way, the location of an object allows you to tell its size.
Best

Kai-Uwe Bux


May 26 '06 #4
Neo wrote:
Hie,
Thanx for reply.Please see my comments...

a) allocate a little more and store the size within the allocated
region.
==> This seems to be okey.

b) store the size within the pointer variable.

==>If pointer variable is holding the sizeof object which is too big
then we are in truoble.See that is why we have virtual table pointers
placed inside the object and pointer dont really knows which type of
object we are pointing.
I am not following this. What do you mean by the "sizeof object" being "too
big". And what kind of trouble do you see. As far as I can tell, on a 32
bit architecture as 64bit pointer variable will happily hold the
information about position and length of any consecutive region of memory.
Of course, for accessing your would only need first 32 bits of that pointer
variable, however nothing in the standard prevents an implementation from
making sizeof(T*) bigger than you might expect.

c) use the low-order bits of a pointer to keep track of the size (only

supports very few different sizes and need some overhead for
dereferencing).

==>same as disscussed in point (b)
Same as in b): I do not see the objection.
d) use different chunks of memory to allocate objects of different
sizes.
This way, the location of an object allows you to tell its size.
==>can u be more precise on this? seems to be interesting ...


Well, your allocator has a pool for 4-byte objects another pool for 8-byte
objects, and so on. For instance, when asked to allocate the first 4-byte
object, the allocator asks the OS for 64KB. It now has a pointer to the
beginning of that chunk and uses that chunk to serve all requests for
4-byte allocation. Whenever you delete a pointer that points into this
chunk, the allocator can deduce that it points to a 4-byte object.
Best
Neo wrote:
> Hi freinds,
> How delete operator comes to know , how much memory it has to
> deallocate?


Any implementation is free to devise its own magic. Possible approaches
include:

a) allocate a little more and store the size within the allocated region.
b) store the size within the pointer variable.
c) use the low-order bits of a pointer to keep track of the size (only
supports very few different sizes and need some overhead for
dereferencing).
d) use different chunks of memory to allocate objects of different sizes.
This way, the location of an object allows you to tell its size.


Please do not needlessly attach a copy of the message. Folks in this news
group, including me, are very sensitive to quoting styles. The preferred
style here is to do as you did in the first part of your post: quote and
write your comments right below what they refer to.
Best

Kai-Uwe Bux
May 26 '06 #5
Neo
Hi
As you said we have 64 bit pointers..as far as I know for 32 bit
systems we have 32 bit sized pointers.If there are any 64 bit pointers
I really dont know about that. I am working with windows 2000 under
microsoft C++ compiler version 6.0.

for storing a size of (large)object , I need more bits from pointer
variable. So what if size is too large. As sizeof operator returns size
(size_t) is an unsigned integer. More the size more the bits I need !
Please let me know if there are any misunderstandings...
Also thanx for mentioning about message posting standards. This is my
first post and I am really happy that lot of good people are here
around !

Neo

May 26 '06 #6
On 2006-05-26 12:08, Neo wrote:
Hi
As you said we have 64 bit pointers..as far as I know for 32 bit
systems we have 32 bit sized pointers.If there are any 64 bit pointers
I really dont know about that. I am working with windows 2000 under
microsoft C++ compiler version 6.0.


What Kai-Uwe Bux tried to say was that even though only 32 bits are
needed for a pointer (on a 32-bit machine) there is no rule (as far as I
know) that prevents an implementation from using more. So one could make
an implementation where pointers are 64 bits and uses half of them to
tell the size of the object the other half points to.

Erik Wikström
--
"I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure
out how to use my telephone" -- Bjarne Stroustrup
May 26 '06 #7

Neo wrote:
Hi freinds,
How delete operator comes to know , how much memory it has to
deallocate?

Neo


Two cases: types with and without a virtual destructor.
If the type has a virtual destructor, delete will call that before
deallocating memory.
Now, since you cannot define a return type, the compiler might use a
(hidden)
return value to return that information. Another solution is to store
the size in a
vtable. There are even more variations, but all take advantage of the
fact that the
compiler has type information for this object, and it's easy to add
size to that type
information.

The case without a virtual destructor is easy. delete then gets a
pointer of exactly
the right type, not a base type. So if it gets a T*, it knows it has to
call T::~T and
then deallocate sizeof(T).

HTH,
Michiel Salters

May 26 '06 #8
In article <1148638085.431124.221190
@j73g2000cwa.googlegroups.com>, vi**********@gmail.com
says...
Hi
As you said we have 64 bit pointers..as far as I know for 32 bit
systems we have 32 bit sized pointers.If there are any 64 bit pointers
I really dont know about that. I am working with windows 2000 under
microsoft C++ compiler version 6.0.
Under Win32, you use only 32 bit of a pointer -- but the
processor (Intel/AMD x86) actually requires 48-bits. The
48 bits consists of a 16-bit segment and a 32-bit offset.
In Win32, the segments are fixed, and all you work with
are offsets.

Nonetheless, if somebody wanted to, they could perfectly
well use multiple segments. In that case, they create a
"descriptor" for a segment that specifies (among other
things) the maximum offset legal for that segment (i.e.
the size of the memory block it refers to). The usual
Win32 segments are all set to a size of 4 GB, but that's
not required in general -- under a different system, you
could set them to the actual sizes of the memory blocks
they referred to.
for storing a size of (large)object , I need more bits from pointer
variable. So what if size is too large. As sizeof operator returns size
(size_t) is an unsigned integer. More the size more the bits I need !
Please let me know if there are any misunderstandings...


There are quite a few ways of getting around this
problem. One is outlined above -- associate some data
with the pointer, but don't make it part of the pointer
itself. Another is to limit your selection of sizes. For
example, many memory allocators round the size of any
allocation to the next larger power of 2. In this case,
you really don't need to store the size of the
allocation, but instead just the base 2 logarithm of the
size. For a 64-bit system, that means the maximum size
you need to store is 64 -- which only requires 6 bits.
Most 64-bit systems only really use around 48 bits (or
less) for actual addressing. That leaves 16 bits (or
more) in the pointer that can be used to hold other data,
such as sizes, types, etc.

--
Later,
Jerry.

The universe is a figment of its own imagination.
May 26 '06 #9
Neo posted:
Hi freinds,
How delete operator comes to know , how much memory it has to
deallocate?

Neo

God::Communicate::AskCompilerSpecificQuestion();
-Tomás
May 26 '06 #10

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

Similar topics

1
by: Senthilvel Samatharman | last post by:
I am just curious about the case 3 in the follwouing program. I understand that case 1 is the right way to overload, while Case 2 is erroneous. But i do think that the implementation of operator...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
8
by: Steve | last post by:
I have several pairs of synchronized subforms in an application. I have a Delete button for each pair that uses the following code or similar to delete a record in the second subform: ...
5
by: ms | last post by:
Why does this select query return the correct records but when I make it a delete query I get a msgbox with "Could not delete from specified tables". SELECT BMIDLog.* FROM stageBMIDLog INNER...
3
by: Maria | last post by:
Is there another way to delete the current record in a subform from the main form, another subform or a sub-subform other than setting focus on a field in the subform and using run command...
3
by: vcornjamb | last post by:
Hello, I am developing a web form that contains some buttons and a data grid which has as its last column link buttons that will delete the data associated with that row. Everything works fine,...
6
by: Joemanc | last post by:
I've converted my VB6 app to VB.NET 2003. For some strange reason, the delete key will not work in any of my textboxes in the application. The backspace key works just fine. The delete key works in...
4
by: =?Utf-8?B?UmljaA==?= | last post by:
On a form - I have a datagridview which is docked to the entire form. The datagridview allows users to Delete and/or Add Rows. On the Form_Load event I Fill the datagridview source table with a...
12
by: Premal | last post by:
Hi, I tried to make delete operator private for my class. Strangely it is giving me error if I compile that code in VC++.NET. But it compiles successfully on VC++6.o. Can anybody give me inputs...
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
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,...
0
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,...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
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...

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.