c++ string pointer to char*

Started by
19 comments, last by Paul C Skertich 11 years, 6 months ago
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?
Advertisement
edit: Sorry I completely misread your post.

Time for some more coffee =)
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
@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?
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

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

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).
[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 ]
Note if you want to use the subscript operator on a pointer to a string you can use (*data)[0].
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()

This topic is closed to new replies.

Advertisement