Thread: New/delete vs malloc/free

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    486

    New/delete vs malloc/free

    I have used C up to now, just starting to teach myself C++. Are there any advantages to using new/delete over malloc/free besides less typing?

    Are they essentially the same thing, just with different syntaxes?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean aside from the fact that new/delete calls the constructor/destructor for the objects it creates, which malloc/free doesn't. Sure, beside that fact, they are pretty much the same. They both allocate memory and assign it to the pointer you give, and release the memory from a given pointer.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    They are very different in that malloc/free just gives you a bunch of bytes, whereas new/delete construct and destruct allocated objects. That is, you can't use malloc/free for anything that needs to have its constructor/destructor called (unless you want to go through great trouble).

    Furthermore, in C++ it is almost always possible to avoid the array form of new[]/delete[], since you can just use std::vector. It is also possible to avoid explicit calls to delete, if you let smart pointers like boost::shared_ptr take care of new'ed objects.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Jun 2009
    Posts
    486
    I guess I should have been more specific - I am interested in arrays of basic types: for example

    is

    Code:
    array = new int[10]
    functionally the same as

    Code:
    array = (int *) malloc(10 * sizeof(int))
    ?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Not quite. If malloc was unable to allocate memory, it would return a null pointer, but the corresponding behaviour from new[] would be to throw std::bad_alloc.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by KBriggs View Post
    I guess I should have been more specific - I am interested in arrays of basic types: for example

    is

    Code:
    array = new int[10]
    functionally the same as

    Code:
    array = (int *) malloc(10 * sizeof(int))
    ?
    As Laserlight points out, new and malloc behaves differently when there isn't enough memory to satisfy the request. However, the other part of the question is sort of "what should I use", which I would say is:
    1. If you are writing C, you haven't got a choice: malloc/free it is.
    2. If you are writing C++, use new/delete - it will should never be wrong to do this, and if for some reason you change your mind, and decide to change the array of integers into a class CInteger, you do not need to make as many changes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    There is also more type-safety.
    This is legal (but wrong) in C:
    float* x = malloc(sizeof(int));
    On the contrary, if you do it in C++, you will get a compile error:
    float* x = new int;

    Type-safety is one of C++'s most powerful features. Make good use of it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. redirect malloc/free
    By pheres in forum C Programming
    Replies: 0
    Last Post: 03-21-2009, 03:28 AM
  2. Overriding operator new/delete?
    By Elysia in forum C++ Programming
    Replies: 13
    Last Post: 02-25-2008, 12:10 PM
  3. Replies: 6
    Last Post: 09-14-2006, 10:46 PM
  4. overloading new/delete
    By DavidP in forum C++ Programming
    Replies: 13
    Last Post: 07-02-2004, 08:17 AM
  5. new/delete problems, please help
    By btq in forum C++ Programming
    Replies: 9
    Last Post: 06-07-2002, 11:23 AM