Is initialization of a garbage variable really an initialization or just an assignment?

I usually see examples of initialization vs assignment like this:

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

      

Obviously something left as garbage or undefined is uninitialized in some way.

But can anyone determine if initialization will be reserved for definition statements and / or if assignments can be assigned as initializations?

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

      

Is there some technical reason why we can't say that int b is garbage-initialized (from a compiler's point of view)?

Also, if possible, it can be compared to initialization and parsing on non-primitive data types.

+3


source to share


3 answers


As far as the language standard is concerned, only the form int a = 5;

initialization is initialization. All forms b = 5;

are purpose.



The same goes for non-primitive types.

+4


source


I'll resurrect this thread to add an important point of view, since the OP's terminology is clear explanation. As @OliCharlesworth pointed out (and he is completely correct on this), since the C language standard is about initialization and assignment, they are two completely different things. For example (assuming a local area):

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 are not so picky about terminology, so they call "initialization" any "operation" that gives a variable its first significant value. So in the above example n = 12

there will be no initialization, whereas there k = 42

will be. Of course, this terminology is vague, imprecise and can be misleading (although it is used too often, especially by teachers when introducing programming to beginners). A simple example of this ambiguity is the previous global accounting example:

// 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 do you say about appointments in main

? The first is obviously only a task, but is the second initiative in vague, general terminology? Is the default 0

set to k

its first "significant" value or not?



Also, a variable is usually said to be uninitialized if no initialization or assignment has been applied to it. Considering:

int x; 
x = 42; 

      

one could say that it is x

not initialized before the assignment, but not after it. The term assignment and initializer are defined syntactically, but terms like "initialization" and "uninitialization" are often used to denote semantics (in somewhat informal usage). [Thanks to Keith Thompson for this last paragraph].

I don't like this vague terminology, but I need to know that it is used and, alas, not too rare.

+5


source


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

0


source







All Articles