我运行程序时出现“分段错误(核心转储)”

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了我运行程序时出现“分段错误(核心转储)”相关的知识,希望对你有一定的参考价值。

(抱歉我的英文不好btw)好吧,我以前复制了这段代码的解决方案。我的代码与解决方案相同(代码没问题,做得很好),但是当我运行程序时,它会显示一条消息,上面写着Segmentation fault (core dumped)。我不知道如何向你展示我的程序的捕获,但我的代码似乎没问题。当我运行程序时,它会在询问Good的音量时完成。然后这条消息:Segmentation fault (core dumped)出现

#include <stdio.h>

#define NUM 5
#define MAX_WAGON_CAPACITY 0.85
#define MAX_WAGON_CAPACITY_ANIMALS 0.5
#define LIMIT1 500
#define LIMIT2 2500
#define FRAGILE 1.10
#define DANGEROUS 1.15
#define FIRST_PRICE 0.50
#define SECOND_PRICE 0.45
#define THIRD_PRICE 0.40

typedef enum { FOOD, CHEMICAL, ANIMALS, VEHICLES,
               ELECTRONICS, CONSTRUCTION, OTHERS } tGoodType;
typedef enum { FALSE, TRUE } boolean;

int main(int argc, char **argv) {
    int idGood;
    float volumeGood;
    tGoodType typeOfGood;
    boolean isFragile;
    boolean isDangerous;
    float train [NUM];
    int nWagons;
    float volumeTrain;
    float price;  
    float surchargeFragile;
    float surchargeDangerous;

    printf("Good identifier: 
");
    scanf("%d", &idGood);
    printf("
Insert volume of Good
");
    scanf("%f", volumeGood);
    printf("
Insert Good type (0-FOOD, 1-CHEMICAL, 2-ANIMALS, 3-VEHICLES, 4-ELECTRONICS, 5-CONSTRUCTION, 6-OTHERS)
");
    scanf("%u", &typeOfGood);
    printf("
Is the Good fragile? (0-FALSE, 1-TRUE)
");
    scanf("%u", &isFragile);
    printf("
Is the Good dangerous) (0-FALSE, 1-TRUE
");
    scanf("%u", &isDangerous);
    printf("
The maximum length of the train is>> ");
    scanf("%f", train[0]);
    printf("
The length of the locomotive is>> ");
    scanf("%f", train[1]);
    printf("
The length of each wagon is>> ");
    scanf("%f", train[2]);
    printf("
The space between each wagon is>> ");
    scanf("%f", train[3]);
    printf("
The volume of a wagon is>> ");
    scanf("%f", train[4]);

    nWagons = (int)((train[1] - train[2]) / (train[3] + train[4]));

    if (typeOfGood == 2)
        volumeTrain = nWagons * train[4] * MAX_WAGON_CAPACITY_ANIMALS;
    else
        volumeTrain = nWagons * train[4] * MAX_WAGON_CAPACITY;

    price = 0.0;
    surchargeFragile = 0.0;
    surchargeDangerous = 0.0;
    if (volumeTrain >= volumeGood) {
        if (volumeGood > 0 && volumeGood < LIMIT1) { 
            price = volumeGood * FIRST_PRICE;
        } else if (volumeGood >= LIMIT1 && volumeGood <= LIMIT2) {
            price = volumeGood * SECOND_PRICE;
        } else {
            price = volumeGood * THIRD_PRICE;
        }
    }
    if (isFragile == 1) {
        surchargeFragile = (price * FRAGILE) - price;
    }
    if (isDangerous == 1) {
        surchargeDangerous = (price * DANGEROUS) - price;
        price = price + surchargeFragile + surchargeDangerous;
    }   

    if (price > 0.0) {
        printf("The Good id is %d", &idGood);
        printf("The number of wagons is %d", &nWagons);
        printf("The price for the good is %f", &price);
    } else {
        printf("The good does not fit the train");
    }

    return 0;
}
答案

你的scanf中有几个错误,产生你的分段错误,它们由编译器指示:

c.c:38:1: warning: ‘volumeGood’ is used uninitialized in this function [-Wuninitialized]
scanf("%f", volumeGood);
^~~~~~~~~~~~~~~~~~~~~~~

c.c:38:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", volumeGood);

因为volumeGood的未定义值被用作scanf将尝试写入的地址

你可能想要的

scanf("%f", &volumeGood);

以及所有这些:

c.c:46:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[0]);
        ^
c.c:48:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[1]);
        ^
c.c:50:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[2]);
       ^
c.c:52:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[3]);
        ^
c.c:54:9: warning: format ‘%f’ expects argument of type ‘float *’, but argument 2 has type ‘double’ [-Wformat=]
scanf("%f", train[4]);

因为列车中的条目使用就像它们包含地址一样,你想要的

scanf("%f", &train[0]);
scanf("%f", &train[1]);
scanf("%f", &train[2]);
scanf("%f", &train[3]);
scanf("%f", &train[4]);

当你使用scanf和等价物时,你必须给出保存值的地址


也在

c.c:84:29: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("The Good id is %d", &idGood);
                             ^
c.c:85:38: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int *’ [-Wformat=]
     printf("The number of wagons is %d", &nWagons);
                                      ^
c.c:86:40: warning: format ‘%f’ expects argument of type ‘double’, but argument 2 has type ‘float *’ [-Wformat=]
     printf("The price for the good is %f", &price);

那个时候反过来,你必须给出地址,而你必须给出地址

     printf("The Good id is %d", idGood);
     printf("The number of wagons is %d", nWagons);
     printf("The price for the good is %f", price);

以上是关于我运行程序时出现“分段错误(核心转储)”的主要内容,如果未能解决你的问题,请参考以下文章

在Linux机器上运行C代码时出现分段错误(核心转储)[关闭]

当我在 ubundu12.04 中运行“phantomjs”命令时出现分段错误(核心转储)

为啥在编译我的代码C(linux)时出现分段错误(核心转储)[关闭]

为 OpenCV 的 C++ createTrackbar 运行 C 包装器时出现编译分段错误(核心转储)

运行 MDART 协议时出现 NS 2.35 分段错误(核心转储)

创建多个矩阵时出现分段错误(核心转储)