Keith said:
Keith said:
(e-mail address removed) writes: [...]
I would instead say, that a static *variable* is something either with
file scope (declared outside of any function) or an auto with a static
declaration. General static objects also include initialization values
with implicit storage (such as inline strings, and potential structs,
or unions that contain them, initialized to something other than an
array) and clearly have a value that is self declared rather than being
0.
I'm not sure this distinction between objects and "variables" is
useful. The standard carefully avoids defining what a "variable" is
(though it uses the word informally a few times).
Yeah, well the "standard" is not something from which you can derive
any true understanding of anything.
I certainly can. I don't know what your problem is.
That's like saying spelling bee champions are expert linguists. A
language standard that goes out of its way to avoid the word variable
seems to me like it maybe has been built on questionable premises.
The fact that certain hunks of data can exist should be a seperate
concept to a variable it happens to be sitting in. A *variable* can
cause a hunk of data to inherit const semantics at run time while a
hunk of data intrinsically in of itsef cannot change its const
semantics. So this distinction kind of matters, and it means knowing
the difference between variables, and "objects" is a prerequisite for
"getting" this.
So for example, if you have an inline string, can you return it from a
function? It depends on the attributes of the *variable* it was first
bound to. It can be returned if it was stuck in a char *, but *not* if
it is stuck in a char[]. How does one think about this without
understanding that its through the variable declaration that we
determine this?
By understanding the attributes of the objects that the program is
operating on.
By "inline string", I assume you mean a string literal. A string
literal corresponds to a static object with certain attributes as
defined in the standard. If it's "stuck in a char*", then you have an
object of type char* that points to the object.
Well, more than that you have implicit storage. The inline string has
to actually exist as a wad of data somewhere.
[...] If it's "stuck in a
char[]", then you've copied the value of that object to another
object.
No, that's a matter of implementation.
char x[] = "xxxx";
is the same as
char x[];
memset (x, 'x', 4);
x[4] = '\0';
and a compiler might easily do that, without the requirement of the
existence of any "xxxx" "object" (actually, this is more likely with
char x[]="", for obvious reaons). Similarly, if you mismatch the size
of the array declaration with the inline string, the compiler has a
bunch of choices as to how to deal with the truncation or expansion of
the array, including the mutation of the representation of the inline
string where it is stored. Since there is no reference to the original
inline string, it does not actually have to exist, or have any property
you think it might have. There's no way for you to force the string
literal to have any particular manifestation you want it to outside of
a particular implementation.
The variable x on the other hand has all sorts of properties. Note
here that in both cases, the variable declaration tells us what the
nature of the string literal's manifestation is, not the string itself.
Understanding comes from knowing what is going on with the variable,
not the string literal.
[...] If that object has automatic storage duration, then returning
a pointer to that object outside the object's scope can cause
undefined behavior; the origin of the value stored in that object is
no longer relevant.
This is all stuff that I learned from the standard.
Yes, but there is the question of what you didn't learn.
I don't know what you mean by "variable",
Its a concept from computer programming. If you are not familliar with
computer programming, you may not have heard of it, but you can't call
yourself a computer programmer, if you don't know what a variable is.
[...] other than that it's
apparently something distinct from an object. If you can provide a
rigorous definition of the word "variable", perhaps we can discuss
this.
Independent named storage? In C I guess it would be declared
independent named storage.