Advertisement

Passing STL string as char *??

Started by June 07, 2000 05:18 AM
18 comments, last by Void 24 years, 7 months ago
Yeah, the problem here is that if this callback function is writing into this string (as I assume it is, otherwise the const char* returned by c_str() would most likely suffice), it assumes a nice contiguous block of memory at the pointer location. The pointer that c_str() returns may point to a single byte of memory is the string is empty, so it''s obviously not safe to just write over that.

I have no idea which specific function you are using, though. Perhaps if you were more specific I could give a more accurate answer?
Try using StringToPass.data()
This returns a pointer to the first element of the sequence.
You might need to write (char *) StringToPass.data()
Hope this helps!


mhanna@nyc.rr.com
mhanna@nyc.rr.com
Advertisement
Gamera said:
quote:
Try using StringToPass.data()
This returns a pointer to the first element of the sequence.
You might need to write (char *) StringToPass.data()


basic_string::data () is exactly the same as basic_string::c_str (), and will return a const char*, giving you the same problem.

I''ve been following this thread, hoping for an elegant solution to your problem, but I don''t think there is one. I think you''re best off to pass a local character array, then assign it to your string. Yes, it''s one more copy than necessary, but that''s what you get when you use a language that doesn''t have a built-in string type (stupid C).
The function I''m using is the


D3DXGetErrorString


from D3DX util

I would like to avoid doing another buffer copy since I''ll be throwing an exception from the error code.. and I would like the exception object I throw to be as small as possible, just in case the exception is "Out of memory".. then trying to allocate more memory should fail since it is already out of memory..
I''ve had that problem before. I just went:

char* bop = string.c_str();
SomeFunc(bop);



ECKILLER
ECKILLER
Of course!!..

The "classic" way to modify const variables..Though I think you may to cast the c_str() to a (char *)..

Thanks there!!!...

..
Advertisement
Void, if you are just gonna get a non-const pointer to c_str() by doing something like:

char* bop = string.c_str();

You will probably just fuck up the String, for reasons given above. The const is there to help you, not to hinder you.
quote: Original post by Kylotan

Void, if you are just gonna get a non-const pointer to c_str() by doing something like:

char* bop = string.c_str();

You will probably just fuck up the String, for reasons given above. The const is there to help you, not to hinder you.


Yes, it is non-standard but it magically works..

If the concern here is the memory block is invalid, I could just reserve space beforehand (I believe there is a allocation routine)

Unless it''s internal reserve method will not return a contigous memory block (which naively I would think it''s just a new), then I don''t think it would fail, but I could be wrong..

..
The string class almost always stores the string's length separately from the char* itself, to make it quicker than strlen(), for example. So writing your own data into it will cause it to be 'wrong' in certain cases. And the end() iterator relies on knowing the correct length. And many of the functions depend on end() being accurate... etc. You might get away with using an stl string like that for a few very simple situations, but you are gonna run into a lot of problems if you actually try to use the string for anything much.

Edited by - Kylotan on June 14, 2000 9:44:03 AM
Understood!..

Thanks there..

This topic is closed to new replies.

Advertisement