如何做的sizeof(阵列)工作 [英] How Does sizeof(Array) work
本文介绍了如何做的sizeof(阵列)工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何Ç发现在运行时数组的大小?其中存储了关于数组大小或数组的边界的信息?
How does c find at run time the size of array? where is the information about array size or bounds of array stored ?
推荐答案
的sizeof(阵列)
完全是由C编译器实现的。该方案被链接的时候,看起来像一个的sizeof()
呼叫您已被改造成一个常数。
sizeof(array)
is implemented entirely by the C compiler. By the time the program gets linked, what looks like a sizeof()
call to you has been converted into a constant.
例如:当您编译此C code:
Example: when you compile this C code:
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char** argv) {
int a[33];
printf("%d\n", sizeof(a));
}
你
.file "sz.c"
.section .rodata
.LC0:
.string "%d\n"
.text
.globl main
.type main, @function
main:
leal 4(%esp), %ecx
andl $-16, %esp
pushl -4(%ecx)
pushl %ebp
movl %esp, %ebp
pushl %ecx
subl $164, %esp
movl $132, 4(%esp)
movl $.LC0, (%esp)
call printf
addl $164, %esp
popl %ecx
popl %ebp
leal -4(%ecx), %esp
ret
.size main, .-main
.ident "GCC: (GNU) 4.1.2 (Gentoo 4.1.2 p1.1)"
.section .note.GNU-stack,"",@progbits
的 $ 132
中间是数组的大小,132 = 4 * 33,注意有没有呼叫的sizeof
指令 - 不像的printf
,这是一个真正的函数
The $132
in the middle is the size of the array, 132 = 4 * 33. Notice that there's no call sizeof
instruction - unlike printf
, which is a real function.
这篇关于如何做的sizeof(阵列)工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文