小编las*_*ns7的帖子

为什么GCC的VLA(可变长度数组)实现中有数字22?

int read_val();
long read_and_process(int n) {
    long vals[n];
    for (int i = 0; i < n; i++)
        vals[i] = read_val();
    return vals[n-1];
}
Run Code Online (Sandbox Code Playgroud)

x86-64 GCC 5.4编译的汇编语言代码为:

read_and_process(int):
        pushq   %rbp
        movslq  %edi, %rax
>>>     leaq    22(,%rax,8), %rax
        movq    %rsp, %rbp
        pushq   %r14
        pushq   %r13
        pushq   %r12
        pushq   %rbx
        andq    $-16, %rax
        leal    -1(%rdi), %r13d
        subq    %rax, %rsp
        testl   %edi, %edi
        movq    %rsp, %r14
        jle     .L3
        leal    -1(%rdi), %eax
        movq    %rsp, %rbx
        leaq    8(%rsp,%rax,8), %r12
        movq    %rax, %r13
.L4:
        call    read_val()
        cltq …
Run Code Online (Sandbox Code Playgroud)

c assembly gcc x86-64 variable-length-array

14
推荐指数
1
解决办法
887
查看次数

标签 统计

assembly ×1

c ×1

gcc ×1

variable-length-array ×1

x86-64 ×1