同一C程序在Code::Blocks与gcc编译后执行异常问题求助
C程序编译执行异常:Code::Blocks运行正常,gcc编译后无输出直接退出
问题现象
- 使用Code::Blocks(MinGW环境)编译目标代码,生成54KB可执行文件,运行完全符合预期:输入内容后按Ctrl+D/Ctrl+C可正常输出单词长度的垂直直方图;
- 使用gcc在cmd或Emacs中编译同一代码,编译成功(生成41KB文件),但运行时仅部分执行就快速退出,无任何输出;即使将gcc编译的exe放到Code::Blocks中运行,结果依旧如此。
该程序是《C程序设计语言》1.13题的实现,功能为统计输入单词长度并输出垂直直方图。
排查与问题根源
多次删除可执行文件重新编译,结果一致。曾怀疑是getchar()函数的问题,最终确认未初始化largestN变量是导致异常的核心原因:
- Code::Blocks默认Debug编译模式下,未初始化的局部变量可能被自动初始化为0,因此
largestN能正常参与后续的最大值遍历逻辑; - gcc默认编译为Release模式,未初始化的局部变量会包含随机垃圾值。在遍历
wordLen数组寻找最大值时,if (wordLen[ii] >= largestN)的判断逻辑会因largestN的随机值失效,甚至可能导致后续int level[largestN];定义的数组出现越界、内存非法访问等问题,最终触发程序崩溃退出,无任何输出。
问题代码分析
问题代码中largestN仅声明未初始化,导致逻辑异常:
//Declaration of variables for Histogram. int currentLvl, largestN, ii, iii, IV, V; //Finds and stores the largest number from the array into var:largestN. for (ii = 0; ii < MAXLENGTH; ii++) { if (wordLen[ii] >= largestN) largestN = wordLen[ii]; }
修复方案
初始化largestN为0,确保最大值遍历逻辑从合理的初始值开始:
//Declaration of variables for Histogram. int currentLvl, largestN = 0, ii, iii, IV, V; //Finds and stores the largest number from the array into var:largestN. for (ii = 0; ii < MAXLENGTH; ii++) { if (wordLen[ii] >= largestN) largestN = wordLen[ii]; }
完整修复后的代码:
#include <stdio.h> #define MAXLENGTH 25 /* Maximum length of input. */ int main(){ /* Histogram of length of words in its input. Vertical or Horizontal. (Vertical is hardest, so let's do that!). * Identify words and count characters within each, store that length in a array that represents the length for each word. * Draw based on the number of words, their lengths, and their maximum height (optional). */ /* Identify when inside of a word, start counting. When leaving word, record c-count into array, reset count to 0 after. * Until entering new word. */ //Declaration of the Counter's variables here //The Histogram's variables are farther down below! int nw, c, cCount, wordLen[MAXLENGTH]; int i = nw = cCount = 0; //Initialize wordLen into 0 to avoid garbage results from any potential unused members of the array. for (i = 0; i < 25; i++) { wordLen[i] = 0; } //Counts characters in each word and stores inside of chronological array. while ((c = getchar())!= EOF){ if (c == ' ' || c == '\t' || c == '\n'){ wordLen[nw] = cCount; ++nw; cCount = 0; } else { ++cCount; } } //Prints first 4 words lengths for comparison purposes. printf("%d %d %d %d\n\n", wordLen[0], wordLen[1], wordLen[2], wordLen[3]); //Declaration of variables for Histogram. int currentLvl, largestN = 0, ii, iii, IV, V; //Finds and stores the largest number from the array into var:largestN. for (ii = 0; ii < MAXLENGTH; ii++) { if (wordLen[ii] >= largestN) largestN = wordLen[ii]; } //Declare and initialize array for comparison purposes for later use in the Histogram generation process. int level[largestN]; level[0] = 0; for (iii = 1; iii < largestN + 1; iii++){ level[iii] = iii; } // printf("\n largest N is %d\n", largestN); //for testing // printf("wordLen 0 is: %d\n", wordLen[0]); //for testing /* Histogram generation: * The logic is simple: From Top-left, move rightwards (in chronological order) and check which of the words' lengths * reach the current level or height (starting from Top). Print a line at that spot if they ever do, print an empty space if * they do not. * Then move one step down, and repeat the process but comparing 1 level lower. Until bottom is reached. */ for (V = largestN; V > 0; V--){ currentLvl = level[V]; for (IV = 0; IV < MAXLENGTH; IV++){ if (wordLen[IV] >= currentLvl) printf("|"); else { printf(" "); } printf(" "); } printf("\n"); } return 0; }
内容的提问来源于stack exchange,提问作者nyrx
相关产品推荐
相关产品推荐

