By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,129 Members | 1,331 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,129 IT Pros & Developers. It's quick & easy.

Is Vector Reserve guaranteed to allocate contiguous memory?

P: n/a

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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.