执行新向量时出现段错误
以下代码在最后一行出现段错误:
HookAct *act = new HookAct;
act->hkAct = HookAct::PRINT;
act->params = new vector<string>;
Valgrind 告诉我:
==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;
};
The following code gets a seg fault on the last line:
HookAct *act = new HookAct;
act->hkAct = HookAct::PRINT;
act->params = new vector<string>;
Valgrind tells me:
==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)
Does anyone have any idea why it is doing this?
FYI, here's the [current, temporary] definition of HookAct:
struct HookAct {
enum {
PRINT
} hkAct;
vector<string> *params;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如Brian所说,错误消息指向
str: :string
用NULL
初始化,这是禁止的。然而,您的代码看起来像是由来自 Java 或 C# 的人编写的,并且习惯于盲目地new
一切。然而,在 C++ 中,自动存储是首选。如果将代码更改为此,
则不再需要手动动态内存管理:
As Brian said, the error message points at
str::string
being initialized withNULL
, which is forbidden. However, your code looks like written by someone who comes from Java or C# and is used to mindlesslynew
everything. In C++, however, automatic storage is preferred.If you change your code to this
no manual dynamic memory managing is necessary anymore:
你粘贴的代码没问题。
我认为您的问题可能是将
NULL
字符串添加到向量act->params
中。The code you pasted is fine.
I think your problem is probably adding a
NULL
string to the vectoract->params
.