c - 没有为指针分配内存,这会导致我在 C 中出现段错误(核心转储)吗?

标签 c debugging unix segmentation-fault

我能够在 eclipse 中的 Windows 计算机上运行它,但是当我尝试在 Unix 计算机上运行它时,出现“段错误(核心已转储)”。如何找到导致错误的行?我读了这个页面 http://www.cs.mun.ca/~michael/c/problems.html,它说这可能是由于没有为指针分配内存,但是当我尝试例如 char *users[1000];

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAX_CHANGE (10.0/86400.0)       /* 10kg/day */
    /* seconds in a day is 24 hours * 60 minutes * 60 seconds */

main() {
    char line[1024];
    char lineC[1024];

    int countToken = 0;
    int lasttime = 0;
    char *tokens;
    char *users;
    int timestamp;
    int duration;
    char userID[1000];
    char lastuser[1000];
    float weight;
    float lastweight;
    float change;
    float changePerTime;

    while (fgets(line,1024,stdin) != NULL) {
        strcpy(lineC, line);
        tokens = strtok(line, " ");
        sscanf(tokens, "%d", &timestamp);   //first token is timestamp
        while(tokens != NULL){
            countToken++;
            tokens = strtok(NULL, " ");
        }

        int countTemp = countToken;
        users = strtok(lineC, " ");
        while(countTemp > 1){
            if(countTemp == countToken){
                countTemp--;
            }
            else{
                users = strtok(NULL, " .0123456789");
                strcat(userID, users);
                countTemp--;
            }
        }
        users = strtok(NULL, " ");
        sscanf(users, "%f", &weight);

        if (countToken < 1 || timestamp == 0) {
            printf("Invalid time\n");
            continue;
            }
        else if (countToken < 2 || ! (isalpha(userID[0]) || userID[0] == '_') )
            printf("Illegal userID\n");
        else if (countToken < 3 || weight < 30.0 || weight > 300.0)
            printf("Illegal weight\n");
        else if (lasttime >= timestamp)
                printf("Nonmonotonic timestamp\n");
        else {
            duration = timestamp - lasttime;
            change = weight - lastweight;
            changePerTime = change / duration;
            int g = strcmp(lastuser, userID);
            if (lasttime > 0 && (changePerTime < -MAX_CHANGE || changePerTime > MAX_CHANGE) && (g==0))
                printf("Suspiciously large weight change\n");
            else
                printf("OK\n");

            lastweight = weight;
            lasttime = timestamp;
            }
        strcpy(lastuser, userID);
        lasttime = timestamp;
        countToken = 0;
        strcpy(userID,  "");

    }
}

最佳答案

有时开始针对新平台进行开发可能很困难,但一旦您了解了基本概念,它就会像任何其他平台一样容易。 :)

我建议您执行以下操作:

  1. 使用调试符号编译您的代码。这将使您能够进行类似于在 VS 中进行调试的方式。使用 gcc 编译器(和其他一些编译器),您应该使用 -g 参数来执行此操作。
  2. 配置您的终端以生成核心转储。核心转储是进程崩溃时进程状态的精确副本。启用此功能取决于您使用的终端(bash、csh 等),但您可以尝试以下操作:'ulimit'、'limits'、'limit'
  3. 运行 gdb。如果你使用 -g 参数编译,并且你有 coredump,你可以执行 'gdb -c COREDUMP_FILENAME BinaryFilename' 它会把你带到导致崩溃的行 2.a 或者,您可以从 gdb 'gdb BinaryFilename' 运行您的程序并逐步执行每一行,直到您的程序崩溃。

关于c - 没有为指针分配内存,这会导致我在 C 中出现段错误(核心转储)吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21399005/

相关文章:

c# - 小型转储文件对分析 .NET WinForms 应用程序崩溃有用吗?

c - 通过GDB加载模块

python - 使用 UNIX cSh 运行 python 程序时出错

c - 为什么在 if-else 中使用运算符 OR(逻辑运算符),为什么不在 if-else 中使用 AND(逻辑运算符)

c - 灵活数组成员的非静态初始化?

visual-studio-2010 - 如何处理 Visual Studio 2010 无响应?

linux - 如何在 CentOs(或任何 LINUX/UNIX 系统)上构建/安装 protobuf 2.4.1?

c - 十进制到 IEEE 754 使用 C 的单精度 IEEE 754 代码

c - 动态查找结构的大小

bash - UNIX:在 $PWD 以上的目录中查找文件