Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Array of Pointers

Status
Not open for further replies.

RichardF

Programmer
Oct 9, 2000
239
GB
Hi,

Quick Question:

Is there any diffence between A and B.

I always thought delete[] was used only if you did new MyType[N];

Code:
struct MyType {
  int age;
  float weight;
];

MyType* pList[10];
int i;

// new each item in the array
for (i = 0; i < 10; i++) 
    pList[i] = new MyType;

// A. delete the array 
for (i = 0; i < 10; i++)
    delete pList[i];

// or  B. delete the array using delete[]
delete[] pList;


Thanks,
Rich.
 
> Is there any diffence between A and B.
Yes.
A is right, B is wrong.

> I always thought delete[] was used only if you did new MyType[N];
Yes, that's it.

Note that if you had done
[tt]pList = new MyType[4];[/tt]
Then deleting would be
[tt]delete [] pList;[/tt]

Since pList itself is an array of 10 pointers, you don't need to delete pList itself, only each pList

--
 
Just to summarize:

Every new shall have a corresponding delete. No more, no less.

Every new ... [ ] shall have a corresponding delete []. No more, no less.

It's as simple as that.

/Per

"It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top