c - Does accesing .text segment via `extern` variables cause undefined-behaviour? -


this file "1.c"

    #include <stdio.h>     char foo;     int bar(){     }     int main(){         printf("%d",foo);         return 0;     }     //-------------------------- 

this file '2.c'

    void foo(){     } 

compiler invoked gcc 1.c 2.c

does above gives undefined behaviour? guess is, yes. otherwise it's impossible optimization.

multiple different definitions same entity (class, template, enumeration, inline function, static member function, etc.) [what common undefined behaviour c++ programmer should know about?

but far know char foo produce weak symbol can overridden void foo(){} @ linkage. furthermore, if change char foo extern char foo, still definition?

it'd cause undefined behaviour, yes. there lots of quotes standard explicit various types of declarations , forth, sums up:

in set of translation units , libraries constitutes entire program, each declaration of particular identifier external linkage denotes same object or function. (6.2.2)

all declarations refer same object or function shall have compatible type; otherwise, behavior undefined. (6.2.7)

char foo; in example tentative definition. if use extern, not definition, it'd still declaration, , above still apply.


Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -