Re: std::basic_string<> contiguous data?
Adam White wrote:
I've been writing a NetString class for network transmissions, which has
been internally implemented using a std::string.
To read data into the NetString from a network socket, I'd planned to
pre-allocate a large std::string (to use as a buffer), then slurp data
directly from the socket into the string via pointer indirection or by
using the std::string::data() array.
The pointer returned by std::string::data() is const; you can't
modify the memory it points to.
But is std::basic_string<>'s data guaranteed to be in a contiguous array?
No.
I believe that TR1 clarified the intention that std::vector<> data be
stored contiguously - is that also true of std::basic_string<> ?
No.
Even if it is contiguous, there is nothing which guarantees that
modifications through the pointer returned by data() will
affect the value of the string itself.
(In practice, of course, I think you'll probably be able to get
away with it in all real implementations:-).)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]