[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Tinycc-devel] Fwd: TCC VLA bug?
From: |
Stefanos |
Subject: |
Re: [Tinycc-devel] Fwd: TCC VLA bug? |
Date: |
Fri, 3 Dec 2021 04:01:57 +0200 |
On Fri, 3 Dec 2021 01:06:35 +0200
Stefanos via Tinycc-devel <tinycc-devel@nongnu.org> wrote:
> With `gcc -std=c99` and `gcc -std=c11` I get the - identical in both cases -
> following output:
>
> 1 2 3 4 5
> 6 7 8 9 10
> 11 12 13 14 15
> 16 17 18 19 20
>
> 21 22 23 24 25
> 26 27 28 29 30
> 31 32 33 34 35
> 36 37 38 39 40
>
> 41 42 43 44 45
> 46 47 48 49 50
> 51 52 53 54 55
> 56 57 58 59 60
>
> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
> 20
> 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
> 40
> 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
> 60
>
> With `tcc -std=c99` though and `tcc -std=c11` I get completely different
> results:
>
> C99 output
> ----------
>
> 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
>
>
> C11 output
> ----------
>
> 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 16391 0-107212368 22035-107212704 22035-107212592
> 22035 0 0 536872934 0 8 0
> 5 0-107212536 22035-107212648 22035-107212536 22035 0 0 536870912
> 0 -1 0 7 0-107212368 22035-107212592 22035
>
>
> For some reason, it gives me the impression it seems '21' as `2+1` thus '3',
> `2+2` thus '4', and so forth; I could be wrong though...
>
Seems like it's related to `malloc()` somehow; with fix-sized array it behaves
as expected.
Here's the code, a bit modified for debugging purposes:
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
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;
int starr[3][4][5];
/* 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++) {
starr[z][y][x] = c++;
printf(
"(%p) starr[%i][%i][%i]: % 4i\n",
&starr[z][y][x], z, y, x, starr[z][y][x]
);
}
puts("");
}
puts("");
}
putchar('\n');
/* print structured */
for (int z=0; z<d; z++) {
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
printf(
"(%p) starr[%i][%i][%i]: % 4i\n",
&starr[z][y][x], z, y, x, starr[z][y][x]
);
}
puts("");
}
puts("");
}
/* reset c to 1 */
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++;
printf(
"(%p) arr[%i][%i][%i]: % 4i\n",
&arr[z][y][x], z, y, x, arr[z][y][x]
);
}
puts("");
}
puts("");
}
putchar('\n');
/* print structured */
for (int z=0; z<d; z++) {
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
printf(
"(%p) arr[%i][%i][%i]: % 4i\n",
&arr[z][y][x], z, y, x, 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("");
}
}