我不明白为什么地图会导致分段错误
我有一个执行以下操作的循环:
short fooID;
char line[256]
map<short,foo> foos;
set<short> barIDs;
while (fgets(line,256,file) != NULL){
string line_copy = line;
/*use token to split the line into several parameters which does not effect foo * bar*/
string token = strtok(line,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
barID = short(atoi(token));
foo * bar;
bar = new foo;
if(barIDs.find(barID) == barIDs.end()){
barIDs.insert(barID);
bar->setID(barID);
this->foos[barID] = bar;
}
}
当我运行此代码时,当从文件加载所有条时,我会遇到分段错误。 barID 范围为 1-1192。
有什么想法吗?
谢谢
上面的代码只是我实际代码的输入摘要
I have a loop that does the following:
short fooID;
char line[256]
map<short,foo> foos;
set<short> barIDs;
while (fgets(line,256,file) != NULL){
string line_copy = line;
/*use token to split the line into several parameters which does not effect foo * bar*/
string token = strtok(line,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
token = strtok(NULL,",");
barID = short(atoi(token));
foo * bar;
bar = new foo;
if(barIDs.find(barID) == barIDs.end()){
barIDs.insert(barID);
bar->setID(barID);
this->foos[barID] = bar;
}
}
When I run this code, I get a segmentation fault when all the bars are loaded from the file.
the barID range is 1-1192.
Any thoughts?
Thanks
The code above is only a typed summery of my actual code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这将创建一个指向内存中随机位置的变量
bar
。您需要使其指向一个有效的对象:bar = new foo;
- 并记住完成后删除它,迭代您的地图并删除所有foo
是你添加的。This creates a variable
bar
pointing to a random location in memory. You need to make it point to a valid object:bar = new foo;
- and remember to delete it when you're done, iterate over your map and delete all thefoo
's you added.请注意,
char * strtok ( char * str, const char * delimiters )
更改了str
参数的内容(请参阅 strtok 上的 C++ 参考)。也许您应该将line
替换为line_copy
,因为您将其声明为string line_copy = line;
?Please note that
char * strtok ( char * str, const char * delimiters )
changes the contents of thestr
parameter (see C++ Reference on strtok). Maybe you should replaceline
withline_copy
since you're declaring it asstring line_copy = line;
?好吧,我的猜测是你在这里崩溃了:
如果数据文件的最后一行有换行符,那么最后一次读取的
fgets
(在 EOF 上返回 NULL 之前)将返回一个空行。您没有检查这种情况。同样,在将strtok
的结果传递给atoi
之前,您不会检查它。在空行上,结果将为NULL
。所以atoi
将会崩溃。也就是说,您不断地更改细节,因此我无法知道所描述的事件序列是否与您的真实代码有关。这不是获得帮助的好方法。我建议您将来提供准确的代码片段(可以编译是最好的)和有关崩溃的一些详细信息(通常至少不难确定崩溃的位置)。
OK, my guess is that you are crashing here:
If the last line of your data file has a line feed, then the last
fgets
read (before it returns NULL on EOF) will return a blank line. You are not checking for this condition. Similarly, you are not checking the result ofstrtok
before passing it toatoi
. And on the blank line, that result will beNULL
. Soatoi
will crash.That said, you are constantly changing the details so I have no way to know whether the described sequence of events has anything to do with your real code. This is not a great way to get help. I'd suggest you in the future provide an accurate code snippet (compilable is the best) and some details on the crash (normally it is not difficult to determine the location of the crash, at least).
你有一个本地 foos:
但后来你用 this->foos 来存储 bar
另外,地图存储的是 foo,而不是 foo*
You have a local foos:
But later you use this->foos to store bar
Also, the map is storing foo, not foo*