sabgenton
sabgenton

Reputation: 1893

Is initializing a garbage variable truly initializing or just an assignment?

I generally see examples of initialisation vs assignment like this:

int funct1(void)
{int a = 5; /*initialization*/
a = 6;}     /*assignment*/

Obviously something left as garbage or undefined somehow is uninitialized.

But could some one please define if initialization is reserved for definition statements and/or whether assignments can be called initialisation?

int funct2(void)
{int b;     
b = 5;}     /*assignment, initialization or both??*/

Is there much of a technical reason why we can't say int b is initialised to garbage (from the compilers point of view)?

Also if possible could this be compared with initializing and assinging on non-primitive data types.

Upvotes: 3

Views: 216

Answers (3)

I'll resurrect this thread to add an important point of view, since the puzzlement about terminology by the OP is understandable. As @OliCharlesworth pointed out (and he's perfectly right about that) as far as the C language standard is concerned initialization and assignment are two completely different things. For example (assuming local scope):

int n = 1;    // definition, declaration and **initialization**
int k;        // just definition + declaration, but no initialization
n = 12;       // assignment of a previously initialized variable
k = 42;       // assignment of a previously UNinitialized variable

The problem is that many books that teach programming aren't so picky about terminology, so they call "initialization" any "operation" that gives a variable its first meaningful value. So, in the example above, n = 12 wouldn't be an initialization, whereas k = 42 would. Of course this terminology is vague, imprecise and may be misleading (although it is used too often, especially by teachers when introducing programming to newbies). As a simple example of such an ambiguity let's recast the previous example taking global scope into account:

// global scope
int n = 1;    // definition, declaration and **initialization**
int k;        // definition, declaration and **implicit initialization to 0**

int main(void)
{
    n = 12;       // assignment of a previously initialized variable
    k = 42;       // assignment of a previously initialized variable

   // ... other code ...
}

What would you say about the assignments in main? The first is clearly only an assignment, but is it the second an initialization, according to the vague, generic terminology? Is the default value 0 given to k its first "meaningful" value or not?

Moreover a variable is commonly said to be uninitialized if no initialization or assignment has been applied to it. Given:

int x; 
x = 42; 

one would commonly say that x is uninitialized before the assignment, but not after it. The terms assignment and initializer are defined syntactically, but terms like "initialization" and "uninitialized" are often used to refer to the semantics (in somewhat informal usage). [Thanks to Keith Thompson for this last paragraph].

I dislike this vague terminology, but one should be aware that it is used and, alas, not too rare.

Upvotes: 5

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272517

As far as the language standard is concerned, only statements of the form int a = 5; are initialisation. Everything of the form b = 5; is an assignment.

The same is true of non-primitive types.

Upvotes: 4

Mr Lister
Mr Lister

Reputation: 46579

And to "Is there much of a technical reason why we can't say int b is initialised to garbage", well, if you don't put any value into a memory location, it's not "initialisation". From the compiler's point of view, no machine language instruction is generated to write to the location, so nothing happens.

Upvotes: 0

Related Questions