我有以下代碼,它適用於N = 10和C = 25罰款,但如果我使用N = 50和C = 25500炸彈與分段錯誤不知道爲什麼我得到一個分段錯誤初始化我的陣列
#include <stdio.h>
#include <stdlib.h>
// create table
int *table;
table = (int *) malloc(sizeof(int) * C+1 * N+1);
if(table == NULL){
printf("[ERROR] : Fail to allocate memory.\n");
}
// initialize table
for(i =0; i < N-1; i++){
for(j = 0; j < C-1; j++){
//table[i*(C+1)+j]=0;
*(table + (i*(C+1)+j))=1;
}
}
printf("table made\n");
所以此工程沒有'主()'? – devnull
確保malloc內部的數學運算正確無誤(可能是(C + 1)*(N + 1)) –
您在哪裏得到了分段錯誤錯誤?把printf語句放在for循環中,並嘗試找出在哪個位置出現了分段錯誤。因爲如果malloc沒有爲大數分配任何內存,我們可以在'if(table == NULL)'條件中找到它。請試試這個 – arunb2w