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

J

Julie

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?
 
B

Bob Hairgrove

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.
 
V

Victor Bazarov

Julie said:
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
 
A

Alan Johnson

Victor said:
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
 
V

Victor Bazarov

Alan said:
Victor said:
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
 
P

Pete C.

Bob Hairgrove wrote:
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>
 
D

Dave Townsend

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;
}
 
P

Pete C.

(please don't top-post. rearranged.)
Dave said:
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
 
J

Julie

Dave said:
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.
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?
 
A

Alan Johnson

Victor said:
Alan said:
Victor said:
- 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
 
A

Alan Johnson

Dave said:
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
 
D

Dave Townsend

whoops!that was a goof, hadn't finished my morning coffee....

dave

Dave Townsend said:
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 said:
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?
 
S

Siemel Naran

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.
 
R

red floyd

Julie said:
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
}
 
J

Jorge Rivera

Julie said:
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
 
J

Jorge Rivera

Jorge said:
Julie said:
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
 
B

Bob Hairgrove

On Fri, 11 Jun 2004 05:41:26 GMT, "Siemel Naran"

[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.
 
S

Siemel Naran

Jorge Rivera said:
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.
 
J

Julie

Julie said:
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
 

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

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top