Why should I use new and not malloc()?

Discussion in 'C' started by shabbir, Feb 28, 2005.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Just got a Mail from a user asking for a question as and thought also after answering him would like to share the same with Expert Advice Forums member

    Why should I use new and not malloc()?

    » Constructors/destructors:
    New and Delete makes sure that constructors and Destructors are called but not the case with malloc and free functions of C.
    » Pointer conversion safety:
    malloc() returns a void* which isn't safe. new returns a pointer of the right type.
    » new is a Operator:
    new is an operator that can be overloaded for better memory management by a class, while malloc() is not an operaotor.
     
  2. Aztec

    Aztec New Member

    Joined:
    May 9, 2006
    Messages:
    90
    Likes Received:
    0
    Trophy Points:
    0
    Just to add few more things

    >new operator computes the size of object automatically whereas malloc cannot.
    >It is possible to initialize the object while allocating memory with new.
     
  3. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    Adding one more point :

    - Its possible to construct an object over an already allocated memory using another version of 'new' operator known as 'placement new' operator. While with malloc() it is not possible.
     
  4. lipun4u

    lipun4u New Member

    Joined:
    Jan 25, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Mumbai
    Home Page:
    http://kodeyard.blogspot.com/
    certain disadvantage of new...

    if the class doesn't have a default constructor, new can't be used to allocate memory for that object.

    Please tell if i am right ?
     
  5. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    From Wikipedia :

     
  6. lipun4u

    lipun4u New Member

    Joined:
    Jan 25, 2010
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    Mumbai
    Home Page:
    http://kodeyard.blogspot.com/
    But the situation is if I have class with a parametrized constructor and no other constructor at all.
     
  7. poornaMoksha

    poornaMoksha New Member

    Joined:
    Jan 29, 2011
    Messages:
    150
    Likes Received:
    33
    Trophy Points:
    0
    Occupation:
    Software developer
    Location:
    India
    In this case too, new can be used to allocate memory and construct object.
     
  8. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    To create a class that cannot be instantiated with new you need to declare a private constructor. Of course, you will need to pair this with a friend class that will be able to create objects, otherwise you won't be able to create any objects of the first type at all, which would be rather pointless.
     
  9. GaneshShinde

    GaneshShinde New Member

    Joined:
    Oct 13, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Sr. Software Engineer
    Location:
    Pune
    One question just poped in my head while reading this, it may be irrelevant to ask in this question.
    Question is what is the similar unctions or operators for realloc, calloc like C language?
     
  10. GaneshShinde

    GaneshShinde New Member

    Joined:
    Oct 13, 2011
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Sr. Software Engineer
    Location:
    Pune
    Corrected from last post
     
  11. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Exactly the same. You can think of C++ as a superset of C - most things will work the same way.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice