简体   繁体   English

ITERATOR列表在std :: string构造函数中被破坏

[英]ITERATOR LIST CORRUPTED in std::string constructor

The code below compiled in Debug configuration in VS2005 SP1 shows two messages with “ITERATOR LIST CORRUPTED” notice. 在VS2005 SP1的Debug配置中编译的以下代码显示了两条带有“ITERATOR LIST CORRUPTED”通知的消息。

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? 这是编译器或标准库中的错误吗?

My bad! 我的错! Edit: Yeah problem with compiler. 编辑:是的编译问题。 See this -- particularly the Community Content section. 看到这一点 - 特别是社区内容部分。

What @dirkgently said in his edit. @dirkgently在他的编辑中说了什么。

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. 显然, std::string一些代码位于运行时dll中,特别是宏定义对构造函数没有生效,迭代器调试的代码被执行。 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. 我认为这是一个错误,虽然可能不是在Visual Studio本身,而是在文档中。

There is a problem with your code. 您的代码存在问题。 Well, several in fact: 嗯,实际上有几个:

  1. std.find('2') returns a size_t , you have a potential cast problem if the value of the size_t returned (like std::string::npos ) is superior to what an int can hold (you would end up with a negative int I think...) std.find('2')返回一个size_t ,你有一个潜在的偏色问题,如果值size_t返回(像std::string::npos )优于什么的int能装(你会最终有一个负面的我觉得...)
  2. if position is negative, or equal to std::string::npos then the range itFirst,itSecond is ill-defined (either because itSecond is before itFirst or because it is past str.end() ) 如果position是负数,或等于std::string::npos那么范围itFirst,itSecond是错误定义的(因为itSeconditFirst之前或因为它超过了str.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. Iterator Debugging用于帮助您捕获这些错误,禁用它像鸵鸟一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM