how does the delete function release the memory?

  • Thread starter =?gb2312?B?wNbA1rTzzOzKpg==?=
  • Start date
?

=?gb2312?B?wNbA1rTzzOzKpg==?=

struct elem
{
int a;
int b;
};


elem * p = new elem[20];
delete []p;
//-----will the text above memory leak?
 
A

Alan Johnson

????? said:
struct elem
{
int a;
int b;
};


elem * p = new elem[20];
delete []p;
//-----will the text above memory leak?

No. That is the correct usage of new[] and delete[]. Are you asking
how delete[] knows how many elements you allocated? That depends on your
implementation, but one possible way is that it allocates a few extra
bytes before the pointer that is returned, in which the number of
elements is stored.

See the FAQ 16.14 for details of this and another method.
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.14
 

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,756
Messages
2,569,533
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top