我不明白为什么地图会导致分段错误

发布于 2024-10-26 05:26:59 字数 742 浏览 2 评论 0原文

我有一个执行以下操作的循环:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

两相知 2024-11-02 05:26:59
foo * bar;

这将创建一个指向内存中随机位置的变量bar。您需要使其指向一个有效的对象:bar = new foo; - 并记住完成后删除它,迭代您的地图并删除所有foo 是你添加的。

foo * bar;

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 the foo's you added.

尘曦 2024-11-02 05:26:59

请注意, 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 the str parameter (see C++ Reference on strtok). Maybe you should replace line with line_copy since you're declaring it as string line_copy = line;?

甜味拾荒者 2024-11-02 05:26:59

好吧,我的猜测是你在这里崩溃了:

barID = short(atoi(token));

如果数据文件的最后一行有换行符,那么最后一次读取的 fgets (在 EOF 上返回 NULL 之前)将返回一个空行。您没有检查这种情况。同样,在将 strtok 的结果传递给 atoi 之前,您不会检查它。在空行上,结果将为NULL。所以atoi将会崩溃。

也就是说,您不断地更改细节,因此我无法知道所描述的事件序列是否与您的真实代码有关。这不是获得帮助的好方法。我建议您将来提供准确的代码片段(可以编译是最好的)和有关崩溃的一些详细信息(通常至少不难确定崩溃的位置)。

OK, my guess is that you are crashing here:

barID = short(atoi(token));

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 of strtok before passing it to atoi. And on the blank line, that result will be NULL. So atoi 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).

偷得浮生 2024-11-02 05:26:59

你有一个本地 foos:

map<short,foo> foos;

但后来你用 this->foos 来存储 bar

this->foos[barID] = bar;

另外,地图存储的是 foo,而不是 foo*

You have a local foos:

map<short,foo> foos;

But later you use this->foos to store bar

this->foos[barID] = bar;

Also, the map is storing foo, not foo*

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文