Home > Back-end >  A strange phenomenon to initialize struct with GNU C 's Compound Literals (maybe UB?)
A strange phenomenon to initialize struct with GNU C 's Compound Literals (maybe UB?)

Time:12-14

#include <cstdio> // GCC C  17
struct node{int x;}a[5];
signed main()
{
    int i=1;
    a[  i]=(node){i};
    for(int i=0;i<5;  i)printf("%d ",a[i].x);
}

Since C 17, a[ i]=i is not a UB.

According to Sequenced-before rules ,

a[ i]=i is equivalent to a[i 1]=i , i =1. (in C 17)

But why does the above code run output 0 0 2 0 0 instead of 0 0 1 0 0?

When I try :

#include <cstdio> // GCC C  17
struct node{int x;node(){x=0;}node(int _x){x=_x;}}a[5];
signed main()
{
    int i=1;
    a[  i]=node(i);
    for(int i=0;i<5;  i)printf("%d ",a[i].x);
}

there is no such problem , output 0 0 1 0 0.

I read the GNU documentation but I can’t find valid information.

So what is going on?

CodePudding user response:

I'd say it's a compiler bug. I tried your code with clang 13.0.0 and it produced the output you wanted. Same with icx 3.0.

But as have been pointed out in comments, compound literals are not a part of C 17 standard, so you cannot really expect this to work at all by just looking at the standard.

CodePudding user response:

When i use prefix operator „0 0 2 0 0” is exacly what i expect.

True, in this case i not supposed to expect that.

  • Related