Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > ifstream::read and pointers to std::vector

Reply
Thread Tools

ifstream::read and pointers to std::vector

 
 
Alex Vinokur
Guest
Posts: n/a
 
      08-10-2005

Testsuites "Comparative Performance Measurement. Reading file into
string" at
http://groups.google.com/group/perfo...73f4d1a05cfbd1
http://groups.google.com/group/sourc...a9b6f91239c909
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 (?).


--
Alex Vinokur
email: alex DOT vinokur AT gmail DOT com
http://mathforum.org/library/view/10978.html
http://sourceforge.net/users/alexvn

 
Reply With Quote
 
 
 
 
Alipha
Guest
Posts: n/a
 
      08-10-2005

Alex Vinokur wrote:
> Testsuites "Comparative Performance Measurement. Reading file into
> string" at
> http://groups.google.com/group/perfo...73f4d1a05cfbd1
> http://groups.google.com/group/sourc...a9b6f91239c909
> 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
a vector<T, Allocator> where T is some type other than bool, then it
obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size()."

> 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.

>
>
> --
> Alex Vinokur
> email: alex DOT vinokur AT gmail DOT com
> http://mathforum.org/library/view/10978.html
> http://sourceforge.net/users/alexvn


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
pointers to constant characters and constant pointers to characters sam_cit@yahoo.co.in C Programming 4 12-14-2006 11:10 PM
Questions about pointers to objects and pointers to functions Marc Thrun C Programming 15 10-04-2005 05:47 PM
Learning pointers and arrays of pointers With Files ketema@gmail.com C Programming 1 03-28-2005 03:51 AM
Pointers and References (and References to Pointers) Roger Leigh C++ 8 11-17-2003 10:14 AM



Advertisments