簡體   English   中英

向量構建的訪問沖突

[英]access violation on vector construction

#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 Kits,然后重新安裝它們,但是,它仍然發生。

我在 Visual Studio 2019 中測試了你的程序,因為這就是你正在使用的。

#include <vector>

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

int main()
{
    return 0;
}

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

因為當你使用“std::”時它崩潰了,我檢查了使用 std 和不使用 std 時加載的 DLL。 您應該檢查 DLL 中的差異。 無性病:在此處輸入圖像描述

性病:

在此處輸入圖片說明

我不確定這是否真的是軟件問題。 (因為您提到您使用 Visual Studio 2017 進行了測試,但它仍然崩潰)在這種情況下,我認為這是您的 RAM 的問題。 也許您可以在另一台計算機上測試您的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM