delete[] or delete?

This is a discussion on delete[] or delete? within the C++ Programming forums, part of the General Programming Boards category; We had a computers lesson yesterday and our teacher was explaining pointers and how to allocate memory.. etc.. using new ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    288

    delete[] or delete?

    We had a computers lesson yesterday and our teacher was explaining pointers and how to allocate memory.. etc.. using new and delete([]).

    At one point, she said:

    Code:
    char* strng = new char[astring.getCount() + 1];
    
    for (i = 0; i < astring.getCount(); i++)
    {
    	strng[i] = astring.getCharacter(i);
    }
    
    strng[i] = '\0';
    
    //do something with strng
    
    delete strng;
    I always thought it was delete[] strng, since strng is allocated as an array? I didnt want to correct the teacher since well, i didnt want to be wrong, but im almost 100% sure that its delete[] and not delete...

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    If you use new, use delete. If you use new[], use delete[].

    You are right. Your teacher goofed. Most teachers I know appreciate being corrected, as do I as a teaching assistant.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    C(++)(#)
    Join Date
    Jul 2004
    Posts
    309
    What happened to teaching the STL?

    Anyways...both compile(for me) but I don't know if it actually works both ways.
    To code is divine

  4. #4
    Registered User
    Join Date
    Aug 2003
    Posts
    288
    ah, thanks for the quick reply, i was beginning to doubt my own knowledge...

    its really annoying when a teacher says something wrong and its like you have to force yourself to beleive him/her because he/she is a teacher...

    anyway, thanks again.

    edit: the teacher wants to leave STL to the end, she thinks if we can make do without STL then we'd do even better with it.

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    delete [] has the advantage of calling destructors on all objects of the array, if you use the standard delete, you risk the chance of memory leaks or worse.

    For chars and ints etc, it probably wont make a difference, but the standard says that you must match both forms of delete and new, so not doing so could result in undefined behavior

  6. #6
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    712
    Quote Originally Posted by Fordy
    delete [] has the advantage of calling destructors on all objects of the array, if you use the standard delete, you risk the chance of memory leaks or worse.
    Yes, Fordy is right, to help you understand problem try this code
    Code:
    #include <iostream>
    #include <cstdlib>
    
    using namespace std;
    
    
    class MyClass
    {
          int x;
          public:
                 MyClass()
                 {
                          cout<<"Constructor!"<<endl;
                 }
                 ~MyClass()
                 {
                           cout<<"Destructor!"<<endl;
                 }
    };
    
    int main()
    {
        MyClass *pobj = new MyClass[5];
        
        delete [] pobj;
       //delete pobj;
        
        system("pause");
    }
    After trying, comment line with [] and uncomment line with delete pobj. Observe what happens.
    What would happen if in MyClass was int *x , constructor allocated memory and destructor deallocated memory?

    After that, post your conclusion here!
    Last edited by Micko; 03-30-2005 at 02:07 PM.
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  7. #7
    Registered User Joelito's Avatar
    Join Date
    Mar 2005
    Location
    Tijuana, BC, México
    Posts
    308
    Ah so... If I understood:
    I should use delete[] whe the allocation was from new[] and delete from new?
    Code:
    // ...
    int main()
    {
    // ...
    MyClass *mc = new MyClass;
    //...
    delete mc;
    //...
    char* s = new s[256];
    //...
    delete s[]:
    //...
    return 0;
    }

  8. #8
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    Precisely! That's all there is to it, Joelito.
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

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, 05:24 PM
  2. why delete[] and delete?
    By dude543 in forum C++ Programming
    Replies: 4
    Last Post: 11-26-2005, 10:55 PM
  3. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 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

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