[cfe-dev] Query regarding global value initialization standard

Karthik Bhat blitz.opensource at gmail.com
Wed Sep 25 04:49:48 PDT 2013


Hi All,
I was going through a gcc TC for C++11. The test case is as follows -

// { dg-options -std=c++0x }
// { dg-do run }

extern "C" void abort ();
extern int ar[2];

int f()
{
  int k = 0;
  if (ar[0] != 42 || ar[1] != 0)
    abort();
  return 1;
}

int i = f();

int ar[2] = {42,i};

int main()
{
  return 0;
}

During dynamic initialization of i in function f() the value of ar[0] is 0
in case of clang were as in case of gcc it is 42.

As per standard(section 3.6.2) all global values should initially be zero
initialized followed by const initialized if possible before dynamic
initialization takes place.
Hence as per standard the const initialization of int ar[2] = {42,i};
should fail as i is not a const here( which seems to be happening in
clang). Hence ar[0],ar[1] is still zero initialized because of which during
dynamic initialization in f() ar[0]  is 0 which seems to be the correct
behavior.

Can i conclude here that clang is behaving correctly and the tc is wrong?
or am i missing something which this gcc tc wanted to capture?

Thanks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130925/6216624f/attachment.html>


More information about the cfe-dev mailing list