delete[]

A

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
 
K

Kira Yamato

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.
 
A

AS

Thanx for the reply. I guessed the same. Can you explain in detail or any
sample code to demonstrate this ?

Kira Yamato 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 "?


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.
 
J

Jim Langston

Kira Yamato 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?
 
A

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 said:
Kira Yamato said:
On 2007-12-24 00:57:39 -0500, "AS" <[email protected]>
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?
 
I

Ian Collins

AS wrote:
Please don't top-post.
Jim Langston said:
On 2007-12-24 00:57:39 -0500, "AS" <[email protected]>
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.
 
K

Kira Yamato

Kira Yamato 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 "?


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.

Thanx for the reply. I guessed the same. Can you explain in detail or any
sample code to demonstrate this ?

Better. I'll give you a hint, and you implement the code. Then we'll
discuss it.

Hint: Pretend you already have all of the memory a 64-bits pointer can
address. Now, pretend you're the "librarian" not of books but instead
of these memory bytes. You job is to do book-keeping of what memory
has been loaned out and returned back.

Design a data-structure that does this book-keeping.

Write the "loan-out" function and the "return-back" function.

Try it. It'll be more rewarding to you this way.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top