- 查看自己系统中栈在虚拟地址空间的大致位置:
#include<stdio.h>
int main()
{
int i;
printf("The stack top is near %p\n", &i);
return 0;
}
引用于《c专家编程》
在我的ubuntu14.04 64位操作系统中输出
The stack top is near 0x7ffc5643981c
符合64位虚拟内存管理
2. linux栈的大小
首先用ulimit -s可以查看栈大小
输出:8192。单位是K。也就是8M
1 #include <stdio.h>
2 int main()
3 {
4 int size = 2000000, i;
5 int buf[size];
6 for(i = 0; i < size; ++i)
7 buf[i] = i;
8 return 0;
9 }
当size为2百万,int占4个字节,总共大小为8百万。程序没问题
当size为210万,程序输出Segmentation fault(core dumped)
3. 查看可以分配多大的内存
#include<stdio.h>
#include<stdlib.h>
int main()
{
int MB = 0;
while(malloc(1 << 20)) ++MB;
printf("Allocated %d MB totaln", MB);
return 0;
}
引用于《C专家编程》
在32位的windows10上最大分配了1904MB。大约2G。