What is the behavior when there are inappropriate types between the extern declaration and the definition?

Suppose I have two files:

== File1 ==

extern char* foo;

== File2 ==

double foo;

These two files seem to be compiling and are very well linked with g ++ and clang ++, despite the type mismatch. As far as I understand, the recommended practice is to put the extern declaration in the header, which both files include, so that File2 will cause an override error.

My questions:

  • Does this lead to undefined behavior according to the C ++ standard? If not what happens in foo in File1?
  • Can linkers catch this type of type mismatch?
+2
source share
2 answers

undefined ++?

, , undefined ( ). , , . - , . , , decl/def (. 3.5, 7.5 "extern" "linkage" ), :

, .

, , , . , , , " undefined", UB - ( , , , , ). " " , , , FUBAR. , , , ​​, , , .

?

, . ( ) , , , (, ), (), . , .

, , , , , , " , ".

+2

undefined ++?

, .

, foo File1?

Uhm, , double foo; . , , NULL , , , - foo foo .

, foo, . foo = malloc(100);, double foo , , , , , , 64- , , , , .

?

, "" , ++. , .

, - COULD , , . . .

+2

All Articles