c++ string pointer to char*

Started by
19 comments, last by Paul C Skertich 11 years, 7 months ago
One more thing to keep in mind with c_str()... DO NOT SAVE A COPY.

Period, ever. Nope, very zilch never. Got it?

A c_str() pointer dies with the containing object, leaving your reference dangling like a hand grenade waiting to explode. Also, any non-const method call on the originating std::string object will invalidate your pointer.

Therefore, always request a new c_str().
Advertisement
In your original post only pointers are being copied. However c_str() can create a new internal cstring that IS guaranteed to be null terminated. Using that function is likely causing the extra memory usage. And as it was said before, any modification of the std::string or it's deletion will result in an the returned pointer from c_str() to be an invalid dangling pointer (. There is no way around this with std::string.c_str(). If you want other functionality such as a class that holds one and only one c string inside it then write your own, it isn't hard.

If you want to keep the value returned from c_str() you will need to copy it yourself.

[source lang="cpp"]

std::string mystr = "Hello world!";

const char *cstr = mystr.c_str();

char *copiedCStr = new char[strlen(cstr) + 1];

strcpy(copiedCStr, cstr);
[/source]

If this post or signature was helpful and/or constructive please give rep.

// C++ Video tutorials

http://www.youtube.com/watch?v=Wo60USYV9Ik

// Easy to learn 2D Game Library c++

SFML2.2 Download http://www.sfml-dev.org/download.php

SFML2.2 Tutorials http://www.sfml-dev.org/tutorials/2.2/

// Excellent 2d physics library Box2D

http://box2d.org/about/

// SFML 2 book

http://www.amazon.com/gp/product/1849696845/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1849696845&linkCode=as2&tag=gamer2creator-20


However c_str() can create a new internal cstring that IS guaranteed to be null terminated.

In C++03, yes. In C++11, no.


Using that function is likely causing the extra memory usage.

I actually find that very unlikely. The popular implementations that I know of certainly don't. It's technically possible, yes (if he's using C++03), but it's unlikely he's working with an implementation that does so, just because most implementations (both C++03 and C++11) don't create a whole copy of the string when c_str() is called. As the OP stated in a later post: "It was me that was doing the extra memory allocation I falsely blammed c_str()"


If you want to keep the value returned from c_str() you will need to copy it yourself.

std::string mystr = "Hello world!";

const char *cstr = mystr.c_str();

char *copiedCStr = new char[mystr.size() + 1]; // no need to call an O(n) function when an O(1) function is available

strcpy(copiedCStr, cstr);


Made a small improvement.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
@Serapth Im just using it for reading anyways its a encryption function so 1pointer for input and 1 for output is how im doing it

@Cornstalks your right it was my fault its not allocating any noticeable difference but it turns out im not using c++11 as when I try the c++11 threads it gives me something saying thats coming in the future so im still concerned that on various compiers/cross compiling and older version of c++ is used

To try to be on the safe side (im sure im not guaranteed anythingbut lets me sleep better) after getting some sleep I was able to setup the pointer with
(const unsigned char*)&(*data)[0]
That works just fine and fingers crossed will never allocate any memory across the board
I would not do that. Ever. Any sane implementation on which (const unsigned char*)&(*data)[0] works will not allocate any extra memory because c_str() can already just return the internal buffer. Any implementation which does allocate memory will not have a contiguous memory block for its string and (const unsigned char*)&(*data)[0] will just cause horrible problems.
There is no guarantee that the internal representation of a std::string is null terminated, so you could not pass that pointer to a function expecting a c string - presumably the very thing you are trying to accomplish.
As I and others have said, &(*data)[0] is horribly unsafe. Plus, it's an obvious enough operation that if it is a safe operation, you can expect your Standard Library implementors to have implemented c_str() as something like that. Your Standard Library implementors are actually brilliant people, and they won't unnecessarily copy a string if it can be avoided.

If you're so concerned about the performance of c_str(), why not just look at how it's defined and make sure it doesn't create a copy? My gcc implementation defines [font=courier new,courier,monospace]std::basic_string::c_str()[/font] as just [font=courier new,courier,monospace]_M_data()[/font], and [font=courier new,courier,monospace]_M_data()[/font] is defined as [font=courier new,courier,monospace]return _M_dataplus._M_p;[font=arial,helvetica,sans-serif], so it absolutely does not create a copy.[/font][/font]

[font=courier new,courier,monospace][font=arial,helvetica,sans-serif]Just check your implementation of c_str() if you're paranoid, and only use implementations that don't create a copy. But again, as I've said, it's unlikely you'll find an implementation that creates a copy of the data if you're not using an exotic system/compiler (and if you were, you'd probably already be aware of things like this).[/font][/font]
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
If you are concerned about copies of data going to memory and need to use a null terminated string anyway, why not use a char buffer? (Is that taboo now?)
@cornstalks Im not too concerned anymore knowing its not at least in my current system but I would almost rather it break than start swapping out and become horribly slow. Im just worried on cross compiling for arm devices since im sure they put far less time into the implementations than x86 but ill cross that path if it ever arises.

@yewbie lol pretty much I dont have to worry about null terminations and keeping the size for binary data, not that its difficult just why do it when strings are cleaner and easier well there is this scenario when I would have more control but I dont believe I need it now that we covered everything.

@cornstalks Im not too concerned anymore knowing its not at least in my current system but I would almost rather it break than start swapping out and become horribly slow.

The problem is if it doesn't break---at first. It could work for you and your test cases, and then for a set of customers horribly fail. Plus, even if it never breaks, it introduces a potential security flaw.


Im just worried on cross compiling for arm devices since im sure they put far less time into the implementations than x86 but ill cross that path if it ever arises.

I would still be very surprised if an ARM implementation had c_str() create and return a copy. It's more work for the programmer who writes the std::basic_string implementation to create a string class that uses multiple allocations instead of one contiguous allocation, and it's even more work to create a copy than to return a pointer to it. So if they're lazy, c_str() is, in my opinion, even more likely to not create a copy.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

This topic is closed to new replies.

Advertisement