【问题标题】:C segmentation fault (core dumped) functionC 分段错误(核心转储)函数
【发布时间】:2020-05-17 17:49:50
【问题描述】:

我相信这与我使用roll_die 有关。如果我只是在正文中使用 rand()%6+1 而不是 roll_die 它可以工作,但我想使用该功能。另外,有什么方法可以使用更少的行来打印结果以使我的代码更短?

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int roll_die() {
    return 1 + rand() % 6;
}

int main()
{

srand((unsigned) time(0));

int roll_die;
int first_die_roll, second_die_roll, total;
int roll_result[13] = {0};
int count = 36000; 
while(count--) {

     first_die_roll = roll_die;
     second_die_roll = roll_die;
     total = first_die_roll + second_die_roll;

    roll_result[total]++;

   }

   printf("Computed die rolling frequencies for 36,000 rolls:\n");

   printf("Sum = 2; Frequency = %d ; Percentage = %f%c\n", roll_result[2], (float)roll_result[2]/36000 * 100, '%');

   printf("Sum = 3; Frequency = %d ; Percentage = %f%c\n", roll_result[3], (float)roll_result[3]/36000 * 100, '%');

   printf("Sum = 4; Frequency = %d ; Percentage = %f%c\n", roll_result[4], (float)roll_result[4]/36000 * 100, '%');

   printf("Sum = 5; Frequency = %d ; Percentage = %f%c\n", roll_result[5], (float)roll_result[5]/36000 * 100, '%');

   printf("Sum = 6; Frequency = %d ; Percentage = %f%c\n", roll_result[6], (float)roll_result[6]/36000 * 100, '%');

   printf("Sum = 7; Frequency = %d ; Percentage = %f%c\n", roll_result[7], (float)roll_result[7]/36000 * 100, '%');

   printf("Sum = 8; Frequency = %d ; Percentage = %f%c\n", roll_result[8], (float)roll_result[8]/36000 * 100, '%');

   printf("Sum = 9; Frequency = %d ; Percentage = %f%c\n", roll_result[9], (float)roll_result[9]/36000 * 100, '%');

   printf("Sum = 10; Frequency = %d ; Percentage = %f%c\n", roll_result[10], (float)roll_result[10]/36000 * 100, '%');

   printf("Sum = 11; Frequency = %d ; Percentage = %f%c\n", roll_result[11], (float)roll_result[11]/36000 * 100, '%');

   printf("Sum = 12; Frequency = %d ; Percentage = %f%c\n", roll_result[12], (float)roll_result[12]/36000 * 100, '%');

   return 0;

}

【问题讨论】:

  • 欢迎来到 SO!如果要调用函数,请使用roll_die() 而不是roll_dieint roll_die 表示您正在使用未初始化的内存,您可以将其移除。

标签: c


【解决方案1】:

您在main 中的声明int roll_die;roll_die 声明为本地 int 变量,并隐藏(或shadows)同名函数。因此,当您以后有这样的一行时:

    first_die_roll = roll_die; // Copying a local variable

您将 unitialized 局部变量的值分配给 first_die_roll。因此,total 的计算值未定义,对 roll_result[total] 数组元素的引用很可能“超出范围”,从而导致内存访问错误(=“分段错误”)。

修复:一是去掉局部变量的声明;然后,要调用 roll_die 函数,您需要在名称中添加括号,无论您需要它:

//  int roll_die; // Remove this line - it HIDES your "roll_die" function!
//...

    first_die_roll = roll_die(); // Brackets make this a function call.
    second_die_roll = roll_die();

请随时要求进一步澄清和/或解释。

【讨论】:

    【解决方案2】:

    您已经声明了一个局部变量:int roll_die,它掩盖了 函数 roll_die 的定义。

    此外,您可以仅通过提供函数名称来调用函数[或任何函数]。当你只是指定函数时,这是函数本身的地址不是函数的调用/调用。

    因此,而不是:

    first_die_roll = roll_die;
    

    你想要:

    first_die_roll = roll_die();
    

    而且,您可以使用for 循环进行打印。此外,您可以使用 %% 的格式添加文字 %

    无论如何,这是您的代码已修复并带有错误注释:

    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    #define ROLLMAX     13
    
    int
    roll_die(void)
    {
        return 1 + rand() % 6;
    }
    
    int
    main()
    {
    
        srand((unsigned) time(0));
    
    // NOTE/BUG: this shadows/masks the actual function
    #if 0
        int roll_die;
    #endif
    
        int first_die_roll, second_die_roll, total;
        int roll_result[ROLLMAX] = { 0 };
        int count = 36000;
    
        while (count--) {
    // NOTE/BUG: this does _not_ call roll_die -- it merely sets the values to the
    // address of the roll_die function
    #if 0
            first_die_roll = roll_die;
            second_die_roll = roll_die;
    #else
            first_die_roll = roll_die();
            second_die_roll = roll_die();
    #endif
            total = first_die_roll + second_die_roll;
    
            roll_result[total]++;
        }
    
        printf("Computed die rolling frequencies for 36,000 rolls:\n");
    
        for (total = 2;  total < ROLLMAX;  ++total) {
            int result = roll_result[total];
            printf("Sum = %d; Frequency = %d ; Percentage = %f%%\n",
                total, result, (float) result / 36000 * 100);
        }
    
        return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2020-12-08
      • 2022-01-14
      • 2017-02-25
      • 2016-07-12
      • 2018-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多