Connecting Tech Pros Worldwide Help | Site Map

Best way to allocate memory for simple types and objects

Dmytro Bablinyuk
Guest
 
Posts: n/a
#1: Sep 5 '06
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?
Ian Collins
Guest
 
Posts: n/a
#2: Sep 5 '06

re: Best way to allocate memory for simple types and objects


Dmytro Bablinyuk wrote:
Quote:
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.
Heinz Ozwirk
Guest
 
Posts: n/a
#3: Sep 5 '06

re: Best way to allocate memory for simple types and objects


"Dmytro Bablinyuk" <dmytro.bablinyuk@rftechnology.com.auschrieb im
Newsbeitrag news:ediiue$cej$1@news-02.connect.com.au...
Quote:
>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

Ron Natalie
Guest
 
Posts: n/a
#4: Sep 5 '06

re: Best way to allocate memory for simple types and objects


Dmytro Bablinyuk wrote:
Quote:
>
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.
Quote:
>
peter koch
Guest
 
Posts: n/a
#5: Sep 5 '06

re: Best way to allocate memory for simple types and objects



Dmytro Bablinyuk wrote:
Quote:
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

Closed Thread