Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Deleting array allocated with new

Reply
Thread Tools

Deleting array allocated with new

 
 
Chuck Rittersdorf
Guest
Posts: n/a
 
      12-03-2003
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.
 
Reply With Quote
 
 
 
 
Ron Natalie
Guest
Posts: n/a
 
      12-03-2003

"Chuck Rittersdorf" <(E-Mail Removed)> wrote in message news:(E-Mail Removed) 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].



 
Reply With Quote
 
 
 
 
Chris Theis
Guest
Posts: n/a
 
      12-03-2003

"Chuck Rittersdorf" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
Peter van Merkerk
Guest
Posts: n/a
 
      12-03-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting dynamically allocated objects in a container subramanian100in@yahoo.com, India C++ 17 08-10-2010 09:02 PM
Deleting memory allocated to the derived class Rohit C++ 7 03-01-2005 10:34 AM
Dynamically Allocated Memory vs. Statically allocated Memory csnerd@gmail.com C++ 5 12-09-2004 01:44 AM
Problem in deleting the dynamic memory in a parent thread that is allocated from a child thread Manisha C++ 2 09-13-2004 05:48 PM
Passing a contiguously allocated 2D array to a function expecting a 1 dimesnional array Jonathan Underwood C Programming 2 08-13-2003 04:21 PM



Advertisments