You need to enable JavaScript to run this app. Mac在线编译器运行C代码出现Segmentation fault (core dumped)错误
优惠活动
大模型
产品
解决方案
定价
更多

Mac在线编译器运行C代码出现Segmentation fault (core dumped)错误

C语言猜数字游戏段错误排查与修复

什么是段错误(Segmentation fault)

段错误是操作系统的内存保护机制触发的报错:当程序试图读写自身无权访问的内存地址时,系统会强制终止程序运行,打印core dumped提示。这类问题和你的Mac设备、在线编译器无关,完全是代码内存访问违规导致的。

代码中触发段错误的核心原因

  • 函数内声明的4个指针变量p1guessPtr、p2guessPtr、p1scorePtr、p2scorePtr属于未初始化的野指针:声明后没有指向任何合法的可访问内存地址,存储的是栈上的随机垃圾值。
  • 代码在指针声明后立刻执行了*p1scorePtr = 0; *p2scorePtr = 0;操作,本质是往随机指向的未知内存位置写入数据,这是最典型的野指针违规操作,程序刚执行到这行就会触发段错误。
  • 后续给p1guessPtr、p2guessPtr赋值合法地址的逻辑根本没有执行机会——野指针写操作已经让程序崩溃退出了。你之前调整指针逻辑后故障更严重,本质是让野指针指向了受系统严格保护的内存区域,触发错误的概率和报错强度更高。

代码中存在的其他逻辑问题(修复段错误后仍会影响游戏运行)

  • 输入校验逻辑存在运算符优先级错误:原写法(p1guess && p2guess > 0)实际等效于(p1guess != 0 && p2guess > 0),完全达不到“校验两个玩家都输入正整数”的目的。
  • 循环终止条件存在运算符优先级错误:原写法while(*p1guessPtr || *p2guessPtr != luckyNum)实际逻辑是“只要P1的猜测值非0,或者P2没猜中就一直循环”,哪怕有玩家提前猜中,游戏也不会停止。
  • 距离计算未取绝对值:原代码直接用luckyNum - 猜测值算距离,如果猜测值比幸运数字大,计算结果会是负数,导致远近判断完全颠倒。
  • 主函数中直接打印了生成的幸运数字,完全失去猜数字游戏的意义。

修复后的可运行代码

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

void checkGuess(int luckyNum){
    int p2prox, p1prox;
    int p1guess,p2guess,p2score,p1score;
    // 直接用普通整型存储分数,无需多余指针操作
    p1score = 0;
    p2score = 0;
    int loopFlag = 1;

    do{
        // 输入校验环节:直到两个玩家都输入合法正整数才继续
        do{
            printf("Enter Player 1's Guess (1-1000):\n");
            scanf("%d",&p1guess);
            printf("Enter Player 2's Guess (1-1000):\n");
            scanf("%d",&p2guess);
            if(p1guess <= 0 || p2guess <= 0){
                printf("Wrong input try again, please enter a positive number\n");
            }
        }while(p1guess <= 0 || p2guess <= 0);
    
        /* 计算猜测值与幸运数字的绝对距离 */
        p1prox = abs(luckyNum - p1guess);
        p2prox = abs(luckyNum - p2guess);

        /* 计分逻辑 */
        if(p1prox == p2prox){
            p1score++;
            p2score++;
            printf("\nSame Guess! Both receive +1 points!\n");
        }
        else if(p1prox < p2prox){
            p1score++;
            printf("\nPlayer 1's guess proximity to lucky number: %d\n",p1prox);
            printf("Player 2's guess proximity to lucky number: %d\n",p2prox);
            printf("Player 1 gets a point! Current score -> P1: %d, P2: %d\n",p1score, p2score);
        }
        else{
            p2score++;
            printf("Player 1's guess proximity to lucky number: %d\n",p1prox);
            printf("Player 2's guess proximity to lucky number: %d\n",p2prox);
            printf("Player 2 gets a point! Current score -> P1: %d, P2: %d\n",p1score, p2score);
        }

        // 只要有玩家猜中幸运数字,就退出游戏主循环
        if(p1prox == 0 || p2prox == 0){
            loopFlag = 0;
        }
        
    }while(loopFlag);

    printf("\nFinal proximity result -> P1: %d, P2: %d\n",p1prox, p2prox);
    if(p1prox == 0){
        printf("Player 1 guessed the lucky number correctly! Final score: %d\n",p1score);
    }
    else{
        printf("Player 2 guessed the lucky number correctly! Final score: %d\n",p2score);
    }
}

int main(){
    int luckyNum;
    srand(time(0));
    luckyNum=(rand() % 1000) + 1;
    printf("A lucky number between 1-1000 has been generated!\n");
    checkGuess(luckyNum);
    return 0;
}

内容的提问来源于stack exchange,提问作者Desneck

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.25 16:45:01