The following code passes a multidimensional array to a function that uses the
C99 format for accepting an array when the size is not known at compile time.
The code outputs incorrect numbers (like 6.91992e-310) if the function "show"
gets inlined, which happens if I do one of these:
  gcc -Wall -save-temps -std=c99 -O2 -finline-functions bug.c -o bug
  gcc -Wall -save-temps -std=c99 -O3 bug.c -o bug
Using less optimization (e.g. -O1) or using the __attribute__((noinline)) on
the "show" function results in correct operation, so I presume it is the
inlining that is not working.

-------------- bug.c -------------------------------------
#include <stdio.h>
void show(int n, int m, double a[n][m]) {
  int i, j;
  for (i = 0; i < n; i++) {
    for (j = 0; j < m; j++) {
      fprintf(stderr, "a[%d][%d] = %g\n", i, j, a[i][j]);

int main(int argc, char** argv) {
  double b[3][2] = {{1.1, 2.1}, {3.1, 4.1}, {5.1, 6.1}};
  return 0;

>> gcc -v
Using built-in specs.
Target: x86_64-suse-linux
Configured with: ../configure --prefix=/usr --infodir=/usr/share/info
--mandir=/usr/share/man --libdir=/usr/lib64 --libexecdir=/usr/lib64
--enable-checking=release --with-gxx-include-dir=/usr/include/c++/4.3
--enable-ssp --disable-libssp --with-bugurl=
--with-pkgversion='SUSE Linux' --disable-libgcj --disable-libmudflap
--with-slibdir=/lib64 --with-system-zlib --enable-__cxa_atexit
--enable-libstdcxx-allocator=new --disable-libstdcxx-pch
--enable-version-specific-runtime-libs --program-suffix=-4.3
--enable-linux-futex --without-system-libunwind --with-cpu=generic
Thread model: posix
gcc version 4.3.2 [gcc-4_3-branch revision 141291] (SUSE Linux)

           Summary: Passing multi-dimensional array to function using C99
                    VLA function parameter format fails if function is
           Product: gcc
           Version: 4.3.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: r dot j dot hogan at reading dot ac dot uk
 GCC build triplet: x86_64-suse-linux
  GCC host triplet: x86_64-suse-linux
GCC target triplet: x86_64-suse-linux

Reply via email to