C中的数组索引限制
发布时间:2020-12-16 10:51:59 所属栏目:百科 来源:网络整理
导读:在 Linux上,有16 GB的RAM,为什么会出现以下段错误: #include stdlib.h#define N 44000int main(void) { long width = N*2 - 1; int * c = (int *) calloc(width*N,sizeof(int)); c[N/2] = 1; return 0;} 根据GDB,问题来自c [N / 2] = 1,但是原因是什么? 解
在
Linux上,有16 GB的RAM,为什么会出现以下段错误:
#include <stdlib.h> #define N 44000 int main(void) { long width = N*2 - 1; int * c = (int *) calloc(width*N,sizeof(int)); c[N/2] = 1; return 0; } 根据GDB,问题来自c [N / 2] = 1,但是原因是什么? 解决方法
你正在分配大约14-15 GB的内存,无论出于什么原因,分配器都不能
现在给你那么多 – 因此当你取消引用一个NULL指针时,calloc会返回NULL并且会出现段错误. 检查calloc是否返回NULL. 假设您正在64位Linux下编译64位程序.如果您正在执行其他操作 – 如果系统上的长度不是64位,则可能会将计算溢出到calloc的第一个参数. 例如,试试 #include <stdlib.h> #include <stdio.h> #define N 44000L int main(void) { size_t width = N * 2 - 1; printf("Longs are %lu bytes. About to allocate %lu bytesn",sizeof(long),width * N * sizeof(int)); int *c = calloc(width * N,sizeof(int)); if (c == NULL) { perror("calloc"); return 1; } c[N / 2] = 1; return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |