Deleting array allocated with new

Discussion in 'C++' started by Chuck Rittersdorf, Dec 3, 2003.

  1. What is the correct form of delete when deleting
    a dynamic block of memory?

    // I am allocating 2D dynamic arrays in the following way.

    int *pData;

    pData = new int *[No_Rows];

    for( int i = 0; i < No_Rows; i++)
    pData = new int[No_Cols];

    // now for deleting it
    // this is what i am not sure about. Compiler accepts it and
    // program runs with no obvious problems, but i am worried
    // i may be causing some kind of memory leak.

    for( i = 0; i < No_Rows; i++){
    delete [] pData; /* is this correct? "[]" after delete
    keyword. compiler accepts it.*/
    // or this
    delete pData; // I think this is wrong but compiler
    accepts
    }

    delete pData;

    to summarize:
    is it:

    delete [] pData;

    or

    delete pData;

    Thanks.
     
    Chuck Rittersdorf, Dec 3, 2003
    #1
    1. Advertising

  2. Chuck Rittersdorf

    Ron Natalie Guest

    "Chuck Rittersdorf" <> wrote in message news:...

    > for( i = 0; i < No_Rows; i++){
    > delete [] pData; /* is this correct? "[]" after delete
    > keyword. compiler accepts it.*/
    > // or this
    > delete pData; // I think this is wrong but compiler
    > accepts
    > }
    >
    > delete pData;


    You want both in the loop:
    delete [] pData
    and afterwards:
    delete [] pdata;

    Always delete[] with new T[n].
     
    Ron Natalie, Dec 3, 2003
    #2
    1. Advertising

  3. Chuck Rittersdorf

    Chris Theis Guest

    "Chuck Rittersdorf" <> wrote in message
    news:...
    > What is the correct form of delete when deleting

    [SNIP
    > int *pData;


    I guess you mean int** pData;

    >
    > pData = new int *[No_Rows];
    >
    > for( int i = 0; i < No_Rows; i++)
    > pData = new int[No_Cols];
    >
    > // now for deleting it

    [SNIP]

    > delete pData;
    >
    > to summarize:
    > is it:
    >
    > delete [] pData;
    >

    The correct way is to write

    for( int i = 0; i < No_Rows; i++)
    delete [] pData;

    delete [] pData;

    HTH
    Chris
     
    Chris Theis, Dec 3, 2003
    #3
  4. Chuck Rittersdorf wrote:
    > What is the correct form of delete when deleting
    > a dynamic block of memory?
    >
    > // I am allocating 2D dynamic arrays in the following way.
    >
    > int *pData;
    >
    > pData = new int *[No_Rows];
    >
    > for( int i = 0; i < No_Rows; i++)
    > pData = new int[No_Cols];
    >
    > // now for deleting it
    > // this is what i am not sure about. Compiler accepts it and
    > // program runs with no obvious problems, but i am worried
    > // i may be causing some kind of memory leak.
    >
    > for( i = 0; i < No_Rows; i++){
    > delete [] pData; /* is this correct? "[]" after delete
    > keyword. compiler accepts it.*/
    > // or this
    > delete pData; // I think this is wrong but compiler
    > accepts
    > }
    >
    > delete pData;
    >
    > to summarize:
    > is it:
    >
    > delete [] pData;
    >
    > or
    >
    > delete pData;


    http://tinyurl.com/xjw7


    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Dec 3, 2003
    #4
    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. Manisha
    Replies:
    2
    Views:
    536
  2. Replies:
    5
    Views:
    623
    Matt Wharton
    Dec 9, 2004
  3. Rohit
    Replies:
    7
    Views:
    404
    Prawit Chaivong
    Mar 1, 2005
  4. Replies:
    2
    Views:
    305
    Default User
    May 8, 2007
  5. , India
    Replies:
    17
    Views:
    477
    Alf P. Steinbach /Usenet
    Aug 10, 2010
Loading...

Share This Page