大概推断一下自己电脑栈的大小

#include <stdio.h>
#include <stdlib.h>

int recurse(int x) {
    int a[1<<15];  /* *(int)4 * 2^15 =  64 KiB */
    printf("x = %d.  a at %p\n", x, a);
    a[0] = (1<<14)-1;//这里随便的
    a[a[0]] = x-1;
    if (a[a[0]] == 0)
	return -1;
    return recurse(a[a[0]]) - 1;}

int main(int argc, char *argv[]) {
    int x = 100;
    if (argc > 1)
	x = atoi(argv[1]);//把字符串转换成整型数的一个函数
    int v = recurse(x);
    printf("x = %d.  recurse(x) = %d\n", x, v);
    return 0;
}

这是一个递归函数,函数内定义了一个128kib大小的数组(图里那行注释算错了=。=,感谢评论提醒)。在这里,数组是被存储在栈中,每次递归调用recurse函数的时候都会重新再分配一次空间,所以只要看这个函数被递归了多少次,就可以知道自己电脑的栈空间大概有多大了。我在自己的电脑上跑了一下:

luoxinxin@luoxinxin-virtual-machine:~/桌面/c$ ./a.out
x = 100.  a at 0x7ffc8dcbce00
x = 99.  a at 0x7ffc8dc9cdd0
x = 98.  a at 0x7ffc8dc7cda0
x = 97.  a at 0x7ffc8dc5cd70
x = 96.  a at 0x7ffc8dc3cd40
x = 95.  a at 0x7ffc8dc1cd10
x = 94.  a at 0x7ffc8dbfcce0
x = 93.  a at 0x7ffc8dbdccb0
x = 92.  a at 0x7ffc8dbbcc80
x = 91.  a at 0x7ffc8db9cc50
x = 90.  a at 0x7ffc8db7cc20
x = 89.  a at 0x7ffc8db5cbf0
x = 88.  a at 0x7ffc8db3cbc0
x = 87.  a at 0x7ffc8db1cb90
x = 86.  a at 0x7ffc8dafcb60
x = 85.  a at 0x7ffc8dadcb30
x = 84.  a at 0x7ffc8dabcb00
x = 83.  a at 0x7ffc8da9cad0
x = 82.  a at 0x7ffc8da7caa0
x = 81.  a at 0x7ffc8da5ca70
x = 80.  a at 0x7ffc8da3ca40
x = 79.  a at 0x7ffc8da1ca10
x = 78.  a at 0x7ffc8d9fc9e0
x = 77.  a at 0x7ffc8d9dc9b0
x = 76.  a at 0x7ffc8d9bc980
x = 75.  a at 0x7ffc8d99c950
x = 74.  a at 0x7ffc8d97c920
x = 73.  a at 0x7ffc8d95c8f0
x = 72.  a at 0x7ffc8d93c8c0
x = 71.  a at 0x7ffc8d91c890
x = 70.  a at 0x7ffc8d8fc860
x = 69.  a at 0x7ffc8d8dc830
x = 68.  a at 0x7ffc8d8bc800
x = 67.  a at 0x7ffc8d89c7d0
x = 66.  a at 0x7ffc8d87c7a0
x = 65.  a at 0x7ffc8d85c770
x = 64.  a at 0x7ffc8d83c740
x = 63.  a at 0x7ffc8d81c710
x = 62.  a at 0x7ffc8d7fc6e0
x = 61.  a at 0x7ffc8d7dc6b0
x = 60.  a at 0x7ffc8d7bc680
x = 59.  a at 0x7ffc8d79c650
x = 58.  a at 0x7ffc8d77c620
x = 57.  a at 0x7ffc8d75c5f0
x = 56.  a at 0x7ffc8d73c5c0
x = 55.  a at 0x7ffc8d71c590
x = 54.  a at 0x7ffc8d6fc560
x = 53.  a at 0x7ffc8d6dc530
x = 52.  a at 0x7ffc8d6bc500
x = 51.  a at 0x7ffc8d69c4d0
x = 50.  a at 0x7ffc8d67c4a0
x = 49.  a at 0x7ffc8d65c470
x = 48.  a at 0x7ffc8d63c440
x = 47.  a at 0x7ffc8d61c410
x = 46.  a at 0x7ffc8d5fc3e0
x = 45.  a at 0x7ffc8d5dc3b0
x = 44.  a at 0x7ffc8d5bc380
x = 43.  a at 0x7ffc8d59c350
x = 42.  a at 0x7ffc8d57c320
x = 41.  a at 0x7ffc8d55c2f0
x = 40.  a at 0x7ffc8d53c2c0
x = 39.  a at 0x7ffc8d51c290
x = 38.  a at 0x7ffc8d4fc260
段错误 (核心已转储)

到38的时候就停下了,并表示越界。所以,可以推断我们电脑栈大小大约是63×128kib这么大。

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值