473,401 Members | 2,139 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,401 software developers and data experts.

about std::vector < int > - memory allocation

This is about the vector template defined in standard C++ .

Suppose I want to create a *huge* vector , as follows.
void f1() {
vector < int > data(1024); //may be not very huge, but for
//demonstration purposes

//Process 'data'
//'data' goes out of scope
//and storage reclaimed automatically
}

void f2() {
vector < int > * p = new vector <int>f1(1024); //

//Process 'p'

delete p; //Explicitly reclaim storage.
}
My question is, given that we want to create a huge vector , which one
among the two is better ?

* If this had been not been a container, then I would
have chosen 'f1()' because it does not involve pointers.

* If this had been not been a container but a *big object*,
may be 'f2()' might be better.

* Given that vector manages dynamic memory allocation internally, are
there any important differences between 'f1()' and 'f2()' at all ?
I am little bit confused here. Please let me know your comments.

Jul 22 '05 #1
3 2248
* Rakesh Sinha:
This is about the vector template defined in standard C++ .

Suppose I want to create a *huge* vector , as follows.
void f1() {
vector < int > data(1024); //may be not very huge, but for
//demonstration purposes

//Process 'data'
//'data' goes out of scope
//and storage reclaimed automatically
}

void f2() {
vector < int > * p = new vector <int>f1(1024); //

//Process 'p'

delete p; //Explicitly reclaim storage.
}
My question is, given that we want to create a huge vector , which one
among the two is better ?

* If this had been not been a container, then I would
have chosen 'f1()' because it does not involve pointers.

* If this had been not been a container but a *big object*,
may be 'f2()' might be better.

* Given that vector manages dynamic memory allocation internally, are
there any important differences between 'f1()' and 'f2()' at all ?
I am little bit confused here. Please let me know your comments.


f2 is less efficient, not exception safe, and a notational nightmare.

Is this a homework question, or have you actually tried it (it should
be impossible to be unaware of the drawbacks if you have tried it)?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 22 '05 #2

Alf P. Steinbach wrote:

f2 is less efficient, not exception safe, and a notational nightmare.

Is this a homework question,
No - this is not.
or have you actually tried it (it should
be impossible to be unaware of the drawbacks if you have tried it)?


Indeed I had tried it, but it was just a co-incidence that I did not
get any exception thrown between new and delete . Thanks for letting me
the information though.

Jul 22 '05 #3
Rakesh Sinha wrote:
This is about the vector template defined in standard C++ .

Suppose I want to create a *huge* vector , as follows.
void f1() {
vector < int > data(1024); //may be not very huge, but for
//demonstration purposes

//Process 'data'
//'data' goes out of scope
//and storage reclaimed automatically
}

void f2() {
vector < int > * p = new vector <int>f1(1024); //

//Process 'p'

delete p; //Explicitly reclaim storage.
}
My question is, given that we want to create a huge vector , which one
among the two is better ?

* If this had been not been a container, then I would
have chosen 'f1()' because it does not involve pointers.

* If this had been not been a container but a *big object*,
may be 'f2()' might be better.

* Given that vector manages dynamic memory allocation internally, are
there any important differences between 'f1()' and 'f2()' at all ?
I am little bit confused here. Please let me know your comments.


Since vector<> generally dynamically allocates its buffer anyway, what
you get with the first is simply the bookkeeping portion of the vector
allocated in automatic storage, and you get the automatic deletion when
it goes out of scope.

The second form is (as was noted) a notational nightmare, and you've got
to keep track of the pointer, and delete it -- at this point, you might
as well have dynamically allocated 1024 ints (with new int[1024]) and
have been done with it.
Jul 22 '05 #4

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

Similar topics

10
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to...
2
by: Chris Thompson | last post by:
Hi I'm writing a p2p client for an existing protocol. I used a std::vector<char> as a buffer for messages read from the server. The message length is the first 4 bytes. The message code the...
11
by: Steve | last post by:
Hi, I'm using a std::vector to store a list of user defined objects. The vector may have well over 1000 elements, and I'm suffering a performance hit. If I use push_back I get a much worse...
11
by: Lynn | last post by:
Hi, Is it better to use std::vector <type> with a pointer to the types being stored or the actual storage for the types ? In other words, is std::vector <mytype> or std::vector <mytype *>...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
24
by: simon | last post by:
Hi, First some background. I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2;
4
by: Bobrick | last post by:
Hi. I'm in the process of making a GUI for a function someone else wrote, and i've come across a type i'm unfamiliar with, namely "std::vector<unsigned char>". I need to get the contents of this...
1
by: sergio311 | last post by:
Hi all, I'm new of c++ and i'm writing a class that create a matrix of data (of T type, it's a template class). The member variable m_data must stores the matrix's data and i prefer use heap so,...
2
by: Tim | last post by:
Dear All, std::vector seems using the copy constructor to allocate memory when needed. Is it possible to control the memory allocation, such as by using my own allocator? My question is as...
8
by: jacek.dziedzic | last post by:
Hi! I need to be able to track memory usage in a medium-sized application I'm developing. The only significant (memory-wise) non- local objects are of two types -- std::vector<and of a custom...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.