副标题#e#
各人都知道在c语言的运行进程中,局部变量都是存放在栈中的,且是从高位到低位举办举办空间分派。
先看一个措施。

很明明,地点从高到低分派,和估量的一样。
稍微修改一下,再运行。

很明明,从低位到高位!!!
#p#副标题#e#
明晰一下问题:栈区会应为局部变量的占内存的巨细变动内存的分派方法。
为什么?为什么?为什么?
用-S生成汇编语言看一下
第一种环境的汇编语言
.file "main.c"
.section .rodata
.LC0:
.string "Address s = Ox%x\n"
.LC1:
.string "Address d = Ox%x\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 %gs:20, %eax
movl %eax, 28(%esp)
xorl %eax, %eax
movl $6513249, 24(%esp)
movw $25185, 21(%esp)
movb $0, 23(%esp)
leal 24(%esp), %eax
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
leal 21(%esp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call printf
movl $1, %eax
movl 28(%esp), %edx
xorl %gs:20, %edx
je .L3
call __stack_chk_fail
.L3:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3"
.section .note.GNU-stack,"",@progbits
第二种环境的汇编语言
.file "main.c"
.section .rodata
.LC0:
.string "Address s = Ox%x\n"
.LC1:
.string "Address d = Ox%x\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 %gs:20, %eax
movl %eax, 28(%esp)
xorl %eax, %eax
movl $6513249, 17(%esp)
movl $1684234849, 21(%esp)
movw $26213, 25(%esp)
movb $0, 27(%esp)
leal 17(%esp), %eax
movl %eax, 4(%esp)
movl $.LC0, (%esp)
call printf
leal 21(%esp), %eax
movl %eax, 4(%esp)
movl $.LC1, (%esp)
call printf
movl $1, %eax
movl 28(%esp), %edx
xorl %gs:20, %edx
je .L3
call __stack_chk_fail
.L3:
leave
.cfi_restore 5
.cfi_def_cfa 4, 4
ret
.cfi_endproc
.LFE0:
.size main, .-main
.ident "GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3"
.section .note.GNU-stack,"",@progbits
在前面的几句mov有很明明的差异,一个是从低到高分派,一个是从高到低分派.
意料:编译器对语言举办的优化,让长的字符串先进栈。
但为什么要这么做呢?
求解答。
本文转载自:http://blog.csdn.net/qp120291570/article/details/8889950
