473,326 Members | 2,136 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

string question

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!

--
Kristofer Pettijohn
kr*******@cybernetik.net
Jul 19 '05 #1
6 4953
Kristofer Pettijohn wrote:
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>

--
WW aka Attila
Jul 19 '05 #2
Kristofer Pettijohn wrote:
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.

--
Best regards,
Andrey Tarasevich
Brainbench C and C++ Programming MVP

Jul 19 '05 #3
Kristofer Pettijohn wrote:
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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4
Kristofer Pettijohn wrote:
strcpy(mystring.c_str(), "Some string C-style"); // C-style copy

What compiler are you using that allows this?


Brian Rodenborn
Jul 19 '05 #5
Default User <fi********@company.com> writes:
Kristofer Pettijohn wrote:
strcpy(mystring.c_str(), "Some string C-style"); // C-style copy

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.
Jul 19 '05 #6
llewelly wrote:
Default User <fi********@company.com> writes:

Kristofer Pettijohn wrote:

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

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.


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
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: sparks | last post by:
I know this is a stupid question but I can't find the answer. Please tell me where to find reference to this so I can print it out and staple it to my head.. dim I as integer dim missedstr as...
5
by: John Baro | last post by:
I have a richtextbox which I want the "literal" rtf of. richtextbox.rtf returns {\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang1033\\uc1 }\r\n\0 when i put this into a string I get...
5
by: Dave | last post by:
I'm receiving info from a com port into a string. I gradually process the string which constantly shortens it. The question is how long can a string be before I need to write some info to disk...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
2
by: Dan Schumm | last post by:
I'm relatively new to regular expressions and was looking for some help on a problem that I need to solve. Basically, given an HTML string, I need to highlight certain words within the text of the...
11
by: Zordiac | last post by:
How do I dynamically populate a string array? I hope there is something obvious that I'm missing here Option Strict On dim s() as string dim sTmp as string = "test" dim i as integer ...
53
by: Jeff | last post by:
In the function below, can size ever be 0 (zero)? char *clc_strdup(const char * CLC_RESTRICT s) { size_t size; char *p; clc_assert_not_null(clc_strdup, s); size = strlen(s) + 1;
6
by: tommaso.gastaldi | last post by:
Hi, does anybody know a speedy analog of IsNumeric() to check for strings/chars. I would like to check if an Object can be treated as a string before using a Cstr(), clearly avoiding the time...
39
by: sucaba.r | last post by:
I don't know if this is a unique problem, or I'm going about it the wrong way. I currently connect to one of our SQL servers via a priviliged account (by using RUNAS). Works with no problem. I...
7
by: Sky | last post by:
I have been looking for a more powerful version of GetType(string) that will find the Type no matter what, and will work even if only supplied "{TypeName}", not the full "{TypeName},{AssemblyName}"...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.