Best way to allocate memory for simple types and objects

  • Thread starter Dmytro Bablinyuk
  • Start date
D

Dmytro Bablinyuk

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?
 
I

Ian Collins

Dmytro said:
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.
 
H

Heinz Ozwirk

Dmytro Bablinyuk said:
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
 
R

Ron Natalie

Dmytro said:
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.
 
P

peter koch

Dmytro said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top