#include <stdio.h> #include <stdlib.h> #include <fcntl.h> #include <unistd.h> int main() { unsigned long long max=3000000000000000000; char buffer[max]; int fd=open("1.txt",O_RDONLY); unsigned long long j=read(fd,buffer,max),i; for(i=0;i<j;i++)printf("%c",buffer[i]); close(fd); return 0; }
调试时gdb的输出
Breakpoint 1, main () at my_cp.c:7
7 unsigned long long max=3000000000000000000;
(gdb) c
Continuing.
Program received signal SIGSEGV, Segmentation fault.
main () at my_cp.c:9
9 int fd=open(“1.txt”,O_RDONLY);
(gdb) c
Continuing.
Program terminated with signal SIGSEGV, Segmentation fault.
The program no longer exists.
(gdb)
3q
解决方案
60
栈上不可能分配那么大内存,在堆上动态分配内存吧
20
栈的大小一般和编译器和操作系统有关,但一般都在1~10M之间