malloc 16mb时出现分段错误

时间:2012-12-12 03:41:53

标签: c arrays

当使用malloc分配16777216(16mb)并尝试迭代该数组n次时,我遇到了分段错误。我似乎无法找到为什么会出现分段错误..

register *a;
a = malloc(16777216);
int times, i, dummy, timeTaken;
for (times = 0; times < 10000; times++) {
    for (i = 0; i < 16777216; i += 1) {
        dummy = a[i];
    }
}

1 个答案:

答案 0 :(得分:2)

应该是:

 register int *a;
 a = malloc(16777216*sizeof(int));