Is Vector Reserve guaranteed to allocate contiguous memory?

G

Gary Kuehn

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
 
R

Rob Williscroft

Gary Kuehn wrote in 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.
 
G

Gary Kuehn

Thanks for the critique.
I'm reviewing all your input, thanks again.

Gary

Rob Williscroft said:
Gary Kuehn wrote in 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top