By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Deleting array allocated with new

P: n/a
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[i] = 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[i]; /* is this correct? "[]" after delete
keyword. compiler accepts it.*/
// or this
delete pData[i]; // I think this is wrong but compiler
accepts
}

delete pData;

to summarize:
is it:

delete [] pData[i];

or

delete pData[i];

Thanks.
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a

"Chuck Rittersdorf" <ch*************@hotmail.com> wrote in message news:fb**************************@posting.google.c om...
for( i = 0; i < No_Rows; i++){
delete [] pData[i]; /* is this correct? "[]" after delete
keyword. compiler accepts it.*/
// or this
delete pData[i]; // I think this is wrong but compiler
accepts
}

delete pData;


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

Always delete[] with new T[n].

Jul 22 '05 #2

P: n/a

"Chuck Rittersdorf" <ch*************@hotmail.com> wrote in message
news:fb**************************@posting.google.c om...
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[i] = new int[No_Cols];

// now for deleting it [SNIP]
delete pData;

to summarize:
is it:

delete [] pData[i];

The correct way is to write

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

delete [] pData;

HTH
Chris

Jul 22 '05 #3

P: n/a
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[i] = 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[i]; /* is this correct? "[]" after delete
keyword. compiler accepts it.*/
// or this
delete pData[i]; // I think this is wrong but compiler
accepts
}

delete pData;

to summarize:
is it:

delete [] pData[i];

or

delete pData[i];


http://tinyurl.com/xjw7
--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.