問題:函數進入就當,第一行打印也沒有出來
stack size限制是在系統中(ulimit -s可以查看)
若程式裡超過的大小,可以compile,但在run time時會產生問題
不過一般不會定義超過1Mbyte,若超過1Mbyte最好還是用malloc
更新記錄
item | note |
---|---|
20170518 | 第一版 |
目錄
Stack Size Limit
問題
設定由1080P換到4K
函數內的定義char array大小超過8M(stack size問題)
stack及heap區塊不同
- 函數裡面是變數是由stack區塊產生
- malloc是由heap區塊產生
函數裡面的變數是進入函數時才去跟stack區塊要
若這時候要超過stack size就會有問題
來源:stack
stack size 大小
- 由limit取得,單位為Kbyte
可以系統最大stack size為8Mbyte1
2[root@localhost nvr]# ulimit -s
8192
其它
ulimit -u: 顯示可開啟的thread上限
ulimit -u 2048:設定可開啟的thread上限為2048
ulimit -s: 顯示最大stack size Kbyte
ulimit -s 8192: 設定最大stack size 8Mbyte
- ulimit -a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17[root@localhost nvr]# ulimit -a
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 15243
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1024
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) 15243
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
問題:函數進入就當,第一行打印也沒有出來
原本1080P(即1920x1080)正常
要了(1920x1080x2x2)=7.9Mbyte的stack size1
2
3
4
5
6
7
8
9
10
11
12int
yuv420_to_grb(ldvr_t * ldvr, int width2, int height2)
{
printf("%s<line:%d> ==============<<<============ TT20-1 \r\n",
__FUNCTION__, __LINE__);
int res, j;
gui_t *gui = ldvr->gui;
bitmap_file_header_t bf;
bitmap_info_header_t bi;
char vloss_bmp_buf[MAX_IMAGE_SIZE * 2];改為4K(即3840x2160)異常
要了(3840x2160x2x2)=31.6Mbyte的stack size,超過8M大小
改為使用預先malloc的方式,即正常
ldvr->core->vloss_bmp_buf = (unsigned char *) malloc(MAX_IMAGE_SIZE x 2);
- define
1
2
3
4
5
6
7
8#ifdef RESOLUTION_4K
#define MAX_IMAGE_WIDTH 3840
#define MAX_IMAGE_HEIGHT 2160
#else
#define MAX_IMAGE_WIDTH 1920
#define MAX_IMAGE_HEIGHT 1080
#endif
#define MAX_IMAGE_SIZE (MAX_IMAGE_WIDTH * MAX_IMAGE_HEIGHT * 2)
區域變數生成
compile之後產生組語如下
使用stack區塊,來生成區域變數
在進入函數頂端立即實現區域變數
若要超過大小,就會產生問題,在第一行打印也看不到
register | note |
---|---|
%rax | temporary register |
%rbx | callee-saved register |
%rsp | stack pointer |
%rbp | callee-saved register |
%rsi | used to pass 2nd argument to functions |
%rdi | used to pass 1st argument to functions |
- CFA
- Typically, the CFA is defined to be the value of the stack pointer at the call site in the previous frame
- .cfi_def_cfa_offset directive, and you can see that the CFA is now at an offset of 16 bytes from the current stack pointer.
- gas-explanation-of-cfi-def-cfa-offset
what-is-the-purpose-of-the-rbp-register-in-x86-64-assembler
pushq %rbp ; Save address of previous stack frame movq %rsp, %rbp ; Address of current stack frame subq $16, %rsp ; Reserve 16 bytes for local variables
- 解决assembly - GAS: Explanation of .cfi_def_cfa_offset
- push a 64-bit (8 byte) return address onto the stack
第一個8byte(return address) - %rsp to reserve another 8 bytes of stack for itself
第二個8byte(reserved space)
- push a 64-bit (8 byte) return address onto the stack