Jump to content
  • Advertisement
Sign in to follow this  
jeff8j

c++ string pointer to char*

This topic is 2217 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to avoid making an unnecessary copy of my data as I believe string->c_str() would do
so im trying to point directly to the string data like &string[0]
but my problem is I have a string pointer like this


void somefunction( std::string* data ){
someotherfunction( data->c_str() ); //needs char*
}


how can I do the equivalent of &data[0] with the pointer?

Share this post


Link to post
Share on other sites
Advertisement
edit: Sorry I completely misread your post.

Time for some more coffee =) Edited by yewbie

Share this post


Link to post
Share on other sites
Your code should work as long as [font=courier new,courier,monospace]someotherfunction[/font] takes a [font=courier new,courier,monospace]const char*[/font].

If the problem is that it takes a [font=courier new,courier,monospace]char*[/font] (but does promise not to write to the buffer), then you can use the const-correctness hack:someotherfunction( const_cast<char*>(data->c_str()) );//C++ style
someotherfunction( (char*)(data->c_str()) );//or C-style casting

Share this post


Link to post
Share on other sites
@yewbie Im not quite sure what your doing there and it seems like your still make duplicates of the string

@hodgman It works width the ->c_str() but im trying to avoid making an extra copy of the data thats why I was trying to point directly to the data

Now that im looking at everything im not sure c_str() is causing the extra data I think it might of been me so I guess going up a step does c_str() allocate any memory or is it a pointer to the data?

Share this post


Link to post
Share on other sites
Jeff, check out this link, it has some interesting reading on the subject:

http://bytes.com/topic/c/answers/164645-does-c_str-property-string-class-allocate-memory

Share this post


Link to post
Share on other sites

Your code should work as long as [font=courier new,courier,monospace]someotherfunction[/font] takes a [font=courier new,courier,monospace]const char*[/font].

If the problem is that it takes a [font=courier new,courier,monospace]char*[/font] (but does promise not to write to the buffer), then you can use the const-correctness hack:someotherfunction( const_cast<char*>(data->c_str()) );//C++ style
someotherfunction( (char*)(data->c_str()) );//or C-style casting




Which by the way, should ring the warning bells and blare the sirons. Simple put, casting away const is perhaps the biggest code smells you will ever encounter.

Not to say it isn't the right way to go, just that it is something you need to be extremely weary off. It *IS* a code smell, it is just possible what stinks is in fact a badly coded library and not your code.

Share this post


Link to post
Share on other sites
Thanks yewbie
It was me that was doing the extra memory allocation I falsely blammed c_str() but that page brings up a question it says maybe it allocates memory maybe not. Im handling big data on little resources so maybe makes a copy is a bit of an alarm is there a way to do my original idea of &data[0] but for strings that way I can make sure it gets a direct pointer?

Im not worried about writing back to it it will remain a const so no worry there. Im just trying to guarantee 2 100MB strings and program and os overhead can run safely on a 512MB device.

Share this post


Link to post
Share on other sites

Now that im looking at everything im not sure c_str() is causing the extra data I think it might of been me so I guess going up a step does c_str() allocate any memory or is it a pointer to the data?

In C++11, c_str() and data() are the same, and are just a pointer to the data (not a pointer to a copy of the data). Calling these functions is O(1). In C++03, however, c_str() may return a copy of the internal buffer (iirc, C++03 didn't require strings to be stored in a contiguous buffer like in C++11). For all practical purposes, chances are your compiler (if using C++03) does use a contiguous internal buffer and that c_str() just returns a pointer to it. Note that &data()[0] is not safe! There's no guarantee the internal string is null-character terminated (in C++03), plus (again, iirc) there's no guarantee that it points to a contiguous buffer. However, most implementations will use a contiguous array, so just using c_str() should be good enough and should never create a copy (unless you're working on an exotic system or with a weird compiler) (this is in C++03; C++11 is of course as I previously mentioned). Edited by Cornstalks

Share this post


Link to post
Share on other sites
Thanks Cornstalks thats good news now I would assume its c++11 not sure about the cross compiler though but ill give it the benefit of the doubt for now.

@SiCrane Thats what I was thinking but I get the warning warning: cast to pointer from integer of different size and a seg fault when running

Thanks everyone its much clearer in my head now everything is working with the c_str()

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!