我試圖計算字典中的詞的開始字母的頻率,其中包含cca 140 000字。我保存的數組中的頻率計數,計數[0]爲字母a,計數[1]爲字母b ...但是,當我總結數組計數時,該值不等於詞典中的單詞總數。我發現如果我將字典大小縮小到95137,數字就相等,但只要字典超過95137個字,count [0]到count [4]的值突然變得非常大。我不知道爲什麼..這裏是我的代碼:數組 - 價值跳
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
FILE *fp = fopen("testdic.txt", "r");
int count[26];
char buffer[30];
for (int i = 0; i < 26; i++)
count[i] = 0;
int total = 0;
while (1)
{
fscanf(fp, "%s", buffer);
if (feof(fp))
break;
count[buffer[0]-97] ++;
total++;
if (count[0] > total) // I used this to find out where the jump occurs
break;
}
printf("%d ", i);
for (int i = 0; i < 26; i++)
printf("%d " , count[i]);
}
堆棧粉碎? 96k整數是400-800kB。如果你有1MB的堆棧,那麼... – 2013-02-16 14:47:57
在嘗試將它用作數組索引之前,在'buffer [0] -97'上放置一些邊界檢查。 – 2013-02-16 15:09:59