473,394 Members | 1,735 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,394 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 4956
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}"...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.