ifstream::read and pointers to std::vector

Discussion in 'C++' started by Alex Vinokur, Aug 10, 2005.

  1. Alex Vinokur

    Alex Vinokur Guest

    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 (?).


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Aug 10, 2005
    #1
    1. Advertising

  2. Alex Vinokur

    Alipha Guest

    Alex Vinokur wrote:
    > 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
    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
     
    Alipha, Aug 10, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    20
    Views:
    4,388
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    755
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    8
    Views:
    1,987
    Csaba
    Feb 18, 2006
  4. Rune Allnor
    Replies:
    4
    Views:
    983
    Rune Allnor
    Dec 11, 2008
  5. mathieu
    Replies:
    12
    Views:
    1,265
    Jeff Flinn
    May 25, 2011
Loading...

Share This Page