You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

switch-case分支退出后数组丢失的C语言问题求助

问题原因

你定义的cpiArray和instructionCountArray是case 1代码块内的局部变量,当执行完case 1的break后,这个代码块的作用域结束,局部变量会被系统回收销毁,所以回到主菜单后,这些数组的内存已经不存在了,其他case自然访问不到数据。

解决方法

要让数组在整个main函数的循环中都能被访问,需要把数组的声明移到main函数的作用域内(也就是switch循环外面),但因为数组大小是用户输入的numOfClass,没法提前确定固定大小,所以需要用动态内存分配(malloc函数)来创建数组,这样内存会一直保留到你主动释放它。

修改后的完整代码

#include <stdio.h>
#include <stdlib.h> // 包含malloc和free的头文件

int main(int argc, const char * argv[]) {
    // 变量声明
    int numOfClass = 0, machineMhz = 0, quitFunction = 0, menuSelection;
    int *cpiArray = NULL; // 用指针存储动态数组地址,初始为NULL
    int *instructionCountArray = NULL;

    // 菜单循环
    while (quitFunction == 0) {
        printf("Performance Assessment:\n-----------------------\n");
        printf("1) Enter parameters\n");
        printf("2) Print table of parameters\n");
        printf("3) Print table of performance\n");
        printf("4) Quit\n\n\n");
        printf("Enter Selection: ");
        scanf("%d", &menuSelection);
        
        switch (menuSelection) {
            case 1:{
                printf("\nYou've selected \"Enter parameters\"\n");
                
                // 若之前分配过内存,先释放避免泄漏
                if (cpiArray != NULL) {
                    free(cpiArray);
                    free(instructionCountArray);
                    cpiArray = NULL;
                    instructionCountArray = NULL;
                }

                // 获取指令类别数量
                printf("\nEnter the number of instruction classes: ");
                scanf("%d", &numOfClass);
                
                // 动态分配数组内存
                cpiArray = (int*)malloc(numOfClass * sizeof(int));
                instructionCountArray = (int*)malloc(numOfClass * sizeof(int));
                
                // 检查内存分配是否成功
                if (cpiArray == NULL || instructionCountArray == NULL) {
                    printf("内存分配失败!\n");
                    numOfClass = 0;
                    break;
                }

                // 获取机器频率
                printf("\nEnter the frequency of the machine: ");
                scanf("%d", &machineMhz);
                
                // 循环获取CPI和指令计数
                for (int i = 0; i < numOfClass; i++) {
                    printf("Enter CPI of Class %d: ", i+1);
                    scanf("%d", &cpiArray[i]);
                    printf("Enter instruction count of Class %d (millions): ", i+1);
                    scanf("%d", &instructionCountArray[i]);
                    printf("\n");
                }
                printf("\n\n");
                break;
            }
            case 2: {
                // 先判断是否已输入参数
                if (numOfClass == 0 || cpiArray == NULL) {
                    printf("\n请先输入参数!\n\n");
                    break;
                }

                printf("\nYou've selected \"Print table of parameters\"\n");
                printf("----------------------\n");
                printf("| Class | CPI | Count |\n");
                // 遍历数组打印数据
                for (int i = 0; i < numOfClass; i++) {
                    printf("| %5d | %3d | %4d  |\n", i+1, cpiArray[i], instructionCountArray[i]);
                }
                printf("----------------------\n\n");
                break;
            }
            case 3:{
                printf("\nYou've selected \"Print table of performance\"\n");
                printf("function not built...sending you back to main menu...\n\n");
                break;
            }
            case 4:{
                printf("\nYou've selected \"Quit\"\n");
                printf("Goodbye!\n\n");
                // 退出前释放动态内存
                if (cpiArray != NULL) {
                    free(cpiArray);
                    free(instructionCountArray);
                }
                quitFunction = 1;
                break;
            }
            default:
                printf("\n无效选项,请重新输入!\n\n");
        }
    }
    
    return 0;
}

关键修改点说明

  1. 动态内存分配:用malloc在堆上分配内存,不会随代码块结束而销毁,直到调用free主动释放。
  2. 内存泄漏预防:重新输入参数时先释放旧内存,退出程序前也释放内存,避免内存泄漏。
  3. 空指针与参数检查:分配内存后检查是否成功,防止空指针操作;case 2先判断是否已输入参数,避免未分配内存就访问数组。
  4. case 2循环修正:替换原来错误的do-while逻辑,用for循环遍历数组,直接访问存储的参数数据。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 11:05:42