Gary Labowitz wrote:
[SNIP]
This looks like fun, so I'll take my stab at it.
We have way too much time on our hands. ;-)
When a variable is made available to a program,
the compiler must have generated code that sets
aside some area of memory for use by the
program.
OK. Let's change memory to something what can be written in the standard...
let's call it storage.
Now we have to obfuscate your sentence, since if just anybody could
understand it (langauge) lawyers (like us ;-) ) would be out of business.
Variable is for example a very bad name, because programmers actually
understand what it means. Also it is not appropriate here: the process we
are going to describe is also applicable to unnamed temporaries, and those
are not variables.
In C++ the creation of an object is done in two steps, one of which is
optional for objects of POD types. In the first step appropriate (size,
alignment) storage is assigned to the object.
If the generated code of the compiler also puts a
predetermined value in the area of memory, this is called
initialization.
The second, optional for POD types, step is initialization.
I.e. the memory allocated to the variable, if
examined with (say) a cout << operation, will show a value that the
programmer specified in the declaration that requested the
allocation. However, if the programmer did not specify an initial
value in the declaration, all bets are off. The initial value of the
variable could be anything and is usually whatever was in the memory
area prior to its being used as the variable being declared.
Without initialization it is not guaranteed that the object represents a
valid value for its own type, therefore it is results in undefined behavior
(behaviour for Europeans) if the object is used as an rvalue.
Initialization might happen different ways. Initialization can take the
form of explicit initializer or implicit initialization.
For certain structures, the compiler will zero-initialize members
implicitly, without the programmer having to specifically declare an
initial value.
(list cases of implicit initialization)
In the case of arrays, for example, if fewer initial
values are provided than the array can store, zero-initialiation will
be done for the remaining array elements.
Once the variable is available to the program, any movement of a
different value into the variable, such as with an assignment
operation, is called assignment.
Storage belonging to objects of POD types might stay in an uninitialized
from until an initial rvalue of a correct type has been assigned to it.
[
int i; // Storage assigned, uninitialized
int j=i; // Undefined behavior, i is not initialized
i = 12; // After the ; i is initialized to 12 using assignment
]
The entire issue is: What is in the variable immediately
after its "creation?"
An uninitialized object, just like with classes. When the operator new
returns it points to a well aligned and big enough storage area, but this
storage area does not yet the conform to the requirements of its final type.
If it is a determinate value, specified by the
programmer, it has been "initialized" by the compiler.
And for PODs this initialization is done by the compiler in the form of
"first assignment".
[Note: by "memory area" I do not exclude use of registers, data
bases, or any other mechanism any given compiler chooses to use for
variable storage.]
It is called storage in the standard (for that very reason, that it might
not be RAM, which we usually think about when we say memory).
Does this make sense to anyone?
Sort of.