简体   繁体   English

没有为指针分配内存,这是否会导致C中的分段错误(内核转储)?

[英]Not allocating memory for pointers, is this causing my segmentation fault (core dumped) in C?

I am able to run it on my windows computer in eclipse, but when I try running it on a Unix computer I get "Segmentation fault (core dumped)". 我可以在Eclipse的Windows计算机上运行它,但是当我尝试在Unix计算机上运行它时,出现“分段错误(核心已转储)”。 How can I find which line is causing the error? 如何找到导致错误的行? I read this page http://www.cs.mun.ca/~michael/c/problems.html which says it could be due to not allocating memory for pointers, but I am getting errors when I try for example char *users[1000]; 我阅读了此页面http://www.cs.mun.ca/~michael/c/problems.html ,其中说这可能是由于未为指针分配内存,但是在尝试使用char *users[1000];时出现错误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,  "");

    }
}

Sometimes it can be hard to start developing for a new platform, but once you understand the basic concepts it is just as easy as any other platform. 有时可能很难开始为新平台进行开发,但是一旦您了解了基本概念,它就和其他平台一样容易。 :) :)

I would recommend you to do the following: 我建议您执行以下操作:

  1. Compile your code with debugging symbols. 使用调试符号编译代码。 This will enable you to debug kind of similar to the way you would do it in VS. 这将使您能够调试类似于在VS中进行调试的方式。 Using the gcc compiler (and some others) you should use the -g argument to do it. 使用gcc编译器(和其他一些编译器)时,应使用-g参数。
  2. Configure your terminal to generate coredumps. 配置您的终端以生成核心转储。 A coredump is an exact copy of the process state at the time where it crashed. 核心转储是崩溃时进程状态的精确副本。 Enabling this depends on the terminal you are using (bash, csh, etc) but you can try the following: 'ulimit', 'limits', 'limit' 启用它取决于您所使用的终端(bash,csh等),但是您可以尝试以下操作:'ulimit','limits','limit'
  3. Run gdb. 运行gdb。 If you used the -g argument to compile, and you have the coredump, you can do 'gdb -c COREDUMP_FILENAME BinaryFilename' It will take you exactly to the line that caused the crash 2.a Alternately you can run your program from gdb 'gdb BinaryFilename' and step every line until your program crashes. 如果使用-g参数进行编译,并且具有coredump,则可以执行'gdb -c COREDUMP_FILENAME BinaryFilename'。它将带您完全到达导致崩溃的行2.a另外,您也可以从gdb运行程序' gdb BinaryFilename”并逐行执行,直到程序崩溃。

您正在滥用NULL指针,并且在修复代码之前,请先阅读有关指针(特别是NULL指针)的更多信息,然后选中此按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM