c - 调试时出现错误 "Arithmetic exception"

标签 c debugging runtime-error

接听时this问题 我尝试运行此代码(用于生成给定范围内的随机数);

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

int main()
{

    int max_range, i = 0, rand_num;
    bool digit_seen[max_range + 1]; // VLAs For C99 only 

    srand((unsigned)time(NULL));
    printf("Enter your maximum of range: ");
    scanf("%d", &max_range);

    for (int i = 1; i <= max_range; i++)
        digit_seen[i] = false;  

    for (;;)
    {  
         rand_num = rand() % max_range + 1;
         if(rand_num !=3)
             if(!digit_seen[rand_num])
             {
                printf("%d ", rand_num);
                digit_seen[rand_num] = true;
                i++;
             }
         if( i == (max_range - 1) )
             exit(0);
    }    
    return 0;
}

它在 max_range47 时工作正常,但之后我在调试时遇到此错误

enter image description here

知道为什么我会收到这个吗?

最佳答案

您正在基于 max_range 的未初始化值创建一个数组:

bool digit_seen[max_range + 1]; // VLAs For C99 only 

它可以有任何可以想象的大小。为什么它对某些值有效而对另一些值无效,这是未定义行为的魔力的一部分;任何事情都可能发生。

关于c - 调试时出现错误 "Arithmetic exception",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18881224/

相关文章:

c - 关于 C 中指针的问题?

java - 如何在 Eclipse 中调试 Maven 项目?

java - Eclipse 调试器悬停生成空框,没有变量值

RStudio 错误 : STRING_ELT() can only be applied to a 'character vector' , 不是 'builtin'

swift - 在 Xcode 模拟器 [swift 3.0] 上启动时屏幕为空白

Android 鼠标光标事件/dev/uinput 不在 Y 轴上移动

java - 从 native API 检索来自加速度计的数据是否更准确?

debugging - 在 Mobile 中运行 Hybris 开发环境进行调试

java - Android Studio 错误 : "Manifest merger failed: Apps targeting Android 12"

c - Loadrunner - 在脚本之间共享共同的 Action