Connecting Tech Pros Worldwide Help | Site Map

Best way to allocate memory for simple types and objects

 
LinkBack Thread Tools Search this Thread
  #1  
Old September 5th, 2006, 01:05 AM
Dmytro Bablinyuk
Guest
 
Posts: n/a
Default Best way to allocate memory for simple types and objects

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?

  #2  
Old September 5th, 2006, 03:45 AM
Ian Collins
Guest
 
Posts: n/a
Default 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.
  #3  
Old September 5th, 2006, 06:35 AM
Heinz Ozwirk
Guest
 
Posts: n/a
Default 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

  #4  
Old September 5th, 2006, 06:55 AM
Ron Natalie
Guest
 
Posts: n/a
Default 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:
>
  #5  
Old September 5th, 2006, 09:05 AM
peter koch
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.