首页 > 解决方案 > 向量构造的访问冲突

问题描述

#include <vector>

std::vector<int> v[1000];

int main()
{
    return 0;
}

我刚刚将此代码与 Visual Studio 2019 一起使用。

    Project1.exe!__CheckForDebuggerJustMyCode() Unknown
>   Project1.exe!std::exchange<std::_Container_proxy *,std::nullptr_t>(std::_Container_proxy * & _Val, void * && _New_val) Line 599 C++
    Project1.exe!std::vector<int,std::allocator<int>>::~vector<int,std::allocator<int>>() Line 676  C++
    Project1.exe!`eh vector destructor iterator'(void * ptr, unsigned __int64 size, unsigned __int64 count, void(*)(void *) destructor) C++
    Project1.exe!`dynamic atexit destructor for 'v''()  C++
    ucrtbased.dll!00007ffbdf8e48d7()    Unknown
    ucrtbased.dll!00007ffbdf8e42e5()    Unknown
    ucrtbased.dll!00007ffbdf8e441a()    Unknown
    ucrtbased.dll!00007ffbdf8e4a81()    Unknown
    ucrtbased.dll!00007ffbdf8e3c51()    Unknown
    ucrtbased.dll!00007ffbdf8e3afd()    Unknown
    ucrtbased.dll!00007ffbdf8e3b6a()    Unknown
    ucrtbased.dll!00007ffbdf8e3de4()    Unknown
    ucrtbased.dll!00007ffbdf8e4176()    Unknown
    Project1.exe!__scrt_common_main_seh() Line 297  C++
    Project1.exe!__scrt_common_main() Line 331  C++
    Project1.exe!mainCRTStartup() Line 17   C++
    kernel32.dll!00007ffc168c7034() Unknown
    ntdll.dll!00007ffc1829cec1()    Unknown

0x00007FF6087210D2 上的访问冲突。这是一个调用堆栈

为什么这段代码会崩溃?我尝试安装另一个版本的 Visual Studio (2017),但它仍然崩溃。我也尝试过重新安装 Window SDK,但它不起作用。

可能的原因是什么,如何解决?


第二个代码示例:

#include <stdio.h>
#include <vector>

int main()
{
    printf("before\n");
    std::vector<std::vector<int>> v(3000);
    printf("after\n");
    return 0;
}

每次运行的异常都会发生变化。上述代码的调用堆栈之一是:

    ConsoleApplication2.exe!std::vector<int,class std::allocator<int> >::_Getal(void)   Unknown
    ConsoleApplication2.exe!std::vector<int,std::allocator<int>>::vector<int,std::allocator<int>>() Line 446    C++
    ConsoleApplication2.exe!std::_Default_allocator_traits<std::allocator<std::vector<int,std::allocator<int>>>>::construct<std::vector<int,std::allocator<int>>>(std::allocator<std::vector<int,std::allocator<int>>> & __formal, std::vector<int,std::allocator<int>> * const _Ptr) Line 695  C++
    ConsoleApplication2.exe!std::_Uninitialized_backout_al<std::allocator<std::vector<int,std::allocator<int>>>>::_Emplace_back<>() Line 1509   C++
    ConsoleApplication2.exe!std::_Uninitialized_value_construct_n<std::allocator<std::vector<int,std::allocator<int>>>>(std::vector<int,std::allocator<int>> * _First, unsigned __int64 _Count, std::allocator<std::vector<int,std::allocator<int>>> & _Al) Line 1835   C++
    ConsoleApplication2.exe!std::vector<std::vector<int,std::allocator<int>>,std::allocator<std::vector<int,std::allocator<int>>>>::_Ufill(std::vector<int,std::allocator<int>> * _Dest, const unsigned __int64 _Count, std::_Value_init_tag __formal) Line 1584    C++
    ConsoleApplication2.exe!std::vector<std::vector<int,std::allocator<int>>,std::allocator<std::vector<int,std::allocator<int>>>>::_Construct_n_copies_of_ty<std::_Value_init_tag>(const unsigned __int64 _Count, const std::_Value_init_tag & _Val) Line 462  C++
    ConsoleApplication2.exe!std::vector<std::vector<int,std::allocator<int>>,std::allocator<std::vector<int,std::allocator<int>>>>::vector<std::vector<int,std::allocator<int>>,std::allocator<std::vector<int,std::allocator<int>>>>(const unsigned __int64 _Count, const std::allocator<std::vector<int,std::allocator<int>>> & _Al) Line 473 C++
>   ConsoleApplication2.exe!main() Line 8   C++

+

此代码也崩溃

#include <vector>

int main()
{
    auto a = new std::vector<int>[3000];
    delete[] a;
    return 0;
}
#include <string>

int main()
{
    auto a = new std::string[3000];
    delete[] a;
    return 0;
}

所以我怀疑内存不足,但是下面的代码运行良好。

#include <vector>

int main()
{
    auto a = new int[10000000];
    delete[] a;
    return 0;
}

我尝试在 Visual Studio 安装程序中卸载所有 Visual Studio 和 Window Kit,然后重新安装它们,但是它仍然会发生。

标签: c++stlwindowconsole-applicationvisual-studio-2019

解决方案


我在 Visual Studio 2019 中测试了您的程序,因为这就是您正在使用的。

#include <vector>

std::vector<int> v[1000];

int main()
{
    return 0;
}

它工作得很好。但是你已经知道了。我在 64 位模式下使用它。(你也做过)

因为当您使用“std::”时它崩溃了,所以我检查了使用 std 和不使用 std 时加载的 DLL。您应该检查 DLL 的差异。没有 STD: 在此处输入图像描述

与性病:

在此处输入图像描述

我不确定这是否真的是软件问题。(因为您提到您使用 Visual Studio 2017 进行了测试,但它仍然崩溃)在这种情况下,我认为这是您的 RAM 的问题。也许您可以在另一台计算机上测试您的代码。


推荐阅读