公众号【嵌入式er笔记】持续记录和分享C/C++、Linux、ARM、Android、IoT等技术相关知识,以及职场、生活经验和感悟。
分类: LINUX
2013-03-01 21:23:23
原文地址:对张孝祥C语言试题其中一题的探讨 作者:chenzhufly
int x=35;
char str[10];
strcpy(str,""/*共13个字母*/);
//问:此时x和strlen(str)的值分别是多少? |
…… |
78 |
56 |
34 |
12 |
…… |
…… |
12 |
34 |
56 |
78 |
…… |
1 #include
2 #include
3
4 int main()
5 {
6 int x = 35;
7 char str[10];
8 strcpy(str,""/*共13个字母*/);
9 printf("%d\n",x);
10 return 0;
11 } |
1: #include
2: #include
3:
4: int main()
5: {
00401010 push ebp
00401011 mov ebp,esp
00401013 sub esp,50h
00401016 push ebx
00401017 push esi
00401018 push edi
00401019 lea edi,[ebp-50h]
0040101C mov ecx,14h
00401021 mov eax,0CCCCCCCCh
00401026 rep stos dword ptr [edi]
6: int x = 35;
00401028 mov dword ptr [ebp-4],23h /*将35压进栈中*/ (1处)
7: char str[10];
8: strcpy(str,""/*共13个字母*/);
0040102F push offset string "" (00420020)
00401034 lea eax,[ebp-10h] (2处) 00401037 push eax
00401038 call strcpy (00401100)
0040103D add esp,8
9: printf("%d\n",x);
00401040 mov ecx,dword ptr [ebp-4] (3处)
00401043 push ecx
00401044 push offset string "%d\n" (0042001c)
00401049 call printf (00401080)
0040104E add esp,8
10: return 0;
00401051 xor eax,eax
11: } |
栈帧布局 高地址
低地址 |
图 4字符串在栈中的布局
.file "sttest.c"
.section .rodata
.align 32
.LC0:
.string ""
.string ""
.LC1:
.string "%d\n"
.text
.globl main
.type main, @function
main:
pushl %ebp
movl %esp, %ebp
subl $56, %esp
andl $-16, %esp
movl $0, %eax
subl %eax, %esp
movl $35, -12(%ebp) /*将35放到栈中,即x处*/ (1处)
movl $.LC0, 4(%esp) leal -40(%ebp), %eax (2处) movl %eax, (%esp) call strcpy
movl -12 (%ebp), %eax (3处)
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call printf
movl $0, %eax
leave
ret
.size main, .-main
.section .note.GNU-stack,"",@progbits
.ident "GCC: (GNU) 3.3.5 (Debian 1:3.3.5-13)" |