- 论坛徽章:
- 0
|
sizeof究竟是怎样实现的?
看了思一克的代码,编译了一下,看了个究竟
- void f(int len)
- {
- char array[len];
- array[0]='a';
- printf("sizeof array=%d\n",sizeof(len));
- }
复制代码
编译后
- .file "test.c"
- .section .rodata
- .LC0:
- .string "sizeof array=%d\n"
- .text
- .globl f
- .type f, @function
- f:
- pushl %ebp
- movl %esp, %ebp
- pushl %ebx
- subl $4, %esp
- movl %esp, %ebx
- movl 8(%ebp), %eax //取len到eax
- decl %eax
- incl %eax
- addl $15, %eax //和下边三句合起来做16字节对齐,不知道干啥
- shrl $4, %eax //
- sall $4, %eax //
- subl %eax, %esp //分配动态数组
- movl %esp, %eax //接下来的代码就容易看了
- movb $97, (%eax)
- subl $8, %esp
- pushl $4
- pushl $.LC0
- call printf
- addl $16, %esp
- movl %ebx, %esp
- movl -4(%ebp), %ebx
- leave
- ret
- .size f, .-f
- .section .note.GNU-stack,"",@progbits
- .ident "GCC: (GNU) 3.3.2 20031022 (Red Hat Linux 3.3.2-1)"
复制代码
如果不是大家争论,还真不知道有这种用法,汗阿[/code] |
|