分段错误很奇怪

发布于 2025-01-03 15:07:16 字数 1886 浏览 1 评论 0原文

问题:当我尝试编译文件并运行时,它存在分段问题。当我将文件传递给我的朋友(他正在使用相同版本的ubuntu)时,服务器将能够运行。我想知道为什么?

下面是我整个页面的代码。 我个人觉得没有什么问题,不过我就贴出来供大家参考。

void readNStoreData ()
{
char words[MAX];
char *wholeLine;
char* delimiter = ",";
int cflag = 0;
int x, count = 0;
char input;

FILE *countryFile;
countryFile = fopen("Countries.txt","r");

if (!countryFile) {
exit(EXIT_FAILURE);
}

while (fgets (words, MAX - 1, countryFile) != NULL)
{
    //atof to convert string to double
    //split a single line into individual tokens indicating , as the delimeter
    //afterwards store them into array
    wholeLine = strtok (words, delimiter);
    strcpy (records [count].TDL, wholeLine);
    wholeLine = strtok (NULL, ",");
    strcpy (records [count].cName, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].FIPS104, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].ISO2, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].ISO3, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    records [count].ISO = atof(wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].cCapital, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].cRegion, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].cCurrencyName, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].cCurrencyCode, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    records [count].cPopulation = atof(wholeLine);
count++;
}
fclose(countryFile); //close file
}

我希望有人能够在某个地方发现错误。 感谢提前提供帮助的人!

运行gdb,错误实际上是这一行。 它位于这个函数中。

    l(gdb) frame 1
    l#1  0x08048936 in readNStoreData () at testserver.c:61
    61                      strcpy (records [count].cName, wholeLine);

Problem: When i tried to compile my file and run, it has segmentation problem. When I passed the file to my friend (he is using the same version of ubuntu), the server will be able to run. I am wondering why?

Below will be my code for the whole page.
Personally I feel that there is no problem with it but I will just paste it for reference in case anyone asks for it.

void readNStoreData ()
{
char words[MAX];
char *wholeLine;
char* delimiter = ",";
int cflag = 0;
int x, count = 0;
char input;

FILE *countryFile;
countryFile = fopen("Countries.txt","r");

if (!countryFile) {
exit(EXIT_FAILURE);
}

while (fgets (words, MAX - 1, countryFile) != NULL)
{
    //atof to convert string to double
    //split a single line into individual tokens indicating , as the delimeter
    //afterwards store them into array
    wholeLine = strtok (words, delimiter);
    strcpy (records [count].TDL, wholeLine);
    wholeLine = strtok (NULL, ",");
    strcpy (records [count].cName, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].FIPS104, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].ISO2, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].ISO3, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    records [count].ISO = atof(wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].cCapital, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].cRegion, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].cCurrencyName, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    strcpy (records [count].cCurrencyCode, wholeLine);
    wholeLine = strtok (NULL, delimiter);
    records [count].cPopulation = atof(wholeLine);
count++;
}
fclose(countryFile); //close file
}

I hope someone will be able to spot the mistake somewhere.
Thanks to advance to those who helped!

run the gdb and the error is actually this line.
it is situated in this function.

    l(gdb) frame 1
    l#1  0x08048936 in readNStoreData () at testserver.c:61
    61                      strcpy (records [count].cName, wholeLine);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦回梦里 2025-01-10 15:07:16

强烈建议您学习使用调试器,例如< a href="http://www.gnu.org/software/gdb/" rel="nofollow">GDB。您可以通过 sudo apt-get install gdb 在 ubuntu 上安装它,

这是一个简短的 教程

Google 发现许多更多示例

编辑

由于您现在已经运行了 GDB,请尝试设置 断点之前 run:

(gdb) br testserver.c:61

在你执行run之后你应该能够打印各种变量,看看哪一个是非法的。

I strongly suggest you to learn to use a debugger, such as GDB. You can install it on ubuntu by sudo apt-get install gdb

Here is a short tutorial.

Google finds many more examples

EDIT:

Since you now have GDB running, try setting a breakpoint before run:

(gdb) br testserver.c:61

and after you do run you should be able to print the various variables and see which one is illegal.

终难遇 2025-01-10 15:07:16

strcpy 的调用失败。您向其中传递了错误的参数(请参阅 http://www.opengroup.org/sud /sud1/xsh/strcpy.htm 用于文档)。

如果您希望我们对此进行分析,您需要向我们展示

  • 如何定义 records
  • 元素结构类型的定义、
  • 如何初始化 records 数组、元素数量、
  • 如何限制 count 不超出 records 数组的容量,
  • 如何确保 records[count].cName (大概是 char* 或 < code>char[]) 足够大以包含由 strtok 解析的令牌

    <块引用>

    注意最大可能的标记是 MAX 个字符,因为它可能是 fgets 返回的最大行长度+ 1 个尾随 NUL 字符,前提是输入中没有单个 delimiter 字符。

This should get you underway.

我偷偷怀疑您可能忘记了一起初始化接收数组(记录),但同样,除非您显示更多代码,否则我们无法知道。

Your call to strcpy fails. You pass bad parameters into it (see http://www.opengroup.org/sud/sud1/xsh/strcpy.htm for documentation).

If you want us to analyze this, you need to show us

  • how records is defined,
  • what the element struct type is defined like,
  • how you initialize the records array, to how many elements,
  • how you limit count to not go beyond the capacity of the records array
  • how you make sure that records[count].cName (presumably either char* or char[]) is large enough to contain the token parsed by strtok

    Note The largest possible token is MAX characters, since it could be the MAX linelength returned by fgets + 1 trailing NUL character iff there isn't a single delimiter character in the input.

This should get you underway.

I have a sneaking suspicion that you may have forgotten to initialize the receiving array (records) alltogether, but again, we can't know unless you show more code.

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