C中的sizeof(数组):分段错误
发布时间:2020-12-16 10:41:14 所属栏目:百科 来源:网络整理
导读:参见英文答案 Getting a stack overflow exception when declaring a large array????????????????????????????????????9个 嗨,我从这段代码得到一个奇怪的分段错误: int main(void){ int array1[10000000]; int n = sizeof(array1); printf("%d n",n ); r
参见英文答案 >
Getting a stack overflow exception when declaring a large array????????????????????????????????????9个
嗨,我从这段代码得到一个奇怪的分段错误: int main(void){ int array1[10000000]; int n = sizeof(array1); printf("%d n",n ); return 0; } 但是,如果我改变 int array1[10000000]; 至 int array1[1000000]; ( one less zero) 该程序工作和打印4000000 我在Fedora 21(64位)上运行它 这是因为C中的数组有最大大小吗?先感谢您 解决方法int array1[10000000]; 对于你的堆栈来说太大了,而你的堆栈溢出了 int array1[1000000]; 很大,但是当阵列适合它时,不会溢出堆栈. 请注意,堆栈的大小可能因系统而异,可以设置为特定大小. 解决方法: >使数组静态. int *array1; array1 = malloc(10000000 * sizeof(int)); if(array1 == NULL) /* If `malloc` failed to allocate memory */ { fputs("Oops! `malloc` failed to allocate memory!n",stderr); exit(-1); /* Exit the program with a return value of `-1` ; Requires `stdlib.h` */ } /* Use the array and after use,free it using */ free(array1); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |