Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Global array defined twice but still compiling?

In a.h, I created an array without the extern keyword by mistake which should result in a tentative definition

a.h:

    MyStruct myArrayOfStructs [];

This array is then defined in a.cpp

a.cpp:

MyStruct myArrayOfStructs[CONSTANT];

This surprisingly compiled.

Why did the compiler not complain about redefinition?

like image 348
Philsbury Avatar asked Dec 24 '22 16:12

Philsbury


1 Answers

That is perfectly valid. The first (.h) says there exists an array somewhere, but does not instantiate it.

The second allocates it.

like image 178
wallyk Avatar answered Jan 09 '23 05:01

wallyk