deleting dynamically allocated memory...

This is a discussion on deleting dynamically allocated memory... within the C++ Programming forums, part of the General Programming Boards category; suppose I do this: int main () { someclass** myclass; myclass = new someclass* [50]; . . . //deleting delete ...

  1. #1
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36

    deleting dynamically allocated memory...

    suppose I do this:

    int main () {

    someclass** myclass;

    myclass = new someclass* [50];

    .
    .
    .

    //deleting
    delete myclass;
    OR
    delete [] myclass;

    }

    So is there any difference between "delete [] myclass" and "delete myclass"?

    delete [] myclass probably means freeing memory for an array of objects, but can we use(is there any difference) "delete myclass"?

    Thanks!!
    If only life is as easy as C...

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Use delete [] as you declared an array of objects.....

    If you just use delete, then the compiler wont call the destructors of each object in your array....

    General rule...."If you use [] in the "new" statment then use it in the "delete" statment as well"

  3. #3
    Registered User heljy's Avatar
    Join Date
    Mar 2002
    Posts
    36
    Thanks for the info!
    If only life is as easy as C...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-23-2009, 03:44 PM
  2. Deleting objects show no drop in memory usage.
    By theJ89 in forum C++ Programming
    Replies: 8
    Last Post: 04-02-2006, 06:21 AM
  3. Where are pointers allocated in memory?
    By sparks in forum C++ Programming
    Replies: 5
    Last Post: 07-20-2005, 12:52 PM
  4. scope of dynamically allocated variables
    By lanzyzhang in forum C Programming
    Replies: 4
    Last Post: 07-20-2004, 10:59 AM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM

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