Visual Studio const_iterator 分配错误

Visual Studio const_iterator Assignment Error

在 Visual Studio 2010 上分配默认构造的 vector<int*>::const_iterator 错误。我已经在 5 个 Visual Studio 2010 系统上尝试过,这些系统都有 Service Pack 1。它在 3 上失败/5 台机器,我已经能够确定导致这 3 个系统出现故障的原因,但我似乎找不到错误报告。

代码如下:

#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;
}

很明显,除了 cout << "Hello World" << endl; 之外的所有内容都在 Release 中进行了优化,因此这个最小的示例只会在 Debug 中失败。但是在调试中它给出的错误是:

Unhandled exception at 0x01071e14 in test.exe: 0xC0000005: Access violation reading location 0x00000000.

比较链接的工作和非工作 MSVCP100D.dlls 表明存在细微差异,工作 .dll 的产品版本:10.0.40219.325,非工作 .dll 的产品版本: 10.0.40219.1.

实际错误发生在

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

再次比较工作版本和非工作版本显示工作版本已发生更改。非工作代码简单地说:

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

工作代码说:

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();
    }

综上所述,这是我的实际问题。我如何获得此更新?我已经使用Windows Update 更新到最新,但是问题还是没有解决。是否有一些隐藏的补丁需要我去某个地方获取?我在任何地方都找不到这个问题的记录,所以我也找不到关于补丁的信息。

此代码具有未定义的行为。 [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. —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. [ 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 是单数形式,其使用不属于段落中列出的任何例外情况。

但是,考虑到您 post 的代码片段,我怀疑即使您对 uninitialized 进行值初始化,您的代码也不会工作,这是 Microsoft 实现中的一个错误,他们固定在 later hotfix.