以下代码在最后一行获取段错误:

   HookAct *act = new HookAct;
    act->hkAct = HookAct::PRINT;
    act->params = new vector<string>;


瓦尔格朗德告诉我:

==15551== Process terminating with default action of signal 11 (SIGSEGV)
==15551== Access not within mapped region at address 0x0
==15551== at 0x5927026: std::string::assign(char const*, unsigned long) (in /usr/lib/libstdc++.so.6.0.10)
==15551== by 0x725424A: test (test.cpp:10)


有谁知道为什么要这么做?

仅供参考,这是HookAct的[当前,临时]定义:

struct HookAct {
    enum {
        PRINT
    } hkAct;
    vector<string> *params;
};

最佳答案

作为Brian said,错误消息指向str::string,并且已使用NULL对其进行了初始化,这是禁止的。但是,您的代码看起来像是来自Java或C#的人编写的,习惯于无意识地new一切。但是,在C ++中,首选自动存储。

如果您将代码更改为此

struct HookAct {
    enum {
        PRINT
    } hkAct;
    vector<string> params;
    HookAct() : hAct(HookAct::PRINT), params() {}
};


不再需要手动进行动态内存管理:

HookAct hookAct;

09-06 18:39