c - Why does the compiler allocate more than needed in the stack? -


i have simple c program. let's say, example, have int , char array of length 20. need 24 bytes in total.

int main() {    char buffer[20];    int x = 0;    buffer[0] = 'a';    buffer[19] = 'a'; } 

the stack needs aligned 16 bytes boundary, presume compiler reserve 32 bytes. when compile such program gcc x86-64 , read output assembly, compiler reserves 64 bytes.

..\gcc -s -o main.s main.c 

gives me:

    .file   "main.c"     .def    __main; .scl    2;  .type   32; .endef     .text     .globl  main     .def    main;   .scl    2;  .type   32; .endef     .seh_proc   main main:     pushq   %rbp                        # rbp pushed, no need reserve more     .seh_pushreg    %rbp     movq    %rsp, %rbp     .seh_setframe   %rbp, 0     subq    $64, %rsp                   # reserving 64 bytes     .seh_stackalloc 64     .seh_endprologue     call    __main     movl    $0, -4(%rbp)                # using first 4 bytes store int     movb    $97, -32(%rbp)              # using rbp-32      movb    $97, -13(%rbp)              # rbp-13 store char array     movl    $0, %eax     addq    $64, %rsp                   # restoring stack last 32 bytes unused     popq    %rbp     ret     .seh_endproc     .ident  "gcc: (x86_64-posix-seh-rev0, built mingw-w64 project) 5.2.0" 

why that? when program assembly, reserve minimum memory need without problem. limitation of compiler has trouble evaluating needed memory or there reason that?

here gcc -v

using built-in specs. collect_gcc=gcc collect_lto_wrapper=d:/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/5.2.0/lto-wrapper.exe target: x86_64-w64-mingw32 configured with: ../../../src/gcc-5.2.0/configure --host=x86_64-w64-mingw32 --build=x86_64-w64-mingw32 --target=x86_64-w64-mingw32 --prefix=/mingw64 --with-sysroot=/c/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64 --with-gxx-include-dir=/mingw64/x86_64-w64-mingw32/include/c++ --enable-shared --enable-static --disable-multilib --enable-languages=c,c++,fortran,objc,obj-c++,lto --enable-libstdcxx-time=yes --enable-threads=posix --enable-libgomp --enable-libatomic --enable-lto --enable-graphite --enable-checking=release --enable-fully-dynamic-string --enable-version-specific-runtime-libs --disable-isl-version-check --disable-libstdcxx-pch --disable-libstdcxx-debug --enable-bootstrap --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-gnu-as --with-gnu-ld --with-arch=nocona --with-tune=core2 --with-libiconv --with-system-zlib --with-gmp=/c/mingw520/prerequisites/x86_64-w64-mingw32-static --with-mpfr=/c/mingw520/prerequisites/x86_64-w64-mingw32-static --with-mpc=/c/mingw520/prerequisites/x86_64-w64-mingw32-static --with-isl=/c/mingw520/prerequisites/x86_64-w64-mingw32-static --with-pkgversion='x86_64-posix-seh-rev0, built mingw-w64 project' --with-bugurl=http://sourceforge.net/projects/mingw-w64 cflags='-o2 -pipe -i/c/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64/opt/include -i/c/mingw520/prerequisites/x86_64-zlib-static/include -i/c/mingw520/prerequisites/x86_64-w64-mingw32-static/include' cxxflags='-o2 -pipe -i/c/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64/opt/include -i/c/mingw520/prerequisites/x86_64-zlib-static/include -i/c/mingw520/prerequisites/x86_64-w64-mingw32-static/include' cppflags= ldflags='-pipe -l/c/mingw520/x86_64-520-posix-seh-rt_v4-rev0/mingw64/opt/lib -l/c/mingw520/prerequisites/x86_64-zlib-static/lib -l/c/mingw520/prerequisites/x86_64-w64-mingw32-static/lib ' thread model: posix gcc version 5.2.0 (x86_64-posix-seh-rev0, built mingw-w64 project)  

compilers may indeed reserve additional memory themselves.

gcc has flag, -mpreferred-stack-boundary, set alignment maintain. according the documentation, default 4, should produce 16-byte alignment, needed sse instructions.

as vermillionazure noted in comment, should provide gcc version , compile-time options (use gcc -v show these).


Comments

Popular posts from this blog

python - Selenium remoteWebDriver (& SauceLabs) Firefox moseMoveTo action exception -

html - How to custom Bootstrap grid height? -

transpose - Maple isnt executing function but prints function term -