【发布时间】:2011-06-28 12:47:40
【问题描述】:
非常不言自明。这是导致“新向量”行出现 SIGABRT 的方法:
vector<string> * Task::arguments() {
vector<string> *args = new vector<string>(); // CAUSES SIGABRT
int count = sizeof(_arguments);
for (int x = 0; x < count; x++) {
string argument(_arguments[x]);
args->push_back(argument);
}
return args;
}
请注意,我在其他地方调用该行没有任何问题。以下是 Task 类中包含的列表:
#include <vector>
#include <unistd.h>
#include <string>
using namespace std;
#include <sys/types.h>
#include <sys/wait.h>
#include <signal.h>
有什么想法吗?
【问题讨论】:
-
为什么要动态分配
std::vector?您几乎肯定应该使用局部变量并按值返回它。 -
如果你真的必须使用
using namespace std;。把它放在 所有包含指令之后。 -
主要问题是 sbi 在他最初的评论中的语气表明他并没有足够关心在他假设事情之前真正了解发生了什么。认真对待这个答案是我的错,但这并不能原谅他张贴一个傲慢的断章取义的答案。然而问题已经解决了,我愿意原谅和忘记;我只敦促人们不要做出假设或断章取义的答案,尤其是对于那些“声誉很高”的人——人们会认真对待你的答案,所以不要用基于错误假设的回应来引导人们进行疯狂追逐。
标签: c++ new-operator stdvector sigabrt