简体   繁体   English

0xC0000005:C ++中的访问冲突

[英]0xC0000005: access violation in C++

Here is my code: 这是我的代码:

class Base
{
public:
    virtual void show() const = 0;
};

class Child : public Base
{
private:
    static const int i = 1;
public:
    virtual void show() const
    {
        cout << i;
    }
};

map<int, const Base &> myMap{
    { 0, Child() },
    { 1, Child() },
};

Base & b = Child();

int main()
{
    b.show();

    myMap.at(0).show(); // This provokes the error

    system("pause>NUL");
    return 0;
}

As you see, I'm trying to use a global (or static ) data, which will call some virtual functions. 如您所见,我正在尝试使用global (或static )数据,这将调用一些virtual函数。 When I test Base & b = Child(); 当我测试Base & b = Child(); and in main : b.show(); mainb.show(); , everything goes well. , 一切顺利。

But, if I use map like above, I will get an error: 但是,如果我使用上面的map ,我将收到一个错误:

0xC0000005: Access violatoin reading location 0x00000000 . 0xC0000005: Access violatoin reading location 0x00000000

I've tried to debug this code and I found that when it arrived myMap.at(0).show(); 我试图调试这个代码,我发现当它到达myMap.at(0).show(); , I got this: , 我懂了:
在此输入图像描述

It seems that the table of virtual functions is Unable to read ... 似乎虚函数表Unable to read ...

Then I tried to use the pointer: 然后我尝试使用指针:
map<int, Base *> and {0, new Child()} . map<int, Base *>{0, new Child()}
This works. 这有效。

So it seems that this error comes from temporary reference. 所以似乎这个错误来自临时引用。

But I don't know why b works. 但我不知道为什么b有效。 b is a temporary reference too. b也是临时参考。
In my opinion, this map contains many b . 在我看来,这张map包含很多b
Why does b works whereas map doesn't work? 为什么b工作而map不起作用?

You have a map of references to temporaries. 你有一个临时工具的地图。 I'm surprised that even compiled 即使编译,我也很惊讶

map<int, Base &> myMap{
    { 0, Child() },
    { 1, Child() },
};

Drop the reference and switch to unique_ptr 删除引用并切换到unique_ptr

std::map<int, std::unique_ptr<Base>> myMap;

You can insert elements into this map using this method . 您可以使用此方法将元素插入此贴图中。 This will prevent object slicing . 这将阻止对象切片

You do the same thing again here 你在这里再做同样的事情

Base & b = Child();

You cannot hold non-const references to temporary objects 您不能保存对临时对象的非const引用

MSVC does allow to affect a temporary to a reference, meaning that Base & b = Child(); MSVC允许影响临时引用,这意味着Base & b = Child(); is accepted and correctly processed, even it is not standard C++ and is rejected by other compilers (and could be rejected by a later version of MSVC) 被接受并正确处理,即使它不是标准的C ++并被其他编译器拒绝(并且可能被更高版本的MSVC拒绝)

But even MSVC does not accept stl containers of references. 但即使是MSVC也不接受stl容器的引用。

So you should either use a map<int, Base> (no ref, store a copy of the object), or a map<int, Base *> (stores a pointer to the object). 所以你应该使用map<int, Base> (没有ref,存储对象的副本),或者map<int, Base *> (存储指向对象的指针)。 In the latter case, you must deal explicitely with the destruction of the object. 在后一种情况下, 必须明确地处理对象的破坏。 You can also use smart pointers , unique_ptr or shared_ptr to let the stl cares for automatic destruction. 您还可以使用智能指针unique_ptrshared_ptr让stl关心自动销毁。

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

相关问题 访问冲突读取位置0xC0000005 C ++ - Access violation reading location 0xC0000005 C++ 从C#调用非托管C ++库(dll)会产生访问冲突错误(0xc0000005) - Calling unmanaged C++ library (dll) from C# creates an access violation error (0xc0000005) C ++错误:0xC0000005:访问冲突写入位置0xfeeefeee - C++ ERROR: 0xC0000005: Access violation writing location 0xfeeefeee 复制构造函数问题 C++:“0xC0000005:访问冲突写入位置 0x00000000。” - Copy Constructor Issue C++: "0xC0000005: Access violation writing location 0x00000000." 错误0xC0000005:从函数C ++返回时发生访问冲突 - Error 0xC0000005: Access Violation when returning from a function C++ C ++:0xC0000005:访问冲突写入位置0x00000000 - C++: 0xC0000005: Access violation writing location 0x00000000 C++ SFML 数组错误:访问冲突读取位置 0xC0000005 - C++ SFML Array Error: Access violation reading location 0xC0000005 0xC0000005:访问冲突读取位置 0x00000000。 C++ vs2015 - 0xC0000005: Access violation reading location 0x00000000. c++ vs2015 C++ QT 异常代码 0xc0000005 读取访问冲突在:0x0,标志=0x0 - C++ QT Exception code 0xc0000005 read access violation at: 0x0, flags=0x0 如何修复 C++ 中的“0xC0000005:访问冲突读取位置” - How to fix “0xC0000005: Access violation reading location” in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM