Delete

This is a discussion on Delete within the C++ Programming forums, part of the General Programming Boards category; Suppose... char *Area[50]; each Area[x] points to a NEW(from HEAP) char array in the destructor.... I don't think delete *Area; ...

  1. #1
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187

    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];

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    you can't delete it if you didn't create it with 'new' first off...


    delete []Array
    Blue

  3. #3
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    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.

  4. #4
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    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;
    }
    Blue

  5. #5
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    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.

  6. #6
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    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...?
    Last edited by Betazep; 03-03-2002 at 07:19 PM.
    Blue

  7. #7
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    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 |
           ----- ----- ----- ----- -----
    Blue

  8. #8
    Something Clever ginoitalo's Avatar
    Join Date
    Dec 2001
    Posts
    187
    Most Excellent !

    .......Much Appreciated !

  9. #9
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    You're most certainly welcome. I'm sure you will return the favor someday....
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete Function in Doubly Linked List
    By Dampecram in forum C Programming
    Replies: 5
    Last Post: 11-15-2008, 04:30 PM
  2. BST delete again, but this time I think I'm close
    By tms43 in forum C++ Programming
    Replies: 9
    Last Post: 11-05-2006, 06:24 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 02:52 PM
  4. why is this prog crashing on delete?
    By Waldo2k2 in forum Windows Programming
    Replies: 2
    Last Post: 12-04-2002, 11:17 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 01:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21