473,785 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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<cha r> 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
24 11037
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
whoops!that was a goof, hadn't finished my morning coffee....

dave

"Dave Townsend" <da********@com cast.net> wrote in message
news:Qb******** ************@co mcast.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.c om> 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<cha r> 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
"Julie" <ju***@nospam.c om> 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<cha r> 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<cha r> v;
v.reserve(s.len gth() + 1);
std::copy(s.beg in(), s.end(), std::back_inser ter(v));
v.push_back(0);

The code

std::vector<cha r> 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
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<cha r> 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<cha r> v;
std::copy(s.beg in(), s.end(), std::back_inser ter(v));
v.push_back('\0 ');
// yada yada yada
}

Jul 22 '05 #14
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<cha r> 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<cha r> 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
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<cha r> 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<cha r> 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<cha r> v;
v.reserve(s.siz e()+1); // This should prevent resizes
v.assign(s.begi n(), 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
On Fri, 11 Jun 2004 05:41:26 GMT, "Siemel Naran"
<Si*********@RE MOVE.att.net> wrote:

[snip]
In all implementations out there I know of, the use of
const_cast<cha r *>(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**********@Ho me.com
Jul 22 '05 #17
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**********@Ho me.com
Jul 22 '05 #18
"Jorge Rivera" <jo*****@roches ter.rr.com> wrote in message news:kUgyc.1424 80
std::vector<cha r> v;
v.reserve(s.siz e()+1); // This should prevent resizes
v.assign(s.begi n(), 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
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<cha r> 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

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

Similar topics

13
13096
by: Victor Hannak | last post by:
I am taking a program written in Borland C++ Builder 4 and converting the non-GUI related code to be generic c++ that can run anywhere. My main issue at this point is dealing with the string classes used in this program. All strings in this program are of the Borland AnsiString class. I would like to convert them over to use std::string. In order to keep from breaking the original program, I was hoping to use std::string everywhere...
10
8184
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is effectively impossible to forward declare std::string. (Yes I am aware that some libraries have a string_fwd.h header, but this is not portable.) That said, is there any real reason why I can't derive an otherwise empty
16
16429
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
8
4817
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
1
5061
by: Wild Wind | last post by:
Hello, I have the following methods I've written to convert from String* to std::string and back: static void ConvertFromStdString(std::string& inStr, System:String* outStr) { outStr = new String(inStr.c_str()); }
15
6905
by: roberts.noah | last post by:
Are there any decent benchmarks of the difference between using c strings and std::string out there? Google isn't being friendly about it. Obviously this would be dependant on different implementations but I don't care. I would be happy to find ANY comparison at this point if it was valid and semi scientifically done.
2
10937
by: pookiebearbottom | last post by:
Just looking for opinion on which of the 3 methods below people use in their code when they convert a 'const char *' to a 'const std::string &' came across #3 in someone's code and I had to think for a sec. At first I read it as converting a 'const char *' to a 'std::string *' void f(const std::string &s) { std::cout << s.size() << "\n";
11
2903
by: Jacek Dziedzic | last post by:
Hi! I need a routine like: std::string nth_word(const std::string &s, unsigned int n) { // return n-th word from the string, n is 0-based // if 's' contains too few words, return "" // 'words' are any sequences of non-whitespace characters // leading, trailing and multiple whitespace characters // should be ignored.
8
13765
by: Edson Manoel | last post by:
I have some C++ unmanaged code that takes std::string& arguments (as reference), and fills them (possibly growing the string). I want to call this code through PInvoke (DllImport), possibly using wrapper layers in unmanaged C++ and C#. I've thought about two approaches: 1) To pass a StringBuilder, this is converted to a char* in C++, the wrapper code converts the char* to a std::string (copy), and in the
0
9643
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10087
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9947
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8971
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.