Stack allocation in C Language (digging holes)

Source: Internet
Author: User

We all know that local variables are stored in the stack during the C language operation, and the space is allocated from high to low.

However, recently I encountered a program that made me a little confused.

First look at a program.

Obviously, addresses are allocated from high to low, just as expected.

Modify it a little and then run it.

Obviously, from low to high !!!

Clarify the problem: the stack should change the memory allocation mode for the memory occupied by local variables.

Why? Why? Why?

Use-s to generate assembly language.

Assembly language in the first case

.file"main.c".section.rodata.LC0:.string"Address s = Ox%x\n".LC1:.string"Address d = Ox%x\n".text.globlmain.typemain, @functionmain:.LFB0:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5andl$-16, %espsubl$32, %espmovl%gs:20, %eaxmovl%eax, 28(%esp)xorl%eax, %eaxmovl$6513249, 24(%esp)movw$25185, 21(%esp)movb$0, 23(%esp)leal24(%esp), %eaxmovl%eax, 4(%esp)movl$.LC0, (%esp)callprintfleal21(%esp), %eaxmovl%eax, 4(%esp)movl$.LC1, (%esp)callprintfmovl$1, %eaxmovl28(%esp), %edxxorl%gs:20, %edxje.L3call__stack_chk_fail.L3:leave.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE0:.sizemain, .-main.ident"GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3".section.note.GNU-stack,"",@progbits

Assembly Language in Case 2

.file"main.c".section.rodata.LC0:.string"Address s = Ox%x\n".LC1:.string"Address d = Ox%x\n".text.globlmain.typemain, @functionmain:.LFB0:.cfi_startprocpushl%ebp.cfi_def_cfa_offset 8.cfi_offset 5, -8movl%esp, %ebp.cfi_def_cfa_register 5andl$-16, %espsubl$32, %espmovl%gs:20, %eaxmovl%eax, 28(%esp)xorl%eax, %eaxmovl$6513249, 17(%esp)movl$1684234849, 21(%esp)movw$26213, 25(%esp)movb$0, 27(%esp)leal17(%esp), %eaxmovl%eax, 4(%esp)movl$.LC0, (%esp)callprintfleal21(%esp), %eaxmovl%eax, 4(%esp)movl$.LC1, (%esp)callprintfmovl$1, %eaxmovl28(%esp), %edxxorl%gs:20, %edxje.L3call__stack_chk_fail.L3:leave.cfi_restore 5.cfi_def_cfa 4, 4ret.cfi_endproc.LFE0:.sizemain, .-main.ident"GCC: (Ubuntu/Linaro 4.7.3-1ubuntu1) 4.7.3".section.note.GNU-stack,"",@progbits

There are obvious differences in the previous several mov statements. One is distribution from low to high, and the other is distribution from high to low.

Conjecture: the compiler optimizes the language to make the long string advanced stack.

But why?

Please answer.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.