By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,534 Members | 2,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,534 IT Pros & Developers. It's quick & easy.

Conversion from std::string to char *, is there a better way?

P: n/a
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;

The above:
- doesn't use any pointers that must be manually deallocated
- is 100% portable(?)
- is 100% conformant(?)

I've seen Bjarne's similar implementation, but it uses new char * instead of
vector (obviously written before the STL was adopted).

auto_ptr<char> in place of vector<char> doesn't work because it doesn't handle
arrays.

Does anyone have comments on the above, specifically as to its suitability for
the requirements defined?
Jul 22 '05 #1
Share this Question
Share on Google+
24 Replies


P: n/a
On Thu, 10 Jun 2004 10:48:33 -0700, Julie <ju***@nospam.com> wrote:
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
Might be a good idea to use parentheses (i.e.: &(v[0]) instead of
&v[0] here) ... I can never remember the order of the operator
precedence.
// use c, where a char * is _specifically_ required
s = c;

The above:
- doesn't use any pointers that must be manually deallocated
- is 100% portable(?)
- is 100% conformant(?)

I've seen Bjarne's similar implementation, but it uses new char * instead of
vector (obviously written before the STL was adopted).

auto_ptr<char> in place of vector<char> doesn't work because it doesn't handle
arrays.

Does anyone have comments on the above, specifically as to its suitability for
the requirements defined?


Looks OK to me.

std::string::c_str() will return a const char * pointing to the
string's character data which you can use to pass to functions which
expect constant pointers to zero-delimited C-style strings as
arguments. This is the simplest way to access the character data.

OTOH, if you have a function which expects a non-const char *, you
MUST copy the data from the constant buffer returned by c_str() to a
non-const buffer which you would generally allocate using "new
char[s.size()]", for example. It might be dangerous to cast away the
const if the function does anything but read the data (the behavior is
actually undefined by the standard).

Of course, your code would have to de-allocate the memory when done
with the buffer. I always use a smart pointer which can handle array
data (i.e. the destructor of the smart pointer calls "delete[]" and
not "delete"). The Boost library has such smart pointer templates. I
use one of my own which I adapted from code called the "grin pointer",
but the Boost library is expected to be incorporated into the next
version of the C++ standard.

The vector<char> is also a good solution, albeit one which might have
a bit more overhead (certainly not much, though) than using a simple
character array. Fortunately, the standard does guarantee that the
vector elements must be contiguous in memory, so assigning &(v[0]) to
a char * is OK.
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #2

P: n/a
Julie wrote:
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;

The above:
- doesn't use any pointers that must be manually deallocated
- is 100% portable(?)
Looks like it
- is 100% conformant(?)
As soon as the requirement for contiguousness of 'std::vector' storage
makes it into the Standard (if it hasn't already) and trickles down to
all library implementations (if they haven't done it already to begin
with).

I've seen Bjarne's similar implementation, but it uses new char * instead of
vector (obviously written before the STL was adopted).
No, it was written before there was a suggestion that std::vector's
storage needs to be contiguous.
Does anyone have comments on the above, specifically as to its suitability for
the requirements defined?


AFAICT, the code is fine.

V
Jul 22 '05 #3

P: n/a
Victor Bazarov wrote:
- is 100% conformant(?)

As soon as the requirement for contiguousness of 'std::vector' storage
makes it into the Standard (if it hasn't already) and trickles down to
all library implementations (if they haven't done it already to begin
with).


Just to remove any uncertainty here, from 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()."
Alan
Jul 22 '05 #4

P: n/a
Bob Hairgrove wrote:
<snip>

OTOH, if you have a function which expects a non-const char *, you
MUST copy the data from the constant buffer returned by c_str() to a
non-const buffer which you would generally allocate using "new
char[s.size()]", for example. It might be dangerous to cast away the
const if the function does anything but read the data (the behavior is
actually undefined by the standard).


new char(s.size() + 1]
You need space for the nul char.

- Pete

<snip>
Jul 22 '05 #5

P: n/a
Alan Johnson wrote:
Victor Bazarov wrote:
- is 100% conformant(?)


As soon as the requirement for contiguousness of 'std::vector' storage
makes it into the Standard (if it hasn't already) and trickles down to
all library implementations (if they haven't done it already to begin
with).


Just to remove any uncertainty here, from 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()."


I take it, that's from the newer edition of the Standard, correct?
Is that ISO/IEC 14882-2003?

V
Jul 22 '05 #6

P: n/a
Julie,

If you make use of the fact that string is actually a vector, you can do the
following,
which follows somewhat your approach, but results in much simpler and few
lines of code,
and doesn't require the explicit inclusion of the vector template class.
Other than that, its exactly the same underlying idea you posted.

dave

#include <string>
#include <iostream>
using namespace std;

int foobar( char* s )
{
// do some nasty modifications to demonstrage non-const ness of s.
s[16]='d';
s[17]='o';
s[18]='g';

s[41]='f';
s[42]='o';
s[43]='x';

return 0;
}

int main( int argc, char* argv[])
{
string foo("the quick brown fox jumped over the lazy dog");
string bar(foo);
// modify the copy of the original string
char* c = &bar[0];
foobar(c);

cout << "original string is \"" << foo << "\"\n";
cout << "modified string is \"" <<bar<< "\"\n";
return 0;
}

"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;

The above:
- doesn't use any pointers that must be manually deallocated
- is 100% portable(?)
- is 100% conformant(?)

I've seen Bjarne's similar implementation, but it uses new char * instead of vector (obviously written before the STL was adopted).

auto_ptr<char> in place of vector<char> doesn't work because it doesn't handle arrays.

Does anyone have comments on the above, specifically as to its suitability for the requirements defined?

Jul 22 '05 #7

P: n/a
(please don't top-post. rearranged.)
Dave Townsend wrote:

"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com... <snip>
Julie,

If you make use of the fact that string is actually a vector, you can
do the following,
which follows somewhat your approach, but results in much simpler and
few lines of code,
and doesn't require the explicit inclusion of the vector template
class. Other than that, its exactly the same underlying idea you
posted.

dave

#include <string>
#include <iostream>
using namespace std;

int foobar( char* s )
{
// do some nasty modifications to demonstrage non-const ness of s.
s[16]='d';
s[17]='o';
s[18]='g';

s[41]='f';
s[42]='o';
s[43]='x';

return 0;
}

int main( int argc, char* argv[])
{
string foo("the quick brown fox jumped over the lazy dog");
string bar(foo);
// modify the copy of the original string
char* c = &bar[0];
foobar(c);

cout << "original string is \"" << foo << "\"\n";
cout << "modified string is \"" <<bar<< "\"\n";
return 0;
}


A string is in no way a vector<char> or any vector. It's a typedef for
std::basic_string<char>.
Furthermore, your code invokes undefined behavior, because a string's memory
is not guarenteed to be contiguous.

- Pete

Jul 22 '05 #8

P: n/a
Dave Townsend wrote:

Julie,

If you make use of the fact that string is actually a vector, you can do the
following,
which follows somewhat your approach, but results in much simpler and few
lines of code,
and doesn't require the explicit inclusion of the vector template class.
Other than that, its exactly the same underlying idea you posted. <snip> string foo("the quick brown fox jumped over the lazy dog");
string bar(foo);

// modify the copy of the original string
char* c = &bar[0];
foobar(c);
Two problems that I'm aware of:
char* c = &bar[0];
/May/ be valid for c, but not c+1 as it isn't required that the underlying
character elements are stored contiguously.
foobar(c);


There is no guarantee that c is '\0' terminated, the standard doesn't require
that the underlying character array(s) are null terminated.

Comments?
Jul 22 '05 #9

P: n/a
Victor Bazarov wrote:
Alan Johnson wrote:
Victor Bazarov wrote:
- is 100% conformant(?)


As soon as the requirement for contiguousness of 'std::vector' storage
makes it into the Standard (if it hasn't already) and trickles down to
all library implementations (if they haven't done it already to begin
with).


Just to remove any uncertainty here, from 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()."

I take it, that's from the newer edition of the Standard, correct?
Is that ISO/IEC 14882-2003?

V


Correct.

Alan
Jul 22 '05 #10

P: n/a
Dave Townsend wrote:
Julie,

If you make use of the fact that string is actually a vector, you can do the
following,
which follows somewhat your approach, but results in much simpler and few
lines of code,
and doesn't require the explicit inclusion of the vector template class.
Other than that, its exactly the same underlying idea you posted.

dave

#include <string>
#include <iostream>
using namespace std;

int foobar( char* s )
{
// do some nasty modifications to demonstrage non-const ness of s.
s[16]='d';
s[17]='o';
s[18]='g';

s[41]='f';
s[42]='o';
s[43]='x';

return 0;
}

int main( int argc, char* argv[])
{
string foo("the quick brown fox jumped over the lazy dog");
string bar(foo);
// modify the copy of the original string
char* c = &bar[0];
foobar(c);

cout << "original string is \"" << foo << "\"\n";
cout << "modified string is \"" <<bar<< "\"\n";
return 0;
}


You are making the assumption that std::string stores its string in
continguous memory, which is NOT required by the standard.

Also, the standard explicitly includes rules that allow a reference
counted implementation of strings. If you ran this code on such an
implementation, then calling foobar as you have would most likely change
*both* strings.

Alan
Jul 22 '05 #11

P: n/a
whoops!that was a goof, hadn't finished my morning coffee....

dave

"Dave Townsend" <da********@comcast.net> wrote in message
news:Qb********************@comcast.com...
Julie,

If you make use of the fact that string is actually a vector, you can do the following,
which follows somewhat your approach, but results in much simpler and few
lines of code,
and doesn't require the explicit inclusion of the vector template class.
Other than that, its exactly the same underlying idea you posted.

dave

#include <string>
#include <iostream>
using namespace std;

int foobar( char* s )
{
// do some nasty modifications to demonstrage non-const ness of s.
s[16]='d';
s[17]='o';
s[18]='g';

s[41]='f';
s[42]='o';
s[43]='x';

return 0;
}

int main( int argc, char* argv[])
{
string foo("the quick brown fox jumped over the lazy dog");
string bar(foo);
// modify the copy of the original string
char* c = &bar[0];
foobar(c);

cout << "original string is \"" << foo << "\"\n";
cout << "modified string is \"" <<bar<< "\"\n";
return 0;
}

"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)
Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;

The above:
- doesn't use any pointers that must be manually deallocated
- is 100% portable(?)
- is 100% conformant(?)

I've seen Bjarne's similar implementation, but it uses new char *
instead of
vector (obviously written before the STL was adopted).

auto_ptr<char> in place of vector<char> doesn't work because it doesn't handle
arrays.

Does anyone have comments on the above, specifically as to its

suitability for
the requirements defined?


Jul 22 '05 #12

P: n/a
"Julie" <ju***@nospam.com> wrote in message
news:40***************@nospam.com...
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *) // presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;

The above:
- doesn't use any pointers that must be manually deallocated
- is 100% portable(?)
- is 100% conformant(?)
Yes to all 3 questions.

You can also do

std::vector<char> v;
v.reserve(s.length() + 1);
std::copy(s.begin(), s.end(), std::back_inserter(v));
v.push_back(0);

The code

std::vector<char> v(s.length() + 1);

creates a vector of chars and initializes each element to 0, which may be
inefficient in some cases (though usually not noticeable).

I've seen Bjarne's similar implementation, but it uses new char * instead of vector (obviously written before the STL was adopted).

auto_ptr<char> in place of vector<char> doesn't work because it doesn't handle arrays.

Does anyone have comments on the above, specifically as to its suitability for the requirements defined?


In all implementations out there I know of, the use of

const_cast<char *>(s.c_str()) would work, and you could modify the
characters. Not a good idea though.
Jul 22 '05 #13

P: n/a
Julie wrote:
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;


how about...

#include <string>
#include <vector>
#include <iterator>
#include <algorithm>

void f()
{
// misc code here....

std::string s("some text here");
std::vector<char> v;
std::copy(s.begin(), s.end(), std::back_inserter(v));
v.push_back('\0');
// yada yada yada
}

Jul 22 '05 #14

P: n/a
Julie wrote:
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;


This worked for me, I am not sure if this is just because of my
compilers implementation....

std::string s = "A null-terminated string";

std::vector<char> v(s.size()+1, 0); // Initialize all characters to 0
const char* csptr = s.c_str(); // Get a const pointer....
std::copy(csptr, csptr + s.size(), v.begin());// Use stl copy,not strcpy
char* fromVector = &v[0]; // Still the same you did

My only concern with this code is the validity of csptr after its
declaration. Maybe someone could enlighten me on weather this is safe
or not.

Also, think about only setting to 0 the final element,
v[v.size()-1] = 0;

JLR
Jul 22 '05 #15

P: n/a
Jorge Rivera wrote:
Julie wrote:
I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are
char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;


This worked for me, I am not sure if this is just because of my
compilers implementation....

std::string s = "A null-terminated string";

std::vector<char> v(s.size()+1, 0); // Initialize all characters to 0
const char* csptr = s.c_str(); // Get a const pointer....
std::copy(csptr, csptr + s.size(), v.begin());// Use stl copy,not strcpy
char* fromVector = &v[0]; // Still the same you did


I like this better....

std::vector<char> v;
v.reserve(s.size()+1); // This should prevent resizes
v.assign(s.begin(), s.end()); // Move the contents from string to vector
v.push_back(0); // Add the NULL-termination (Shouldn't resize)

Now THIS should be both portable, compliant, and decently efficient.

JLR
Jul 22 '05 #16

P: n/a
On Fri, 11 Jun 2004 05:41:26 GMT, "Siemel Naran"
<Si*********@REMOVE.att.net> wrote:

[snip]
In all implementations out there I know of, the use of
const_cast<char *>(s.c_str()) would work, and you could modify the
characters. Not a good idea though.

[snip]

This is not a good idea because your program could crash! Constant
data is often stored in read-only memory sections on most operating
systems. Writing to anything in such areas of RAM would most likely
cause an access violation.
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #17

P: n/a
On Thu, 10 Jun 2004 19:52:21 GMT, "Pete C." <x@x.x> wrote:
Bob Hairgrove wrote:
<snip>

You need space for the nul char.


Oops! You are quite correct.
--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #18

P: n/a
"Jorge Rivera" <jo*****@rochester.rr.com> wrote in message news:kUgyc.142480
std::vector<char> v;
v.reserve(s.size()+1); // This should prevent resizes
v.assign(s.begin(), s.end()); // Move the contents from string to vector
v.push_back(0); // Add the NULL-termination (Shouldn't resize)

Now THIS should be both portable, compliant, and decently efficient.


I like this, but be aware that many platforms have assembly level
optimizations of strcpy (and other functions like strchr, memcpy, memcmp,
etc) that utilize special CPU instructions. The use of v.assign or
std::copy does a byte by byte copy, and it appears most library and compiler
implementations don't provide specializations to turn the byte by byte copy
into the efficient strcpy.

So it's possible that the OP's original code is faster on some platforms.
One can only test to be 100% sure of what to do for one's particular
platform.
Jul 22 '05 #19

P: n/a
Julie wrote:

I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;

The above:
- doesn't use any pointers that must be manually deallocated
- is 100% portable(?)
- is 100% conformant(?)

I've seen Bjarne's similar implementation, but it uses new char * instead of
vector (obviously written before the STL was adopted).

auto_ptr<char> in place of vector<char> doesn't work because it doesn't handle
arrays.

Does anyone have comments on the above, specifically as to its suitability for
the requirements defined?


Thanks to everyone that replied -- I got the confirmation & answers I needed.

J
Jul 22 '05 #20

P: n/a
Julie wrote:

I'm re-evaluating the way that I convert from a std::string to char *.
(Requirement: the source is a std::string, the usable contents are char *)

Here is what I've come up with:

#include <string>
#include <vector>
#include <cstring>

// presume s from somewhere, such as:
std::string s = "<initial value>";

std::vector<char> v(s.length() + 1);
std::strcpy(&v[0], s.c_str());
char * c = &v[0];
// use c, where a char * is _specifically_ required
s = c;


So then, under the presumption that this is about as 'clean' as it can get,
does anyone have a comment on why a method for direct access wasn't provided in
the std::string implementation? I'd have presumed that something like that
would have been requisite to be more compatible w/ C and C-style strings and
interfaces.

Something like:

class string
{
private:
char * _accessbuf; // NULL initialized in constructor
public:

char * string::access(const size_t num_chars)
{
delete [] _accessbuf;
_accessbuf = new char[num_chars+1];
copy(_accessbuf, num_chars, 0);
_accessbuf[num_chars] = '\0';
return _accessbuf;
}

void string::commit(const bool commit=true)
{
if (_accessbuf)
{
if (commit)
{
*this = _accessbuf;
}
delete [] _accessbuf;
_accessbuf = NULL;
}
}
// etc.
};

Then, accessing C-style interfaces would resolve to:

GetFileName(str.access(_MAX_PATH), _MAX_PATH);
str.commit();

instead of the cumbersome method in my original post.

Comments?

(Note: the names access and commit are completely arbitrary, more appropriate
names probably exist.)
Jul 22 '05 #21

P: n/a
Julie wrote:
Julie wrote:

I'm re-evaluating the way that I convert from a std::string to char *.


Maybe I misunderstood your post, by I find string::c_str() works well for
me.

--
Chris Gordon-Smith
London
Homepage: http://graffiti.virgin.net/c.gordon-smith/
Email Address: Please see my Home Page
Jul 22 '05 #22

P: n/a
Chris Gordon-Smith wrote:

Julie wrote:
Julie wrote:

I'm re-evaluating the way that I convert from a std::string to char *.


Maybe I misunderstood your post, by I find string::c_str() works well for
me.


c_str() returns a const pointer, so you can't directly modify the contents.

My proposal is for something that would encapsulate my original code into two
methods that would return a non-const pointer so that the contents can be
directly modified.
Jul 22 '05 #23

P: n/a
On Sat, 26 Jun 2004 09:25:59 -0700, Julie wrote:
Chris Gordon-Smith wrote:
Julie wrote:
Julie wrote:

I'm re-evaluating the way that I convert from a std::string to char *.


Maybe I misunderstood your post, by I find string::c_str() works well for
me.


c_str() returns a const pointer, so you can't directly modify the contents.

My proposal is for something that would encapsulate my original code into two
methods that would return a non-const pointer so that the contents can be
directly modified.


Why, exactly, do you want to do this?

--
Some say the Wired doesn't have political borders like the real world,
but there are far too many nonsense-spouting anarchists or idiots who
think that pranks are a revolution.

Jul 22 '05 #24

P: n/a
Owen Jacobson wrote:

On Sat, 26 Jun 2004 09:25:59 -0700, Julie wrote:
Chris Gordon-Smith wrote:
Julie wrote:
Julie wrote:
>
> I'm re-evaluating the way that I convert from a std::string to char *.

Maybe I misunderstood your post, by I find string::c_str() works well for
me.
c_str() returns a const pointer, so you can't directly modify the contents.

My proposal is for something that would encapsulate my original code into two
methods that would return a non-const pointer so that the contents can be
directly modified.


Why, exactly, do you want to do this?


From my previous post:
... to be more compatible w/ C and C-style strings and interfaces.

Jul 22 '05 #25

This discussion thread is closed

Replies have been disabled for this discussion.