简体   繁体   English

Visual Studio const_iterator 赋值错误

[英]Visual Studio const_iterator Assignment Error

The assignment of a default constructed vector<int*>::const_iterator errors on Visual Studio 2010. I've tried this on 5 Visual Studio 2010 systems that all have Service Pack 1. It fails on 3/5 machines, I've been able to identify what is causing the failure on the 3 systems but I cannot seem to find a bug report. Visual Studio 2010 上默认构造的vector<int*>::const_iterator错误的分配。我已经在 5 个都具有 Service Pack 1 的 Visual Studio 2010 系统上尝试过这个。它在 3/5 台机器上失败,我已经能够确定导致 3 个系统故障的原因,但我似乎找不到错误报告。

Here is the code:这是代码:

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector<int*> vec;
    int arr[3] = {};

    for(int i = 0; i < sizeof(arr) / sizeof(arr[0]); ++i) vec.push_back(arr + i);

    vector<int*>::const_iterator initialized = vec.cbegin();
    vector<int*>::const_iterator uninitialized;

    initialized = uninitialized;

    cout << "Hello World" << endl;

    return 0;
}

Clearly everything but the cout << "Hello World" << endl;显然除了cout << "Hello World" << endl; is optimized out in Release so this minimal example will only fail in Debug.在 Release 中优化了,所以这个最小的例子只会在 Debug 中失败。 But in Debug the error it gives is:但在调试中,它给出的错误是:

Unhandled exception at 0x01071e14 in test.exe: 0xC0000005: Access violation reading location 0x00000000. test.exe 中 0x01071e14 处未处理的异常:0xC0000005:访问冲突读取位置 0x00000000。

Comparing the working and non-working MSVCP100D.dlls that were linked shows that there is a slight discrepancy, the working .dll is Product Version: 10.0.40219.325 and the non-working .dll is Product Version: 10.0.40219.1.比较链接的工作和非工作 MSVCP100D.dll 显示存在轻微差异,工作 .dll 是产品版本:10.0.40219.325,非工作 .dll 是产品版本:10.0.40219.1。

The actual error occurs in实际错误发生在

C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\include\\xutility C:\\Program Files (x86)\\Microsoft Visual Studio 10.0\\VC\\include\\xutility

And again diffing the working and non-working version shows that a change has been made to the working version.再次区分工作版本和非工作版本表明已经对工作版本进行了更改。 The non-working code simply says:非工作代码只是说:

if (_Myproxu != _Right._Myproxy)
    _Adopt(_Right._Myproxy->_Mycont);

The working code says:工作代码说:

if (_Myproxy == _Right._Myproxy)
    ;
else if (_Right._Myproxy != 0)
    _Adopt(_Right._Myproxy->_Mycont);
else
    {   // becoming invalid, disown current parent
    _Lockit _Lock(_LOCK_DEBUG);
    _Orphan_me();
    }

All that to say, here's my actual question.说了这么多,这是我的实际问题。 How do I get this update?我如何获得此更新? I've updated to the latest using Windows Update, but the problem has not been resolved.我已使用 Windows Update 更新到最新版本,但问题仍未解决。 Is there some hidden patch that I need to go somewhere to get?是否有一些隐藏的补丁需要我去某个地方获取? I can't find this issue written up anywhere, so I also can't find information about the patch.我在任何地方都找不到这个问题,所以我也找不到关于补丁的信息。

This code has undefined behavior.此代码具有未定义的行为。 [iterator.requirements.general]/p6: [iterator.requirements.general]/p6:

Iterators can also have singular values that are not associated with any sequence.迭代器也可以有不与任何序列相关联的奇异值。 [ Example : After the declaration of an uninitialized pointer x (as with int* x; ), x must always be assumed to have a singular value of a pointer. [示例:在声明未初始化的指针x (与int* x; ),必须始终假定x具有指针的奇异值。 end example ] Results of most expressions are undefined for singular values; 结束示例] 大多数表达式的结果对于奇异值是未定义的; the only exceptions are destroying an iterator that holds a singular value, the assignment of a non-singular value to an iterator that holds a singular value, and, for iterators that satisfy the DefaultConstructible requirements, using a value-initialized iterator as the source of a copy or move operation.唯一的例外是销毁持有奇异值的迭代器,将非奇异值分配给持有奇异值的迭代器,以及对于满足DefaultConstructible要求的迭代器,使用值初始化的迭代器作为复制或移动操作。 [ Note : This guarantee is not offered for default initialization, although the distinction only matters for types with trivial default constructors such as pointers or aggregates holding pointers. [注意:默认初始化不提供此保证,尽管区别仅对具有普通默认构造函数(例如指针或持有指针的聚合)的类型很重要。 end note ] In these cases the singular value is overwritten the same way as any other value. 尾注] 在这些情况下,奇异值的覆盖方式与任何其他值相同。 Dereferenceable values are always non-singular.可解引用的值总是非奇异的。

uninitialized is singular, and its use doesn't fall within any of the exceptions listed in the paragraph. uninitialized是单数,它的使用不属于该段落中列出的任何例外情况。

However, given the snippets you post, I suspect that your code wouldn't work either even if you value-initialize uninitialized , which is a bug in Microsoft's implementation, and which they fixed in a later hotfix .但是,鉴于您发布的片段,我怀疑即使您对uninitialized值进行了初始化,您的代码也无法工作,这是 Microsoft 实现中的一个错误,并且他们在以后的修补程序中修复了该错误。

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

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