std::vector push_back 导致访问冲突 [英] std::vector push_back results in access Violation

查看:45
本文介绍了std::vector push_back 导致访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

故事:我正在制作一个共享库,将 C 代码作为包装器暴露给在 VS 2008 中编译的 C++ 静态库,因此我与该编译器相关联.在我的库中,我正在尝试编写一个非常简单的字典来通过共享库边界传递数据.我不会传递实际的字典,只会传递处理程序并提供访问其成员的函数.字典只是键值对的 std::vector.

Story: I'm making a shared library exposing C code as a wrapper to a C++ static library compiled in VS 2008, so I'm tied to that compiler. In my library I'm trying to do a very simple dictionary to pass data through the shared library boundaries. I'll not pass the actual dictionary, just the handler an provide functions to access its members. The dictionary is simply a std::vector of key-value pairs.

问题: 每次我尝试将键值对推回字典时,我的程序都会崩溃.这是一个独立的演示代码(一个新的 Win32 控制台项目的 main.cpp):

Problem: My program crashes every time I try to push_back a key-value pair into the dictionary. Here is a self-contained demo code (main.cpp of a fresh Win32 console project):

#include "stdafx.h"

#include <cstdlib>
#include <vector>

typedef enum KeyType
{
    KeyType_CHAR = 0,
    KeyType_INT,
    KeyType_CHAR_PTR_AS_STRING
};

typedef enum ValueType
{
    ValueType_CHAR = 0,
    ValueType_INT,
    ValueType_CHAR_PTR_AS_STRING
};

struct DictNode
{
    ValueType value_type;
    void* value_unsafe_ptr;
    void* key_unsafe_ptr;
};

struct Dict
{
    KeyType key_type;
    std::vector<DictNode> nodes;
};

int _tmain(int argc, _TCHAR* argv[])
{
    /* Create Dict */
    Dict* test = (Dict*)malloc(sizeof(Dict));
    test->key_type = KeyType_INT;

    /* Add (0, "Zero") */
    int* key0 = (int*)malloc(sizeof(int));
    *key0 = 0;

    char* content0 = (char*)malloc(sizeof(char)*5);
    content0[0] = 'Z';
    content0[1] = 'e';
    content0[2] = 'r';
    content0[3] = 'o';
    content0[4] = '';

    DictNode node0;
    node0.value_type = ValueType_CHAR;
    node0.key_unsafe_ptr = key0;
    node0.value_unsafe_ptr = content0;

    test->nodes.push_back(node0); // BOOM

    /* Release memory */
    test->nodes.clear();
    free(key0);
    free(content0);
    free(test);

    return 0;
}

在行 test->nodes.push_back(node0); 我得到 0xC0000005:访问冲突读取位置 0xcdcdcdc1.通过在该行设置断点,我可以看到所有 keycontent0、node0 和 test 都已定义并具有 正确的值.

At line test->nodes.push_back(node0); I get 0xC0000005: Access violation reading location 0xcdcdcdc1. By setting a breakpoint at that line, I can see that all key, content0, node0 and test are defined and have the correct values.

推荐答案

Dict* test = (Dict*)malloc(sizeof(Dict)); 没有按照你想的那样做.

Dict* test = (Dict*)malloc(sizeof(Dict)); does not do what you think it does.

malloc 分配一块内存,但不对其进行初始化.所以稍后,当您调用 test->nodes.push_back 时,您正在调用未初始化的内存.未定义的行为.在你的情况下,它崩溃了.

malloc allocates a block of memory, but does not initialize it. So later on, when you call test->nodes.push_back, you're calling into uninitialized memory. Undefined behavior. In your case, it crashes.

这里的解决方案是用new分配test,这将初始化testtest->nodes.

The solution here is allocate test with new, which will initialize both test and test->nodes.

Dict* test = new Dict;

更好的解决方案是询问为什么 test 是动态分配的.

The even better solution is to ask why test is dynamically allocated to begin with.

这篇关于std::vector push_back 导致访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆