std::string 构造函数中的迭代器列表已损坏
下面在 VS2005 SP1 的调试配置中编译的代码显示了两条带有“ITERATOR LIST CORRUPTED”通知的消息。
代码片段
#define _SECURE_SCL 0
#define _HAS_ITERATOR_DEBUGGING 0
#include <sstream>
#include <string>
int main()
{
std::stringstream stream;
stream << "123" << std::endl;
std::string str = stream.str();
std::string::const_iterator itFirst = str.begin();
int position = str.find('2');
std::string::const_iterator itSecond = itFirst + position;
std::string tempStr(itFirst,itSecond); ///< errors are here
return 0;
}
这是编译器或标准库中的错误吗?
The code below compiled in Debug configuration in VS2005 SP1 shows two messages with “ITERATOR LIST CORRUPTED” notice.
Code Snippet
#define _SECURE_SCL 0
#define _HAS_ITERATOR_DEBUGGING 0
#include <sstream>
#include <string>
int main()
{
std::stringstream stream;
stream << "123" << std::endl;
std::string str = stream.str();
std::string::const_iterator itFirst = str.begin();
int position = str.find('2');
std::string::const_iterator itSecond = itFirst + position;
std::string tempStr(itFirst,itSecond); ///< errors are here
return 0;
}
Is it a bug in the compiler or standard library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的坏!编辑:是的,编译器有问题。请参阅此 - 特别是社区内容部分。
My bad! Edit: Yeah problem with compiler. See this -- particularly the Community Content section.
@dirkgently 在他的编辑中说了什么。
显然,std::string的一些代码位于运行时dll中,特别是宏定义对构造函数不生效,而迭代器调试的代码被执行。您可以通过静态链接运行时库来解决此问题。
我认为这是一个错误,尽管可能不是在 Visual Studio 本身中,而是在文档中。
What @dirkgently said in his edit.
Apparently, some code for
std::string
is located in the runtime dll, in particular the macro definition does not take effect for the constructor an the code for iterator debugging gets executed. You can fix this by linking the runtime library statically.I would consider this a bug, though perhaps not in the Visual Studio itself, but in the documentation.
您的代码有问题。好吧,事实上有几个:
std.find('2')
返回一个size_t
,如果size_t
的值存在潜在的转换问题> 返回的(如std::string::npos
)优于int
可以容纳的内容(我认为你最终会得到一个负 int...)position
为负数,或等于std::string::npos
那么范围itFirst,itSecond
定义不明确(因为>itSecond
在itFirst
之前,或者因为它在str.end()
之后)更正您的代码,并检查它是否仍然抛出。迭代器调试可以帮助您捕获这些错误,使其像鸵鸟一样停止运行。
There is a problem with your code. Well, several in fact:
std.find('2')
returns asize_t
, you have a potential cast problem if the value of thesize_t
returned (likestd::string::npos
) is superior to what anint
can hold (you would end up with a negative int I think...)position
is negative, or equal tostd::string::npos
then the rangeitFirst,itSecond
is ill-defined (either becauseitSecond
is beforeitFirst
or because it is paststr.end()
)Correct your code, and check if it stills throw. Iterator Debugging is here to help you catch these mistakes, disabling it acting like an ostrich.