https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70514
Bug ID: 70514 Summary: Variable length arrays lead to garbage in another array Product: gcc Version: 5.3.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: kwpolska at gmail dot com Target Milestone: --- Created attachment 38162 --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=38162&action=edit The broken code. I’m writing a really simple algorithm to find all prime factors of a compound integer n. factorize() needs to know primes, so it uses a variable-length array that it passes to the sort_of_sieve() function. It should also make its results easy to modify, so an array OUT[50] for results is passed. Desired output of the program: 2 2 2 2 The output of the program is random and might be one of: a) four twos b) four twos and a segfault c) four numbers that are some random stack garbage (and maybe a segfault) d) a segfault, without any output e) a floating point exception (?!) Replacing the primes VLA with bool *primes = new bool[n]; works. Compiled with (no error/warning output): $ g++ -Wall -Wextra vla-bug.cpp -o vla -save-temps This program compiles with clang++ v3.7.1. MSVC++ does not support VLAs and thus refuses to compile. Running on Arch Linux. $ g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-unknown-linux-gnu/5.3.0/lto-wrapper Target: x86_64-unknown-linux-gnu Configured with: /build/gcc/src/gcc-5-20160209/configure --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=https://bugs.archlinux.org/ --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ --enable-shared --enable-threads=posix --enable-libmpx --with-system-zlib --with-isl --enable-__cxa_atexit --disable-libunwind-exceptions --enable-clocale=gnu --disable-libstdcxx-pch --disable-libssp --enable-gnu-unique-object --enable-linker-build-id --enable-lto --enable-plugin --enable-install-libiberty --with-linker-hash-style=gnu --enable-gnu-indirect-function --disable-multilib --disable-werror --enable-checking=release Thread model: posix gcc version 5.3.0 (GCC)