string question

  • Thread starter Kristofer Pettijohn
  • Start date
K

Kristofer Pettijohn

I'm a bit unsure about this and the results... but given the following:

....
std::string mystring;
mystring.reserve(256);

strcpy(mystring.c_str(), "Some string C-style"); // C-style copy
....

Is something like this even legal? I've tested it, and it works,
but I just don't know if its "proper".

of course, I wouldn't use strcpy; I'm inquiring because I'd like to use
BSD read()'s without using char[]'s.

Thanks!
 
W

White Wolf

Kristofer said:
I'm a bit unsure about this and the results... but given the
following:

...
std::string mystring;
mystring.reserve(256);

strcpy(mystring.c_str(), "Some string C-style"); // C-style copy
...

Is something like this even legal?
No.

I've tested it, and it works,
but I just don't know if its "proper".

It is not. In standard C++ std::string::c_str() returns a const pointer
_and_ special care has been taken in the standard to ensure that library
implementations can give you a char buffer, which is a *copy* of the string
internals.
of course, I wouldn't use strcpy; I'm inquiring because I'd like to
use BSD read()'s without using char[]'s.

Then read into an std:: vector<char>
 
A

Andrey Tarasevich

Kristofer said:
I'm a bit unsure about this and the results... but given the following:
...
std::string mystring;
mystring.reserve(256);

strcpy(mystring.c_str(), "Some string C-style"); // C-style copy
...

Is something like this even legal? I've tested it, and it works,
but I just don't know if its "proper".
...

No, it is not legal. It won't work for several reasons.

Firstly, in general case the pointer returned by 'c_str()' doesn't
really give you access to the innards of a 'std::string' object. In
other words, there is no guarantee that returned pointer points to the
actual character sequence controlled by this 'std::string' object. It is
possible that 'c_str()' returns a pointer to a temporary buffer
allocated specifically for this purpose. The modifications may affect
this temporary buffer, but have no effect on the actual character
sequence controlled by this 'std::string' object.

Secondly, the character sequence stored in a 'std::string' object is not
guaranteed to reside in a continuous block of memory. It can be split
across several blocks.

Thirdly, null-character has no special meaning within character sequence
stored in a 'std::string' object. 'std::string's (unlike C-strings) are
not "terminated" by any special character, which means that in one way
or another 'std::string' objects have to keep the current length of the
stored sequence as a separate piece(s) of data. For this reason, all
operations that may modify the length of the sequence must go through
'std::string's public interface, thus giving 'std::string' objects the
ability keep the length information up-to-date. Any attempts to "hack
around" that interface using, for example, direct pointers to internal
string data, will almost inevitably destroy the integrity of
'std::string' object.
 
K

Kevin Goodsell

Kristofer said:
I'm a bit unsure about this and the results... but given the following:

...
std::string mystring;
mystring.reserve(256);

There is no reserve() member for std::string. That's a vector-only function.
strcpy(mystring.c_str(), "Some string C-style"); // C-style copy

Clearly wrong, for reasons that others have explained.

-Kevin
 
L

llewelly

Default User said:
What compiler are you using that allows this?

IIRC MSVC6 and 7 will compile this. Howver, they don't 'allow' it
per se; it has weird behavior.
 
K

Kevin Goodsell

llewelly said:
IIRC MSVC6 and 7 will compile this. Howver, they don't 'allow' it
per se; it has weird behavior.

I tried the following code in MSVC 6:

#include <string>
#include <string.h>


int main()
{
std::string mystring;
mystring.reserve(256);

strcpy(mystring.c_str(), "Some string C-style"); // C-style copy

return 0;
}

It gave the following error:

C:\Documents and Settings\Kevin\My Documents\temp\fun\fun.cpp(10) :
error C2664: 'strcpy' : cannot convert parameter 1 from 'const char *'
to 'char *'
Conversion loses qualifiers


-Kevin
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top