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

clarification on delete []

P: n/a
Which of the following do I use delete[] instead of just delete.

//1.)
// not sure about this one, as char is of size 1
char *str = new char[10];

//2.)
//not sure about this one, as it is a primitive
int *array = new int[100];

//3.)
// I know I need delete [] for this one
Obj *array = new Obj[2];

//4.)
// what about this one?
//I think I have to manually
//call delete on each element,
//then delete ptrArray?
Obj **ptrArray = new Obj*[2];
ptrArray[0] = new Obj();
ptrArray[1] = new Obj();

//5.)
//similar but a 1 byte primitive
char **strArray = new char*[2];
strArray[0] = new char[10];
strArray[1] = new char[10];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");

//I think this is the same as above, but uglier:
char **strArray = (char**)new char[20];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");

//6.)
//similar but a multi byte primitive
int **intArray = new int[2];
intArray[0] = new int[10];
intArray[1] = new int[10];

Thanks,

~Shea M.

Jul 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Shea Martin wrote:

Which of the following do I use delete[] instead of just delete.


If you used new[] you need delete[]. If you didn't use new[] you
shouldn't use delete[].

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 19 '05 #2

P: n/a

"Shea Martin" <sm*****@arcis.com> wrote in message news:Igglb.8933$f7.481052@localhost...
Which of the following do I use delete[] instead of just delete.


Always use delete [] with new []
Size or the type of the object makes no difference.
Jul 19 '05 #3

P: n/a
Shea Martin wrote:
Which of the following do I use delete[] instead of just delete.
If it's an array allocated with new[], you need to delete[] it. There is
nothing more to it.
//1.)
// not sure about this one, as char is of size 1
What does the size of char have to do with it?
char *str = new char[10];
You need delete[] here, since you used new[].

//2.)
//not sure about this one, as it is a primitive
Doesn't matter.
int *array = new int[100];
Here, too. new[] again.
//3.)
// I know I need delete [] for this one
Obj *array = new Obj[2];
You're right. This needs it too, since (you probably guessed it) you
used new[].
//4.)
// what about this one?
//I think I have to manually
//call delete on each element,
//then delete ptrArray?
Obj **ptrArray = new Obj*[2];
ptrArray[0] = new Obj();
ptrArray[1] = new Obj();
Not quite. You need to delete[] ptrArray.

//5.)
//similar but a 1 byte primitive
char **strArray = new char*[2];
strArray[0] = new char[10];
strArray[1] = new char[10];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");
No. This is not similar, since you're allocating an array of pointers,
and for each of them, you're allocating an array again, while above you
were allocating one single object for each pointer. So you need to
delete[] each string and then delete[] the pointer array. Once again
one delete[] for every new[].
//I think this is the same as above, but uglier:
char **strArray = (char**)new char[20];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");
No, absolutely not. It's just plain wrong. You're allocating an array of
20 chars and then tell the compiler that you want it to treat that as
an array of pointers instead. Those pointers are uninitialized, and
then you try to write data to whatever bogus address they point to.
//6.)
//similar but a multi byte primitive
int **intArray = new int[2];
intArray[0] = new int[10];
intArray[1] = new int[10];


Doesn't make a difference (why should it?). It's just the same as
example 5 (well the first one).

Jul 19 '05 #4

P: n/a
Rolf Magnus wrote:
Shea Martin wrote:

Which of the following do I use delete[] instead of just delete.

If it's an array allocated with new[], you need to delete[] it. There is
nothing more to it.

//1.)
// not sure about this one, as char is of size 1

What does the size of char have to do with it?

char *str = new char[10];

You need delete[] here, since you used new[].

//2.)
//not sure about this one, as it is a primitive

Doesn't matter.

int *array = new int[100];

Here, too. new[] again.

//3.)
// I know I need delete [] for this one
Obj *array = new Obj[2];

You're right. This needs it too, since (you probably guessed it) you
used new[].

//4.)
// what about this one?
//I think I have to manually
//call delete on each element,
//then delete ptrArray?
Obj **ptrArray = new Obj*[2];
ptrArray[0] = new Obj();
ptrArray[1] = new Obj();

Not quite. You need to delete[] ptrArray.

//5.)
//similar but a 1 byte primitive
char **strArray = new char*[2];
strArray[0] = new char[10];
strArray[1] = new char[10];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");

No. This is not similar, since you're allocating an array of pointers,
and for each of them, you're allocating an array again, while above you
were allocating one single object for each pointer. So you need to
delete[] each string and then delete[] the pointer array. Once again
one delete[] for every new[].

//I think this is the same as above, but uglier:
char **strArray = (char**)new char[20];
strcpy(strArray[0], "zero");
strcpy(strArray[1], "one");

No, absolutely not. It's just plain wrong. You're allocating an array of
20 chars and then tell the compiler that you want it to treat that as
an array of pointers instead. Those pointers are uninitialized, and
then you try to write data to whatever bogus address they point to.

//6.)
//similar but a multi byte primitive
int **intArray = new int[2];
intArray[0] = new int[10];
intArray[1] = new int[10];

Doesn't make a difference (why should it?). It's just the same as
example 5 (well the first one).

Thanks, for the clarification.
~S

Jul 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.