【问题标题】:segmentation fault weird分段错误奇怪
【发布时间】:2012-02-09 13:13:36
【问题描述】:

问题:当我尝试编译我的文件并运行时,它有分段问题。当我将文件传递给我的朋友(他使用相同版本的 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);

【问题讨论】:

  • 您是否尝试使用valgrindgdbstrace 进行调试?你知道错误发生在哪一行吗?我不会为你调试这个,这是可怕的代码,如果你知道sprintf,你为什么要这样使用strcat
  • @cha0site 我已调试,这是错误。

标签: linux ubuntu client segmentation-fault


【解决方案1】:

强烈建议您学习使用debugger,例如GDB。你可以通过sudo apt-get install gdb在ubuntu上安装它

这是一个简短的tutorial

Google finds 更多examples

编辑

既然您现在运行 GDB,请尝试在 run 之前设置一个 breakpoint

(gdb) br testserver.c:61

在您执行run 之后,您应该能够print 各种变量并查看哪个是非法的。

【讨论】:

  • (gdb) frame 1 #1 0x08048936 in readNStoreData () at testserver.c:61 61 strcpy (records [count].cName, wholeLine);
  • @Andres:你发布它是为了什么?这是 您的 答案 :) 那是缓冲区溢出或损坏的 src/dest 指针。
  • @sehe,坦率地说,我真的不知道我能用它做什么。以及我应该做些什么来使它正确。有cmets吗?
  • @Andres:调用 strcpy 失败。只需检查 strcpy 的所有参数。这是手册页:opengroup.org/sud/sud1/xsh/strcpy.htm
【解决方案2】:

您对strcpy 的呼叫失败。您将错误的参数传递给它(有关文档,请参阅http://www.opengroup.org/sud/sud1/xsh/strcpy.htm)。

如果你想让我们分析这个,你需要给我们看

  • records 是如何定义的,
  • 元素结构类型的定义是什么样的,
  • 你如何初始化records数组,有多少个元素,
  • 如何限制 count 不超出 records 数组的容量
  • 如何确保records[count].cName(可能是char*char[])足够大以包含strtok 解析的令牌

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

这应该让你开始。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多