我正在做一个小项目,在其他事情中解析一些文件。当我用valgrind检查我的项目时,我得到了这个错误:
Syscall param open(filename) points to unaddressable byte(s)
对于我的理解(和阅读),这意味着我发送了一个没有定义的内存,空的或者被删除了,但是我不知道为什么……
这是engine.cpp。它的构造函数从控制台接收"char** argv“变量
//alot of includes and using namespace std.
Engine::Engine(char** args) {
processConfFile(args[1]);
}
void Engine::processConfFile ( char* fileName) {
string* fileContent = fileToString(fileName); //this line is specified at the stacktrace
stringstream contentstream(*fileContent);
// parsing and stuff
delete fileContent;
}
string* Engine::fileToString(const char* fileName) const{
string* content = new string();
ifstream ifs (fileName); // this line produces the error
if (ifs) {
content->assign((istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>()));
ifs.close();
}
else {
//TODO logger error.
}
return content;
}你知道是什么导致了这个问题吗?提前进行thx检查。
附言:代码运行得很好。文件被正确读取和解析。
发布于 2013-11-24 04:56:06
我的第一个猜测是在构造Engine时没有很好地定义/分配args1。你期望在**args中有什么?你如何调用你的构造函数?我猜类似于main中的Engine(argv),但如果你从来没有检查过argc是否大于1,你将传递一个带有未初始化内存的数组,当你最后尝试使用它时,它会卡住。
https://stackoverflow.com/questions/20166477
复制相似问题