none
malloc() versus new

    Question

  • I have a small app, so I don't see how this could be affected, but I thought I'd ask...

    Is there any drawback to using malloc() instead of new to allocate memory? Or, why should I use new instead of malloc(), other than the fact that new has become the standard convention?


    Lamblion
    Sunday, September 11, 2011 9:26 PM

Answers

  • On 11/09/2011 23:44, Lamblion wrote:

    I knew that malloc() is strictly C, but not about the constructor. But what I'm really wondering is if there is any kind of performance hit, especially in a large application or project?

    new calls malloc(), so I think you won't find any performance hits.

    If you want to make your memory allocation faster, you may want to use custom memory allocators like pool allocators.

    See this for an example:

    http://blogs.msdn.com/b/oldnewthing/archive/2005/05/19/420038.aspx
     Giovanni

    • Marked as answer by Lamblion Sunday, September 11, 2011 10:28 PM
    Sunday, September 11, 2011 10:08 PM

All replies

  • malloc is a C function, so it doesn't know about C++ constructors.  If you malloc an object its constructor will not be executed.
    Sunday, September 11, 2011 9:30 PM
  • I knew that malloc() is strictly C, but not about the constructor. But what I'm really wondering is if there is any kind of performance hit, especially in a large application or project?
    Lamblion
    Sunday, September 11, 2011 9:44 PM
  • On 11/09/2011 23:44, Lamblion wrote:

    I knew that malloc() is strictly C, but not about the constructor. But what I'm really wondering is if there is any kind of performance hit, especially in a large application or project?

    new calls malloc(), so I think you won't find any performance hits.

    If you want to make your memory allocation faster, you may want to use custom memory allocators like pool allocators.

    See this for an example:

    http://blogs.msdn.com/b/oldnewthing/archive/2005/05/19/420038.aspx
     Giovanni

    • Marked as answer by Lamblion Sunday, September 11, 2011 10:28 PM
    Sunday, September 11, 2011 10:08 PM
  • >new calls malloc()

    Perhaps. I believe that may depend on the implementation.
    The C++ Standard says:

    " ... the function first attempts to allocate the requested
    storage. Whether the attempt involves a call to the Standard
    C library function malloc is unspecified."

    - Wayne
    Sunday, September 11, 2011 10:26 PM
  • Much obliged. That answers it for me.
    Lamblion
    Sunday, September 11, 2011 10:28 PM
  • >Is there any drawback to using malloc() instead of new to allocate memory? Or, why should I use new instead of malloc(), other than the fact that new has become the standard convention?

    Do yourself a big favour and try to ensure you don't write code that
    uses either of them :)

    Use smart classes like string and vector - that way you're unlikely to
    get any memory leaks in your application.

    Dave

    Sunday, September 11, 2011 10:30 PM
  • On 12/09/2011 00:26, WayneAKing wrote:

    new calls malloc()

    Perhaps. I believe that may depend on the implementation.

    I was thinking of Visual C++ implementation.

    Giovanni

    Monday, September 12, 2011 10:01 AM