how does the delete function release the memory?

Discussion in 'C++' started by =?gb2312?B?wNbA1rTzzOzKpg==?=, Jan 27, 2007.

  1. struct elem
    {
    int a;
    int b;
    };


    elem * p = new elem[20];
    delete []p;
    //-----will the text above memory leak?
    =?gb2312?B?wNbA1rTzzOzKpg==?=, Jan 27, 2007
    #1
    1. Advertising

  2. =?gb2312?B?wNbA1rTzzOzKpg==?=

    Alan Johnson Guest

    ????? wrote:
    > 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

    --
    Alan Johnson
    Alan Johnson, Jan 27, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mahesh Prasad
    Replies:
    1
    Views:
    707
    Tom Wells
    Feb 22, 2004
  2. John Goche
    Replies:
    1
    Views:
    7,482
    John Goche
    Dec 17, 2005
  3. D. Susman

    Why does "delete" not delete?

    D. Susman, Mar 10, 2008, in forum: C++
    Replies:
    11
    Views:
    529
    Andre Kostur
    Mar 11, 2008
  4. Hong Chen

    Delete does not really delete

    Hong Chen, Jun 3, 2008, in forum: C++
    Replies:
    2
    Views:
    511
    Puppet_Sock
    Jun 16, 2008
  5. Benjamin Peterson

    [RELEASE] Python 2.7 release candidate 1 released

    Benjamin Peterson, Jun 6, 2010, in forum: Python
    Replies:
    3
    Views:
    364
    Lie Ryan
    Jun 6, 2010
Loading...

Share This Page