473,387 Members | 1,585 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,387 software developers and data experts.

Is Vector Reserve guaranteed to allocate contiguous memory?


Is Reserve guaranteed to allocate contiguous memory?

How safe is the following:

vector<char> vbuff;

int sz = numeric_limits<short int>::max();

// reserve contiguous memory IS THIS ALWAYS TRUE ?

vbuff.reserve(sz);

ifstream file;

try {

file.open(fname.c_str(), ios_base::in);

// first line should be the equity ticker identifiers

file.getline(&vbuff[0], sz);

Thanks
Gary
Jul 23 '05 #1
2 5227
Gary Kuehn wrote in news:Q4******************@fe10.lga in comp.lang.c++:

Is Reserve guaranteed to allocate contiguous memory?
Yes, but its implementation defined if that memory will
actually be there when you need it.

Some OS's only bprovide real memory when you use it, if some other
process (ay) comes along and uses up all the real memory and
swap file between when you "reserve" memory an actaly use it
then ou're out of luck.

How safe is the following:

Not at all, you need resize, not reserve.

reserve only allocates memory, its not available until
you actually populate the vector.


vector<char> vbuff;

int sz = numeric_limits<short int>::max();

// reserve contiguous memory IS THIS ALWAYS TRUE ?

vbuff.reserve(sz);

vbuff.resize( sz );

Even better (for paranoid people anyway):

vbuff.resize( sz, '\0' );

as this will copy a 0 char to every location

It "uses" the memory, so you know its real not virtual.
ifstream file;

try {

file.open(fname.c_str(), ios_base::in);

// first line should be the equity ticker identifiers

file.getline(&vbuff[0], sz);


How about:

std:string s;
std::getline( file, s );

I.e. let the standard library do all the memory allocation.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 23 '05 #2
Thanks for the critique.
I'm reviewing all your input, thanks again.

Gary

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@216.196. 109.145...
Gary Kuehn wrote in news:Q4******************@fe10.lga in comp.lang.c++:

Is Reserve guaranteed to allocate contiguous memory?


Yes, but its implementation defined if that memory will
actually be there when you need it.

Some OS's only bprovide real memory when you use it, if some other
process (ay) comes along and uses up all the real memory and
swap file between when you "reserve" memory an actaly use it
then ou're out of luck.

How safe is the following:


Not at all, you need resize, not reserve.

reserve only allocates memory, its not available until
you actually populate the vector.


vector<char> vbuff;

int sz = numeric_limits<short int>::max();

// reserve contiguous memory IS THIS ALWAYS TRUE ?

vbuff.reserve(sz);


vbuff.resize( sz );

Even better (for paranoid people anyway):

vbuff.resize( sz, '\0' );

as this will copy a 0 char to every location

It "uses" the memory, so you know its real not virtual.
ifstream file;

try {

file.open(fname.c_str(), ios_base::in);

// first line should be the equity ticker identifiers

file.getline(&vbuff[0], sz);


How about:

std:string s;
std::getline( file, s );

I.e. let the standard library do all the memory allocation.

Rob.
--
http://www.victim-prime.dsl.pipex.com/

Jul 23 '05 #3

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...
9
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
34
by: Adam Hartshorne | last post by:
Hi All, I have the following problem, and I would be extremely grateful if somebody would be kind enough to suggest an efficient solution to it. I create an instance of a Class A, and...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
3
by: wenmang | last post by:
hi, I like to preallocate a vector of an element X, my question is after calling reserve(), is there a guarantee that memory is contiguous?
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
23
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat...
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: 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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.