Best way to allocate memory for simple types and objects

Discussion in 'C++' started by Dmytro Bablinyuk, Sep 5, 2006.

  1. I came across several possible ways of allocating memory for objects,
    for example:

    1. malloc(sizeof(T)*3)/free - raw memory

    2. new T[3]/delete[] - buffer would be initialized to
    default-constructed T objects.

    3. operator new(sizeof(T)*3)/operator delete - raw memory

    What the best way of allocating memory for simple types and objects?
    For objects the "new T[3]" looks like the best way since it initializes
    the array, but what about simple types?
    Right now for "int *" I am using malloc, but would it be better if I use
    "operator new" for instance? What the advantages?
     
    Dmytro Bablinyuk, Sep 5, 2006
    #1
    1. Advertising

  2. Dmytro Bablinyuk

    Ian Collins Guest

    Dmytro Bablinyuk wrote:
    > I came across several possible ways of allocating memory for objects,
    > for example:
    >
    > 1. malloc(sizeof(T)*3)/free - raw memory
    >
    > 2. new T[3]/delete[] - buffer would be initialized to
    > default-constructed T objects.
    >
    > 3. operator new(sizeof(T)*3)/operator delete - raw memory
    >
    > What the best way of allocating memory for simple types and objects?
    > For objects the "new T[3]" looks like the best way since it initializes
    > the array, but what about simple types?
    > Right now for "int *" I am using malloc, but would it be better if I use
    > "operator new" for instance? What the advantages?


    If you are in any doubt, just stick with new/delete.

    --
    Ian Collins.
     
    Ian Collins, Sep 5, 2006
    #2
    1. Advertising

  3. Dmytro Bablinyuk

    Heinz Ozwirk Guest

    "Dmytro Bablinyuk" <> schrieb im
    Newsbeitrag news:ediiue$cej$...
    >I came across several possible ways of allocating memory for objects, for
    >example:
    >
    > 1. malloc(sizeof(T)*3)/free - raw memory
    >
    > 2. new T[3]/delete[] - buffer would be initialized to default-constructed
    > T objects.
    >
    > 3. operator new(sizeof(T)*3)/operator delete - raw memory
    >
    > What the best way of allocating memory for simple types and objects?
    > For objects the "new T[3]" looks like the best way since it initializes
    > the array, but what about simple types?
    > Right now for "int *" I am using malloc, but would it be better if I use
    > "operator new" for instance? What the advantages?


    Always do what is easy to learn and, even more important, easy to remember.
    For non-POD types new[]/delete[] (or new/delete for single instance) is the
    only reasonable way, and this also works for POD types. So there is no
    reason to use malloc at all. What if you change the type in the future.
    Would you like to spend hours replacing all those mallocs and frees, and
    later days to find the one place you missed? And will you know in a year
    when to use delete and when free in a long forgotton program? Why do you
    want to carry around two big hammers when you only need one to hit your
    thumb?

    Heinz
     
    Heinz Ozwirk, Sep 5, 2006
    #3
  4. Dmytro Bablinyuk

    Ron Natalie Guest

    Dmytro Bablinyuk wrote:

    >
    > 2. new T[3]/delete[] - buffer would be initialized to
    > default-constructed T objects.


    Only if T isn't POD. This inane behavior was specifically
    put in place to avoid the overhead of that overhead.
    Frankly I think it's silly.

    >
     
    Ron Natalie, Sep 5, 2006
    #4
  5. Dmytro Bablinyuk

    peter koch Guest

    Dmytro Bablinyuk wrote:
    > I came across several possible ways of allocating memory for objects,
    > for example:
    >
    > 1. malloc(sizeof(T)*3)/free - raw memory
    >
    > 2. new T[3]/delete[] - buffer would be initialized to
    > default-constructed T objects.
    >
    > 3. operator new(sizeof(T)*3)/operator delete - raw memory
    >
    > What the best way of allocating memory for simple types and objects?
    > For objects the "new T[3]" looks like the best way since it initializes
    > the array, but what about simple types?
    > Right now for "int *" I am using malloc, but would it be better if I use
    > "operator new" for instance? What the advantages?


    Are you absolutely sure you need a pointer? My best guess is that you
    should use std::vector or possibly some fixed-size array such as
    provided by boost.
    If std::vector is not possible, the only remaining choice is new [].
    malloc is to plain ugly and errorprone in C++.

    /Peter
     
    peter koch, Sep 5, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Office Drone
    Replies:
    7
    Views:
    18,271
    Billy N. Patton
    Jul 27, 2004
  2. Peter Hickman

    Best way to allocate a large amount of data

    Peter Hickman, Nov 30, 2004, in forum: C Programming
    Replies:
    6
    Views:
    446
    Dan Pop
    Dec 1, 2004
  3. Ralph Shnelvar
    Replies:
    44
    Views:
    427
    Albert Schlef
    Jan 7, 2010
  4. Replies:
    15
    Views:
    182
  5. Replies:
    8
    Views:
    311
    Jorgen Grahn
    May 16, 2013
Loading...

Share This Page