VS2010中令人困惑的std :: string :: c_str()行为 [英] Confusing std::string::c_str() behavior in VS2010

查看:224
本文介绍了VS2010中令人困惑的std :: string :: c_str()行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我确定我做错了什么,但是对于我一生,我不知道该怎么办!请考虑以下代码:

I'm sure I've done something wrong, but for the life of me I can't figure out what! Please consider the following code:

cerr<<el.getText()<<endl;
cerr<<el.getText().c_str()<<endl;
cerr<<"---"<<endl;
const char *value = el.getText().c_str();
cerr<<"\""<<value<<"\""<<endl;
field.cdata = el.getText().c_str();
cerr<<"\""<<field.cdata<<"\""<<endl;

el 是XML元素,而 getText 返回一个std :: string。与预期的一样, el.getText() el.getText()。c_str()打印相同的值。但是,当 value 设置为 时-即空字符串-当它分配 c_str()。编写此代码是为了设置 field.cdata = value ,因此正在清除它。将其更改为设置为 value 的假定相同的表达式后,它可以正常工作,并且最后一行显示期望值。

el is an XML element and getText returns a std::string. As expected, el.getText() and el.getText().c_str() print the same value. However, value is set to "" - that is, the empty string - when it assigned the result of c_str(). This code had been written to set field.cdata=value, and so was clearing it out. After changing it to the supposedly-identical expression value is set from, it works fine and the final line prints the expected value.

由于 el 在堆栈中,我以为我可能一直在破坏它-即使在 value 之后设置后, el 中的基础值仍然正确。

Since el is on the stack, I thought I might have been clobbering it - but even after value is set, the underlying value in el is still correct.

我的下一个想法是,将常量分配给const指针存在一些奇怪的编译器特定问题,所以我写了以下内容:

My next thought was that there was some weird compiler-specific issue with assigning things to const pointers, so I wrote the following:

std::string thing = "test";
std::cout << thing << std::endl;
std::cout << thing.c_str() << std::endl;
const char* value = thing.c_str();
std::cout << value << std::endl;

正如预期的那样,我得到了'test'3次。

As expected, I get 'test' three times.

所以现在我不知道发生了什么。显而易见,示例中没有发生某些奇怪的事情,但是我不知道它是什么,而且我对如何继续查找一无所知。有人可以启发我,或者至少指出我正确的方向吗?

So now I have no clue what is going on. It would seem obvious that there is something strange going on in my program that's not happening in the sample, but I don't know what it is and I'm out of ideas about how to keep looking. Can somebody enlighten me, or at least point me in the right direction?

推荐答案

我认为 el .getText()返回一个临时 string 对象。当该对象被销毁时, c_str()返回的指针不再有效(请记住,这是 c_str( )也可以无效)。

I assume that el.getText() is returning a temporary string object. When that object is destroyed the pointer returned by c_str() is no longer valid (keep in mind that that are other ways the pointer returned by c_str() can be invalidated, too).

临时对象将在其创建的完整表达式的结尾处被销毁(通常是

The temporary object will be destroyed at the end of the full expression it's created in (which is generally at the semi-colon in your example above).

您也许可以通过以下方式解决问题:

You may be able to solve your problem with something like the following:

const char *value = strdup(el.getText().c_str());

会创建字符串的副本作为原始的 char 动态分配的内存中的数组。然后,当不再需要该数据时,您便有责任在该指针上调用 free()

which creates a copy of the string as a raw char array in dynamically allocated memory. You then become responsible for calling free() on that pointer at some point when that data is no longer needed.

这篇关于VS2010中令人困惑的std :: string :: c_str()行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆