ifstream::read and pointers to std::vector

A

Alex Vinokur

Testsuites "Comparative Performance Measurement. Reading file into
string" at
http://groups.google.com/group/perfo/msg/8273f4d1a05cfbd1
http://groups.google.com/group/sources/msg/27a9b6f91239c909
contain several algorithms including the following ones.

==================================
ifstream ifs; // input file stream
string ret_str;

### CPP-21: std::vector and std::copy
------------------------------­------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ostream_iterator<char> out(oss);
copy (&v[0], &v[v.size()], out);
ret_str = oss.str();
------------------------------­------------------
### CPP-23: std::vector and istream::read()
------------------------------­------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ret_str = (v.empty() ? string() : string (v.begin(), v.end()));
------------------------------­------------------
==================================

Eljay Love-Jensen wrote me in private message:
std::vector does not guarantee that the memory is one contiguous block.
Your particular implementation may-or-may-not provide that guarantee -- but
reliance upon that particular implementation behavior is not portable.

So, my algorithms CPP-21 and CPP-23 are not legal because the following
two line are not legal (?).

vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);

So, we can't use read() with pointers to std::vector (?).
 
A

Alipha

Alex said:
Testsuites "Comparative Performance Measurement. Reading file into
string" at
http://groups.google.com/group/perfo/msg/8273f4d1a05cfbd1
http://groups.google.com/group/sources/msg/27a9b6f91239c909
contain several algorithms including the following ones.

==================================
ifstream ifs; // input file stream
string ret_str;

### CPP-21: std::vector and std::copy
------------------------------­------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ostream_iterator<char> out(oss);
copy (&v[0], &v[v.size()], out);
ret_str = oss.str();
------------------------------­------------------
### CPP-23: std::vector and istream::read()
------------------------------­------------------
vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);
ret_str = (v.empty() ? string() : string (v.begin(), v.end()));
------------------------------­------------------
==================================

Eljay Love-Jensen wrote me in private message:
std::vector does not guarantee that the memory is one contiguous block.
Your particular implementation may-or-may-not provide that guarantee -- but
reliance upon that particular implementation behavior is not portable.

as of 2003, the C++ standard explicitly states that a std::vector uses
one contiguous block:

ISO/IEC 14882:2003, section 23.2.4-1:

"The elements of a vector are stored contiguously, meaning that if v is
So, my algorithms CPP-21 and CPP-23 are not legal because the following
two line are not legal (?).

vector<char> v (no_of_file_bytes);
ifs.read(&v[0], no_of_file_bytes);

So, we can't use read() with pointers to std::vector (?).

as of 2003, the above code is legal.
 

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

Staff online

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top