tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Tinycc-devel] Fwd: TCC VLA bug?


From: grischka
Subject: [Tinycc-devel] Fwd: TCC VLA bug?
Date: Wed, 01 Dec 2021 15:18:05 +0100
User-agent: Thunderbird 2.0.0.23 (Windows/20090812)

Got this report on private email.  Not sure what it means ...
---------------------------------------------------------->>

Output of the code below if compiled with TCC is pretty messy:
array values are "misplaced" and overwrite each other.
But everything's ok if compiled with GCC.

#include <stdlib.h>
#include <stdio.h>

int main() {
    const int w = 5;
    const int h = 4;
    const int d = 3;

    int (*arr)[h][w] = malloc(sizeof(int) * d*h*w);

    int c = 1;

    /* fill with consecutive numbers */
    for (int z=0; z<d; z++) {
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                arr[z][y][x] = c++;
            }
        }
    }

    /* print structured */
    for (int z=0; z<d; z++) {
        for (int y=0; y<h; y++) {
            for (int x=0; x<w; x++) {
                printf("% 4i", arr[z][y][x]);
            }
            puts("");
        }
        puts("");
    }

    /* print plain */
    for (int i=0; i<d*h*w; i++) {
        printf("% 4i", ((int*)arr)[i]);
        if ((i+1) % (w*h) == 0) puts("");
    }
}
```

```
$ tcc vla.c && ./a.out
   1   2  21  22  41
  42  43  44  45  46
  47  48  49  50  51
  52  53  54  55  56

  21  22  41  42  43
  44  45  46  47  48
  49  50  51  52  53
  54  55  56  57  58

  41  42  43  44  45
  46  47  48  49  50
  51  52  53  54  55
  56  57  58  59  60

   1   2  21  22  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56
  57  58  59  60   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0   0
`




reply via email to

[Prev in Thread] Current Thread [Next in Thread]