- 论坛徽章:
- 0
|
回复 218# pmerofc
#include <stdio.h>
int main( void )
{
int i = 0 ;
printf(" %u \n" , sizeof (int *) ) ;
printf(" %u \n" , sizeof (int *) & i ) ;
return 0;
}
上面代码的结果:
4
0
#include <stdio.h>
int main( void )
{
int i ;
int *p = &i;
printf(" %u \n" , sizeof (int *) ) ;
printf(" %u \n" , sizeof (int *) p ) ;
return 0;
}
上面代码编译不通过:
comoon@demon:~/code/tmp$ gcc -o test3 test3.c
test3.c: In function ‘main’:
test3.c:9:40: error: expected ‘)’ before ‘p’
测试编译器:gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
系统:ubuntu12.04
因为我是新手,没完全读过c99标准,也对编译器不是灰常了解,不过我觉得像这种问题,应该把这个c代码对应的汇编研究下,很有帮助!虽然我没系统完全的学过汇编,不会照着手册也凑合看。。
下面是你第一段代码的汇编
.file "test2.c"
.section .rodata
.LC0:
.string " %u \n"
.text
.globl main
.type main, @function
main:
.LFB0:
.cfi_startproc
pushl %ebp
.cfi_def_cfa_offset 8
.cfi_offset 5, -8
movl %esp, %ebp
.cfi_def_cfa_register 5
andl $-16, %esp
subl $32, %esp
movl $0, 28(%esp)
movl $.LC0, %eax
movl $4, 4(%esp)
movl %eax, (%esp)
call printf
movl 28(%esp), %eax
movl %eax, %edx
andl $4, %edx
movl $.LC0, %eax
movl %edx, 4(%esp)
movl %eax, (%esp)
call printf
movl $0, %eax
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3"
.section .note.GNU-stack,"",@progbits
而且,编译器的具体实现也不一样。。。有必要也可以看下gcc的源码。。(太多,不适合我这种新手!) |
|