溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

學(xué)習(xí)堆棧大小限制

發(fā)布時間:2020-07-20 21:06:16 來源:網(wǎng)絡(luò) 閱讀:668 作者:59090939 欄目:編程語言

堆棧大小在運行程序時起著重要作用

1. 程序中堆棧的最大大小是多少?

在Linux上,可以使用ulimit命令獲得允許的最大堆棧大小。

[root@web11 c]# ulimit -a
core file size????????? (blocks, -c) 0
data seg size?????????? (kbytes, -d) unlimited
scheduling priority???????????? (-e) 0
file size?????????????? (blocks, -f) unlimited
pending signals???????????????? (-i) 3823
max locked memory?????? (kbytes, -l) 64
max memory size???????? (kbytes, -m) unlimited
open files????????????????????? (-n) 1024000
pipe size??????????? (512 bytes, -p) 8
POSIX message queues???? (bytes, -q) 819200
real-time priority????????????? (-r) 0
stack size????????????? (kbytes, -s) 8192
cpu time?????????????? (seconds, -t) unlimited
max user processes????????????? (-u) 3823
virtual memory????????? (kbytes, -v) unlimited
file locks????????????????????? (-x) unlimited

On my Linux running in vm the value is 8MB.

2. 讓我們來看一下堆棧大小對程序的影響

[root@web11 c]# vim test_stack.c
#include <stdio.h>

int test()
{
?? int a[2097151];

? printf("Hello world");
}

int main()
{
?? test();
?? return 0;
}

[root@web11 c]# ./test_stack

Segmentation fault
我們看到報錯信息了,那是因為達(dá)到了系統(tǒng)堆棧的限制,被系統(tǒng)kill了。
程序中我們定義了一個數(shù)組 int a[2097151],由于是int類型,一個int類型占4個字節(jié)。2097151*4=8388604;8388604>8M,達(dá)到系統(tǒng)上限了
,所以會報錯。
[root@web11 c]# ulimit -s unlimited

[root@web11 c]# ulimit -s
unlimited

再次運行程序

[root@web11 c]# ./test_stack??????
Hello world

程序運行成功,沒有報錯信息。




向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI