Initialisierung globaler Variablen
-
Hallo!
Werden globale Variablen immer mit 0 bzw. NULL initialisiert, oder ist das kompilerabhängig?
-
globale variablen werden vom programmierer initialisiert. wenn nicht, dann wurden sie nicht initialisiert und ihr wert ist unbestimmt.
//EDIT: das gilt uebrigens nicht nur fuer globale variablen, sondern variablen generell.
-
Variablen mit statischer Speicherdauer (das betrifft u.a. globale Variablen) werden standardmäßig mit 0 initialisiert.
-
ISO/IEC 9899:TC3, 6.7.8 schrieb:
If an object that has automatic storage duration is not initialized explicitly, its value is
indeterminate. If an object that has static storage duration is not initialized explicitly,
then:
— if it has pointer type, it is initialized to a null pointer;
— if it has arithmetic type, it is initialized to (positive or unsigned) zero;
— if it is an aggregate, every member is initialized (recursively) according to these rules;
— if it is a union, the first named member is initialized (recursively) according to these
rules.Globale variablen haben static storage duration, sie werden also mit 0 initialisiert.
-
Cool.
Und was ist ein aggregate?
-
global n00b schrieb:
Und was ist ein aggregate?
Arrays und Strukturen.
-
thank you all!