Delete

Printable View

• 03-03-2002
ginoitalo
Delete
Suppose...

char *Area[50];

each Area[x] points to a NEW(from HEAP) char array

in the destructor....

I don't think delete *Area; wil destroy all 50 dynamic arrays ?

if (Area)
for(int i=0;Area[i];i++)
delete Area[i];

Gives an error, so does, delete *Area[i];
• 03-03-2002
Betazep
you can't delete it if you didn't create it with 'new' first off...

delete []Array
• 03-03-2002
Sorensen
Assuming unallocated elements are NULL

for(int i=0;i<50;i++)
if(Area[i])
delete [] Area[i];

should work, if I've understood what you're trying to do.
• 03-03-2002
Betazep
Oh... sorry. I missed it. I see what you are saying...

Code:

```int main() {   int i;   char *the[10];   for (i=0; i<=9; ++i)       the[i] = new char[10];   for (i=0; i<=9; ++i)       delete []the[i]; return 0; }```
• 03-03-2002
ginoitalo
When i allocate the NEW array, how is it possible to assign NULL to each NEW element in the newly created array ?

things like Area[i]+j=NULL in a for loop don't work, they look like they should though, I'm probably missing some * operators somewhere.
• 03-03-2002
Betazep
so you want every array element in each of the new character arrays to be null?

use a nested for loop...

Code:

```   for (i=0;i<=9;++i)   {       for (j=0;j<=9;++j)       {         the[i][j]=NULL;       }   }```
draw a line straight down on a piece of lined paper. Imagine that line is an array of pointers and the lines going to the right are representative of a character array per line.

The character arrays are referenced normally, but through the use of a pointer. You cannot put a NULL at the position of the pointer, because it is a character array and not a character. So the character arrays themselves can have data put into them by

the[0][0] = 'a';

which represents the first pointer to the first block of the first array. It is like a grid... it is called a two dimensional array.

Make sense...?
• 03-03-2002
Betazep
Here is a graphical representation of the above mentioned that I made real quick. Perhaps it will help you...

Code:

```        0    1    2    3    4       ----- ----- ----- ----- ----- ptr1->| 0,0 | 0,1 | 0,2 | 0,3 | 0,4 |       ----- ----- ----- ----- -----      ptr2->| 1,0 | 1,1 | 1,2 | 1,3 | 1,4 |       ----- ----- ----- ----- ----- ptr3->| 2,0 | 2,1 | 2,2 | 2,3 | 2,4 |       ----- ----- ----- ----- ----- ptr4->| 3,0 | 3,1 | 3,2 | 3,3 | 3,4 |       ----- ----- ----- ----- ----- ptr5->| 4,0 | 4,1 | 4,2 | 4,3 | 4,4 |       ----- ----- ----- ----- -----```
• 03-03-2002
ginoitalo
Most Excellent !

.......Much Appreciated !
• 03-04-2002
Betazep
You're most certainly welcome. I'm sure you will return the favor someday.... ;)