[英]Segmentation fault on printing pointer value
The following program crashes when I try to print the value of v
. 当我尝试打印
v
的值时,以下程序崩溃。 I'm trying to understand why. 我想知道为什么。 Any suggestions?
有什么建议么?
#include <stdio.h>
int main() {
int v[5000000];
printf("\n\nv = %p", v);
return 0;
}
EDIT : the program does not segfault if instead of allocating 5000000 elements I allocate 500000 or less. 编辑 :如果不分配5000000个元素而不分配500000或更少,程序不会出现段错误。
EDIT(2) : increasing the stack size solved the problem. 编辑(2) :增加堆栈大小解决了问题。 On Linux, I increase the stack size after reading the answer of stephane-rouberol (using
ulimit -s <some_large_number>
). 在Linux上,我在阅读了stephane-rouberol的答案后增加了堆栈大小(使用
ulimit -s <some_large_number>
)。
Congrats, you have stack overflow :) 恭喜,你有堆栈溢出 :)
Find a way to increase the size of the stack or just allocate the array dynamically: 找到一种方法来增加堆栈的大小或只是动态分配数组:
int* v = malloc( 5000000 * sizeof *v);
/* do something */
free( v );
Stack overflow ! 堆栈溢出 ! See
ulimit -s
if you use bash
or limit stacksize
if [t]csh
如果你使用
bash
或者如果[t]csh
limit stacksize
请参阅ulimit -s
Or instead of using stack, you can use the heap with malloc
或者不使用堆栈,您可以将堆与
malloc
You already have the cause. 你已经有了原因。
5000000
is too big to handle for the program stack where v
will be allocated. 对于将分配
v
的程序堆栈, 5000000
太大而无法处理。 You should allocate it dynamically with malloc
. 你应该用
malloc
动态分配它。
The stack size of a program is dependent on compiler switches and defaults are different from OS to OS. 程序的堆栈大小取决于编译器开关,默认值不同于OS到OS。 In your case it sounds as if the stack is too small to accomodate that large number.
在你的情况下,它听起来好像堆栈太小,无法容纳那么大的数字。 See your compiler(linker) switches to increase stack size.
请参阅编译器(链接器)开关以增加堆栈大小。
尝试
printf("\n\nv = %p", (void *)v);
As others have said, stack overflow. 正如其他人所说,堆栈溢出。 To understand why and when the code actually crashes, this is what goes on between the lines:
要了解代码实际崩溃的原因和时间,这就是行之间的事情:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.