Jump to content

  • Log In with Google      Sign In   
  • Create Account


c++ string pointer to char*


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
20 replies to this topic

#1 jeff8j   Members   -  Reputation: 676

Like
2Likes
Like

Posted 20 September 2012 - 08:04 AM

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?

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


Sponsor:

#2 yewbie   GDNet+   -  Reputation: 665

Like
1Likes
Like

Posted 20 September 2012 - 08:15 AM

edit: Sorry I completely misread your post.

Time for some more coffee =)

Edited by yewbie, 20 September 2012 - 08:18 AM.


#3 Hodgman   Moderators   -  Reputation: 27690

Like
4Likes
Like

Posted 20 September 2012 - 08:15 AM

Your code should work as long as someotherfunction takes a const char*.

If the problem is that it takes a char* (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


#4 jeff8j   Members   -  Reputation: 676

Like
3Likes
Like

Posted 20 September 2012 - 08:22 AM

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

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


#5 yewbie   GDNet+   -  Reputation: 665

Like
1Likes
Like

Posted 20 September 2012 - 08:24 AM

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

#6 Serapth   Crossbones+   -  Reputation: 5181

Like
4Likes
Like

Posted 20 September 2012 - 08:29 AM

Your code should work as long as someotherfunction takes a const char*.

If the problem is that it takes a char* (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.

#7 jeff8j   Members   -  Reputation: 676

Like
3Likes
Like

Posted 20 September 2012 - 08:34 AM

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.

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


#8 Cornstalks   Crossbones+   -  Reputation: 6966

Like
4Likes
Like

Posted 20 September 2012 - 08:35 AM

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, 20 September 2012 - 08:38 AM.

[ 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 ]

#9 SiCrane   Moderators   -  Reputation: 9392

Like
2Likes
Like

Posted 20 September 2012 - 08:39 AM

Note if you want to use the subscript operator on a pointer to a string you can use (*data)[0].

#10 jeff8j   Members   -  Reputation: 676

Like
2Likes
Like

Posted 20 September 2012 - 08:44 AM

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()

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


#11 Serapth   Crossbones+   -  Reputation: 5181

Like
2Likes
Like

Posted 20 September 2012 - 11:51 AM

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().

#12 EddieV223   Members   -  Reputation: 1388

Like
0Likes
Like

Posted 20 September 2012 - 10:47 PM

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]

Edited by EddieV223, 20 September 2012 - 10:55 PM.

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.1 Download http://www.sfml-dev.org/download.php

SFML2.1 Tutorials http://www.sfml-dev.org/tutorials/2.1/

 

// 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

 


#13 Cornstalks   Crossbones+   -  Reputation: 6966

Like
1Likes
Like

Posted 20 September 2012 - 11:14 PM

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.

Edited by Cornstalks, 20 September 2012 - 11:16 PM.

[ 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 ]

#14 jeff8j   Members   -  Reputation: 676

Like
0Likes
Like

Posted 21 September 2012 - 12:53 AM

@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

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


#15 BitMaster   Crossbones+   -  Reputation: 3651

Like
0Likes
Like

Posted 21 September 2012 - 01:18 AM

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.

#16 rip-off   Moderators   -  Reputation: 7660

Like
0Likes
Like

Posted 21 September 2012 - 05:04 AM

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.

#17 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

Posted 21 September 2012 - 09:02 AM

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 std::basic_string::c_str() as just _M_data(), and _M_data() is defined as return _M_dataplus._M_p;, so it absolutely does not create a copy.

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).
[ 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 ]

#18 yewbie   GDNet+   -  Reputation: 665

Like
0Likes
Like

Posted 21 September 2012 - 11:13 AM

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

#19 jeff8j   Members   -  Reputation: 676

Like
0Likes
Like

Posted 21 September 2012 - 11:24 AM

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

Firefox youtube video and audio downloader MP3 MP4 OGG WEBM

https://addons.mozilla.org/en-US/firefox/addon/simple-youtube-converter/


#20 Cornstalks   Crossbones+   -  Reputation: 6966

Like
0Likes
Like

Posted 21 September 2012 - 11:34 AM

@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.
[ 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 ]




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS