clarification on delete []

S

Shea Martin

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.
 
P

Pete Becker

Shea said:
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[].
 
R

Ron Natalie

Shea Martin said:
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.
 
R

Rolf Magnus

Shea said:
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).
 
S

Shea Martin

Rolf said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top