Thread: why delete[] and delete?

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    why delete[] and delete?

    I am still trying to understand why do I have to use the brackets when deleting an array. and no brackets other wise.
    CornedBee wrote :
    It's got, among other things, something to do with destructors. Only with [] does delete know to call more than one destructor. (Which doesn't give you permission to mix them if the underlying object doesn't have a destructor.)

    So I wrote this little example, and understod part of it.

    Code:
    #include <iostream>
    #include <mylib.h>
    
    #define  N   3
    
    class mstr 
      {
      char   *str;
    public :
      mstr(void)
         {
         str = NULL;
         }
       void add_new(const char *s)
         {
         str = new char[strlen(s)+1];
         strcpy(str, s);
         }
        ~mstr(void)
        {
        if ( str )
          {
           printf("delete %s\n", str);
           delete str;
           }
        }
        void show(void)
        {
        printf("%s\n", str);
        }
    };
    
    
    class strt
    {
       public :
       mstr  *tbl;
       strt(void)
         {
         tbl = new mstr[N];
         tbl[0].add_new("strt :one");
         tbl[1].add_new("strt :two");
         tbl[2].add_new("strt :three");
         }
       void disp_tbl(void)
         {
         for ( int i = 0 ; i < N ; ++i )       
             tbl[i].show();
         }
    };
    
    
    
    int   main(void)
    {
    strt  st1, st2;
    int   *p = new int[5];
    
    delete p;  // I dont use bracktes and dont crash here !!!!
    
    st1.disp_tbl();
    
    printf("===> delete []st1.tbl <=== : \n");
    delete  []st1.tbl;
    
    
    printf("===> delete st2.tbl <=== : \n");
    delete  st2.tbl; // I dont use bracktes and DO crash here !!!!
    
    
    return(0);
    }

    I see that when I call "delete []st1.tbl" , with brackets it also call
    delete for mstr[0] mstr[1] mstr[2].

    But why does the program crash with "delete st2.tbl" ?

    The output is :
    strt ne
    strt :two
    strt :three
    ===> delete []st1.tbl <=== :
    delete strt :three
    delete strt :two
    delete strt ne
    ===> delete st2.tbl <=== :
    delete strt ne
    Abort (core dumped)

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Its kinda simple:
    new allocates memory for one element, so it only needs to do the constructor for one element.
    new[] allocates memory for one or more elements, so it needs to call the constructor for that many elements.

    delete deallocates memory for one elment, so it only needs to do the deconstructor for one element.
    delete[] deallocates memory for one or more elements, so it needs to the deconstructor for that many elements.

    Now image you used new[] to create 10 objects. But then called delete. What happens to those other 9 elements?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    But why does the program crash with "delete st2.tbl" ?
    From the link Enahs posted:
    The result of deleting an array object with delete is undefined...
    Do you know what undefined behaviour is?

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Thanks all

    I got it. I thought I will remain with unfree memory.
    But 7stud quote told my mistake.

    Again thanks all.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 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
  2. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  3. delete[] or delete?
    By X PaYnE X in forum C++ Programming
    Replies: 7
    Last Post: 03-30-2005, 03:16 PM
  4. delete and delete[]
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 06-26-2003, 04:40 AM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM