开发者

c++ stl string <bad ptr> but there is string value what does it mean?

I have a C++ application that I'm setting a simple text string in std::string like this:

string foo = "my test string";

but when I see it in the debugger I'm getting this:

{_Buf=0x09d55c50 "my test string" _Ptr=0x74736e69 "‹MQhDqrtV‹ר‹VPכ‹URhDqrtP‹ר…uu‹Muƒֱ$טFo" }

or in a other string I'm getting this:

{_Buf=0x09d55c70 "some other string" _P开发者_JS百科tr=0x7372614c <Bad Ptr> }

I'm hand typing it in to Visual Studio Express 2008, of course I found it after some other lib that was expecting this string kept getting exceptions.

When I changed it to foo=""; everything worked fine.

The function of the lib api looks like this:

connect(const std::string& hostName, const std::string& userName, const std::string& password) 

(MySQL C++ connector)

My question is what is this _Ptr, and more importantly why am I getting exceptions?

UPDATE

ok its really strange but the thing that cause me the exception is the length off the string only if i set 15 characters long string it go's ok but if i set longer string exception is throwen . im using visual studio 2008 express 32 bit


I think that what you're seeing is the "small string optimization" and _Ptr isn't valid (or it's being used for space) for your short strings.

EDIT: If you're passing std::string to another library as a string and not a char* then you need to make sure that your application is built with exactly the same compiler, version, options, optimization, debug settings, and standard library as the external library was. Otherwise the string ABI won't be guaranteed to be compatible and you'll get all sorts of unexplicable behavior.


Are you building in debug mode? The library is probably set up to accept release mode strings. I suspect you'll find building for release will solve your issues.


Those memory addresses appear to be in kernel space. Check to see if your calling convention is wrong. It would help if you could post a stack trace.


IMO, _Ptr variable shouldn't be of concern - this is just of _Elem type, which is char for basic_string. It means char* - a basic memory location, not necessarily a valid C-string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜