473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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?
Sep 5 '06 #1
4 2323
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.
Sep 5 '06 #2
"Dmytro Bablinyuk" <dm**************@rftechnology.com.auschrieb im
Newsbeitrag news:ed**********@news-02.connect.com.au...
>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

Sep 5 '06 #3
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.
>
Sep 5 '06 #4

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

Sep 5 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Chuck Ritzke | last post by:
I keep asking myself this question as I write class modules. What's the best/smartest/most efficient way to send a large object back and forth to a class module? For example, say I have a data...
4
by: alan | last post by:
Dear all, I am a newbie in C++. If I want to allocate memory to objects, I will use double *d = new double instead of ANSI-C malloc. But if I want to resize, say enlarge num of objects to...
4
by: Franklin Lee | last post by:
Hi All, I use new to allocate some memory,even I doesn't use delete to release them. When my Application exit, OS will release them. Am I right? If I'm right, how about Thread especally on...
6
by: Peter Hickman | last post by:
I have a program that requires x strings all of y length. x will be in the range of 100-10000 whereas the strings will all be < 200 each. This does not need to be grown once it has been created....
20
by: Joel Hedlund | last post by:
Hi all! I use python for writing terminal applications and I have been bothered by how hard it seems to be to determine the terminal size. What is the best way of doing this? At the end I've...
1
by: kiplring | last post by:
List<string> effectList = new List<string>(); effectList.Clear(); effectList = null; using (List<string> effectList = new List<string>()) { } If there are so many calls, I should save as...
6
by: kamsmartx | last post by:
I'm new to programming and need some help figuring out an algorithm. I need to design some kind of algorithm which will help us define capacity for one of our portfolios....here's the problem...
19
by: Daniel Pitts | last post by:
I have std::vector<Base *bases; I'd like to do something like: std::for_each(bases.begin(), bases.end(), operator delete); Is it possible without writing an adapter? Is there a better way? Is...
1
by: bizt | last post by:
Hi, I have a process where every so often I refresh a table's data using a setTimeout() .. and when its no longer needed, a clearTimeout(). The following is a simple example of how this is...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.