Thread: free() or delete?

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    417

    free() or delete?

    Can they be used interchangibly (they seem to be able to), and if so, which is better to use (i.e. does every OS/compiler support them, does one have more functionality)?

    And continuing that line of thought, is there a better way than using new? I know there are malloc and things like that... but those are probably not as good to use, correct?

    Thanks a bunch!

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Delete usually does the same as free, except that it also calls destructors....so if you call malloc and then delete, you may end up with a crash

    if you use new, use delete. If malloc, then free....

    In C++ try stick with new/delete

  3. #3
    i want wookie cookies the Wookie's Avatar
    Join Date
    Oct 2002
    Posts
    455
    you're not supposed to interchange them, new goes with delete and free goes with malloc

    new and delete call the constructor and destructor respectively, malloc and free dont

  4. #4

  5. #5
    C++ introduced new and delete to replace malloc() and free(). So when using C++ just use new and delete, but in C these two are not avilable, so just use malloc() and free() when creating a C program.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    the problem is that I was told to use

    strdup()

    should I manually allocate it?

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Then use free(). Note that strdup is actually a very common extention, it's not really true C/C++. It's no sin to use malloc, and particularly realloc in C++, if you know what you are doing. In general, you should avoid the whole issue alltogether and use std::string and std::vector and let them handle all such nasty details as cleaning up after themselves.

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    417
    unfortunately, doing that didn't work. I tried.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MSVC 2003 Debugging delete
    By Bajanine in forum Windows Programming
    Replies: 2
    Last Post: 09-09-2007, 12:15 PM
  2. Proper Usage of the delete Operator
    By thetinman in forum C++ Programming
    Replies: 7
    Last Post: 04-25-2007, 11:53 PM
  3. delete by copy only for non-children nodes?
    By patricio2626 in forum C++ Programming
    Replies: 17
    Last Post: 11-18-2006, 08:37 AM
  4. delete and delete []
    By Lionel in forum C++ Programming
    Replies: 8
    Last Post: 05-19-2005, 01:52 PM
  5. Problem need help
    By Srpurdy in forum C++ Programming
    Replies: 1
    Last Post: 07-24-2002, 12:45 PM