473,787 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete[]

AS
Hello,
consider the following statements,
int *pBuf = new int[10]; //this will allocate 10*sizeof(int) memory

delete []pBuf // this will delete all the memory allocated to pBuf

Question: How come the application knows that it has to delete memory of
pBuf for 10 * sizeof(int) from the statement "delete []pBuf "?
Pls. reply back
John
Dec 24 '07 #1
6 1732
On 2007-12-24 00:57:39 -0500, "AS" <as**********@r ediffmail.comsa id:
Hello,
consider the following statements,
int *pBuf = new int[10]; //this will allocate 10*sizeof(int) memory

delete []pBuf // this will delete all the memory allocated to pBuf

Question: How come the application knows that it has to delete memory of
pBuf for 10 * sizeof(int) from the statement "delete []pBuf "?
Pls. reply back
John
Same way how free() knows how much memory to reclaim from malloc(): It
keeps internal data structure on what pieces of memory were issued.

--

-kira

Dec 24 '07 #2
AS
Thanx for the reply. I guessed the same. Can you explain in detail or any
sample code to demonstrate this ?

"Kira Yamato" <ki*****@earthl ink.netwrote in message
news:2007122400 425016807-kirakun@earthli nknet...
On 2007-12-24 00:57:39 -0500, "AS" <as**********@r ediffmail.comsa id:
>Hello,
consider the following statements,
int *pBuf = new int[10]; //this will allocate 10*sizeof(int) memory

delete []pBuf // this will delete all the memory allocated to pBuf

Question: How come the application knows that it has to delete memory of
pBuf for 10 * sizeof(int) from the statement "delete []pBuf "?
Pls. reply back
John

Same way how free() knows how much memory to reclaim from malloc(): It
keeps internal data structure on what pieces of memory were issued.

--

-kira

Dec 24 '07 #3
"Kira Yamato" <ki*****@earthl ink.netwrote in message
news:2007122400 425016807-kirakun@earthli nknet...
>On 2007-12-24 00:57:39 -0500, "AS" <as**********@r ediffmail.com>
said:
>>Hello,
consider the following statements,
int *pBuf = new int[10]; //this will allocate 10*sizeof(int)
memory delete []pBuf // this will delete all the memory allocated to
pBuf Question: How come the application knows that it has to delete
memory of pBuf for 10 * sizeof(int) from the statement "delete
[]pBuf "?
>Same way how free() knows how much memory to reclaim from malloc(): It
keeps internal data structure on what pieces of memory were
issued.
Thanx for the reply. I guessed the same. Can you explain in detail or
any sample code to demonstrate this ?
It's the compiler/OS that keeps track. It may be done differently in
different compilers and OSes. I think you gave the perfect example, what
more would you need?

--
Jim Langston
ta*******@rocke tmail.com
Dec 24 '07 #4
AS
I would like to know how this is happening internally, suppose that if we
have to write our own Memory Manager which handle these ?

"Jim Langston" <ta*******@rock etmail.comwrote in message
news:Ne******** *****@newsfe05. lga...
>"Kira Yamato" <ki*****@earthl ink.netwrote in message
news:200712240 0425016807-kirakun@earthli nknet...
>>On 2007-12-24 00:57:39 -0500, "AS" <as**********@r ediffmail.com>
said:
Hello,
consider the following statements,
int *pBuf = new int[10]; //this will allocate 10*sizeof(int)
memory delete []pBuf // this will delete all the memory allocated
to
pBuf Question: How come the application knows that it has to delete
memory of pBuf for 10 * sizeof(int) from the statement "delete
[]pBuf "?
>>Same way how free() knows how much memory to reclaim from malloc(): It
keeps internal data structure on what pieces of memory were
issued.
>Thanx for the reply. I guessed the same. Can you explain in detail or
any sample code to demonstrate this ?

It's the compiler/OS that keeps track. It may be done differently in
different compilers and OSes. I think you gave the perfect example, what
more would you need?

--
Jim Langston
ta*******@rocke tmail.com

Dec 24 '07 #5
AS wrote:
Please don't top-post.
"Jim Langston" <ta*******@rock etmail.comwrote in message
news:Ne******** *****@newsfe05. lga...
>>"Kira Yamato" <ki*****@earthl ink.netwrote in message
news:20071224 00425016807-kirakun@earthli nknet...
On 2007-12-24 00:57:39 -0500, "AS" <as**********@r ediffmail.com>
said:
Hello,
consider the following statements,
int *pBuf = new int[10]; //this will allocate 10*sizeof(int)
memory delete []pBuf // this will delete all the memory allocated
to
pBuf Question: How come the application knows that it has to delete
memory of pBuf for 10 * sizeof(int) from the statement "delete
[]pBuf "?
Same way how free() knows how much memory to reclaim from malloc(): It
keeps internal data structure on what pieces of memory were
issued.
Thanx for the reply. I guessed the same. Can you explain in detail or
any sample code to demonstrate this ?
It's the compiler/OS that keeps track. It may be done differently in
different compilers and OSes. I think you gave the perfect example, what
more would you need?
Or quote signatures.
>>
I would like to know how this is happening internally, suppose that if we
have to write our own Memory Manager which handle these ?
The answer is part of your operating system or runtime libraries. If
you can , consult the source for those. Writing your own memory manager
is an excellent way to understand the problems involved with dynamic
memory management.

--
Ian Collins.
Dec 24 '07 #6
On Dec 24, 12:43*pm, "AS" <asudhakar...@r ediffmail.comwr ote:
I would like to know how this is happening internally, suppose that if we
have to write our own Memory Manager which handle these ?
Consider going through the C++ FAQ lite -
http://www.parashift.com/c++-faq-lit...html#faq-16.14
Dec 24 '07 #7

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

Similar topics

2
2599
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown during construction, a form of
1
3849
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
3
9418
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated...
1
3888
by: Douglas Peterson | last post by:
class Allocator { public: virtual void * Alloc(size_t) = 0; virtual void * Free(void*) = 0; }; class Object { public:
2
2093
by: Dave | last post by:
Hello all, I'd like to find a source on the web that discusses, in a comprehensive manner and in one place, everything about new / delete. It should include overloading operator new, the new operator, placement, nothrow, arrays, etc... My books cover the topic, I've found FAQs on the web that cover the topic, and so on, but all the sources I've found are disjointed. There's a bit on this page, a bit on that page, and so on. The...
3
4653
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
9
8179
by: rohits123 | last post by:
I have an overload delete operator as below ////////////////////////////////// void operator delete(void* mem,int head_type) { mmHead local_Head = CPRMemory::GetMemoryHead(head_type); mmFree(&local_Head,(char *)mem); CPRMemory::SetMemoryHeadAs(local_Head,head_type); } ///////////////////// void* operator new(size_t sz, int head_Type) {
10
2097
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a sizeof(foo)-sized buffer but does NOT call foo::foo().
15
5026
by: LuB | last post by:
I am constantly creating and destroying a singular object used within a class I wrote. To save a bit of time, I am considering using 'placement new'. I guess we could also debate this decision - but for the sake of this post ... I'm searching for an answer that assumes said decision. If I allocate memory in the class declaration: char buffer;
29
2273
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
9655
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9497
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,...
0
10363
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10169
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 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...
0
9964
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 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...
1
7517
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
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();...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.