 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jens Gustedt Guest
|
Posted: Wed Jun 27, 2012 4:26 am Post subject: Re: C scope and struct definitions |
|
|
Am 27.06.2012 01:45, schrieb Keith Thompson:
| Quote: | Jens Gustedt <jens.gustedt (AT) loria (DOT) fr> writes:
Am 26.06.2012 22:08, schrieb Alan Curry:
Scope is the boundary of a variable's existence at compile time.
of the identifier, to be precise
Lifetime is
the boundary of a variable's existence at runtime. They're basically the same
thing in different dimensions (temporal at runtime, spatial at compile time).
no, not at all. Lifetime is a property of objects, not of
variables. Many objects aren't related to any kind of scope, are not
related to variables or identifiers, namely those that are allocated
through malloc and friends.
[...]
What exactly do you mean by "variable"?
|
I refer to it as non-normative term that is used in the standard to
refer to an identifier with object type.
| Quote: | I tend to think of it as an informal word referring to a (probably
non-const) object that has a name, but I don't think that's how you're
using it.
I'm not saying your usage is wrong, but since the C standard doesn't
define "variable" I really can't tell what you mean by it.
|
It doesn't define it but it uses it in footnotes and non-normative
text at several occasions. From these uses I think the indended
meaning is pretty clear.
My point was that scope and lifetime are quite diffent concepts
because they are properties of completely different objects.
I don't think that this discussion will lead us much further.
Jens |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Wed Jun 27, 2012 5:01 am Post subject: Re: C scope and struct definitions |
|
|
Jens Gustedt <jens.gustedt (AT) loria (DOT) fr> writes:
| Quote: | Am 27.06.2012 01:45, schrieb Keith Thompson:
[...]
What exactly do you mean by "variable"?
I refer to it as non-normative term that is used in the standard to
refer to an identifier with object type.
|
That makes it sound like an identifier, not the object it refers to, is
a "variable". Was that your intent? The uses of "variable" in the
standard (in non-normative text) clearly refer to objects, not
identifiers.
| Quote: | I tend to think of it as an informal word referring to a (probably
non-const) object that has a name, but I don't think that's how you're
using it.
I'm not saying your usage is wrong, but since the C standard doesn't
define "variable" I really can't tell what you mean by it.
It doesn't define it but it uses it in footnotes and non-normative
text at several occasions. From these uses I think the indended
meaning is pretty clear.
My point was that scope and lifetime are quite diffent concepts
because they are properties of completely different objects.
|
Agreed (though I'd say "entities" rather than "objects"; identifiers
have scope, and objects have lifetime).
[...]
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Wed Jun 27, 2012 5:03 am Post subject: Re: C scope and struct definitions |
|
|
Keith Thompson <kst-u (AT) mib (DOT) org> writes:
| Quote: | pacman (AT) kosh (DOT) dhis.org (Alan Curry) writes:
[...]
A variable is a name, a value, an address, or some combination of those
possibilities, depending on when you look at it. When you're writing the code
(as opposed to compiling and running it), the variable is the name. If you
saw something like
[snip]
I asked upthread what you mean by "variable"; I hadn't yet read
the above.
In my opinion, saying that a variable is "a name, a value, an
address, or some combination of those possibilities, depending on
when you look at it" makes the term far too vague to be useful,
especially without knowing *how* it depends on when you look at it.
Which is why I prefer to use well-defined terms like "object" and
"identifier".
|
Sorry, I didn't keep track of who wrote what. I had asked Jens Gustedt
what he meant by "variable".
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
| Back to top |
|
 |
James Kuyper Guest
|
Posted: Wed Jun 27, 2012 8:57 am Post subject: Re: C scope and struct definitions |
|
|
On 06/27/2012 02:26 AM, Jens Gustedt wrote:
| Quote: | Am 27.06.2012 01:45, schrieb Keith Thompson:
....
What exactly do you mean by "variable"?
I refer to it as non-normative term that is used in the standard to
refer to an identifier with object type.
|
Terms can't be normative, only statements can be normative. Identifiers
identify things, and the things that they identify may have a type, but
identifiers themselves do not. Personally, I use the definition "A
variable is a named object.". That definition fits every appearance of
the word "variable" as a noun in the C99 standard; I haven't checked yet
whether that's still true for C2011.
--
James Kuyper |
|
| Back to top |
|
 |
Stefan Ram Guest
|
Posted: Wed Jun 27, 2012 3:33 pm Post subject: Re: C scope and struct definitions |
|
|
James Kuyper <jameskuyper (AT) verizon (DOT) net> writes:
| Quote: | identifiers themselves do not. Personally, I use the definition "A
variable is a named object.".
|
That would be too vague for my taste. For example, after
int a[3];
one might say that »a[1]« was a variable, because someone
called it »joe«.
A more important point is: Your definition makes being a
variable a property of objects: By your definition, a
variable is a /special kind of object/: an object that is
/named/. An object is given by a region of storage and a
type. Now, when such an object is given, can one determine
whether this given object is a variable by your definition?
Usually not, because there is no way to find names starting
at the object! (At run-time most names are lost, and at
compile time there are no objects yet, so names and objects
rarely seem to exist at the same time at all.)
So, my definition would choose a wording more like
»A variable is a /pair of a name and an object/, where
the name refers to the object[ and ...]«.
However, the point remains: Variable are usually chosen
to talk about source code, even when the source code is
only written on paper and never ever executed at all, it
still can have variables. So why include those objects
in the definition of a variable at all, when they never
might come into existence?
Also, I am not sure whether to use »name« or »identifier«
above.
Can an object have several names in C?
union u0
{ int a;
int b; };
comes to my mind.
How many variables are declared with
union u0 u;
? for example, 1, 2 or 3?
When a variable is a »named object« an /object/ with several
names counts as /one/ variable. When a variable is /pair/ of
a name and an object, each name counts as /one/ variable in
this case.
Do union member names count at all as variable names? |
|
| Back to top |
|
 |
Kenneth Brody Guest
|
Posted: Wed Jun 27, 2012 4:13 pm Post subject: Re: C scope and struct definitions |
|
|
On 6/26/2012 6:18 PM, Alan Curry wrote:
| Quote: | In article <jsdbb6$t43$1@dont-email.me>,
Eric Sosman <esosman@ieee-dot-org.invalid> wrote:
On 6/26/2012 4:08 PM, Alan Curry wrote:
Scope is the boundary of a variable's existence at compile time.
Ah. This is obviously some strange usage of the word "scope"
that I hadn't previously been aware of. More to the point, it's a
It's an explanation of the concept, in my own words, deliberately chosen to
not look like standardese. I did that because I believe if you can't explain
something in your own words, you don't really understand it.
|
But, you used "standardese" words to do so, thereby making it sound as if
you were misunderstanding the concepts. If you're going to use words that
have a specific meaning in the context you're using them, but you choose to
use them for something else, you are at great risk for being misunderstood.
While you might get away with calling the main box of a desktop computer the
"CPU", you would be way off base to call it the "hard drive" (as I have seen
done way too often).
[...]
| Quote: | A variable is a name, a value, an address, or some combination of those
possibilities, depending on when you look at it.
|
But, we were talking about "identifiers", of which "variables" are just a
small subset.
| Quote: | When you're writing the code
(as opposed to compiling and running it), the variable is the name. If you
saw something like
void foo(int i)
{
/*something*/
}
int bar(void)
{
return i;
}
Wouldn't you think it's reasonable to say that the reason bar() won't compile
is that the variable i doesn't exist there?
|
If you word it that way, yes. If you were to use the more technical "not in
scope", then yes as well. However, if you were to use something about the
"lifetime" of "i", then I would have to disagree.
| Quote: | If you don't accept that wording,
then you're too much of a language lawyer to ever make sense of the way real
programmers talk.
|
The problem was that you were using "scope" in a way different than its
defined meaning in the context of the C language.
| Quote: | Lifetime is
the boundary of a variable's existence at runtime. They're basically the same
thing in different dimensions (temporal at runtime, spatial at compile time).
And for local variables, they both default to the block enclosing the
declaration. At compile time, the variable can be referenced (i.e. its name
can be used) while that block is being parsed. At runtime, the variable can
be referenced (i.e. its value can be loaded from memory) while that block is
active.
Well, no. Whenever an auto variable's identifier is in scope,
that identifier exists, yes. But the opposite does not hold: Even
while the variable exists, its identifier can be out of scope. (I
can think of three ways for this to happen; there may be others.)
That's a fun puzzle. Shadowing of course, which is a "don't do that".
Preprocessor effects ruled out, I assume. Maybe a variant of shadowing in
which typedef is used in an inner block.
|
Not to mention a simple function call.
void foo(void)
{
int i;
bar();
}
void bar(void)
{
/* "i" still exists, but is out of scope. */
}
--
Kenneth Brody |
|
| Back to top |
|
 |
James Kuyper Guest
|
Posted: Wed Jun 27, 2012 5:00 pm Post subject: Re: C scope and struct definitions |
|
|
On 06/27/2012 01:33 PM, Stefan Ram wrote:
| Quote: | James Kuyper <jameskuyper (AT) verizon (DOT) net> writes:
identifiers themselves do not. Personally, I use the definition "A
variable is a named object.".
That would be too vague for my taste. For example, after
int a[3];
one might say that »a[1]« was a variable, because someone
called it »joe«.
|
That's not a name that a C program can attach to a[1]. A C++ program
could do so using a reference, but this is comp.lang.c.
Outside of a C context, (if you can figure out what "int a[3]" might
mean outside of C context), if someone wants to use the name 'joe' for
a[1], I have no objection to calling it a variable.
| Quote: | A more important point is: Your definition makes being a
variable a property of objects: By your definition, a
variable is a /special kind of object/: an object that is
/named/. An object is given by a region of storage and a
type. Now, when such an object is given, can one determine
whether this given object is a variable by your definition?
Usually not, because there is no way to find names starting
at the object! (At run-time most names are lost, and at
compile time there are no objects yet, so names and objects
rarely seem to exist at the same time at all.)
So, my definition would choose a wording more like
»A variable is a /pair of a name and an object/, where
the name refers to the object[ and ...]«.
However, the point remains: Variable are usually chosen
to talk about source code, even when the source code is
only written on paper and never ever executed at all, it
still can have variables. So why include those objects
in the definition of a variable at all, when they never
might come into existence?
|
I've never used the term "variable" to refer to anything other than the
object that might or might not come into existence, when the program is
running. When I want to refer to the identifier, rather than the object
it identifies, I'll use a phrase like "the variable's name"; which would
be a meaningless phrase if "variable" referred to the identifier, rather
than to the object it identifies.
| Quote: | Also, I am not sure whether to use »name« or »identifier«
above.
Can an object have several names in C?
union u0
{ int a;
int b; };
comes to my mind.
|
That's a good question.
| Quote: | How many variables are declared with
union u0 u;
? for example, 1, 2 or 3?
When a variable is a »named object« an /object/ with several
names counts as /one/ variable. When a variable is /pair/ of
a name and an object, each name counts as /one/ variable in
this case.
Do union member names count at all as variable names?
|
Yes. The only question is how many different variables there are. When
people are discussing how a term should be defined. I find it helpful to
focus on what you want to say, using the term. Can you think of
something you would want to say about a variable, that would be harder
(or easier) to say if a and b were different variables, than it would be
if they were considered a single variable?
I looked to the standard for some examples of how it uses the term. I
found one which would require a rewrite if 'a' and 'b' were considered
different variables: "A floating-point status flag is a system variable
...." (7.6p1). If the status flag were a member of a union, and if
different members of the same union with the same type were considered
different variables, then "a" would have to be changed to "one or more",
to cover the possibility that the flag was a union member.
Similarly, 7.17.2.1 p2: "Concurrent access to the variable being
initialized ..."; if it were a member of a union, and those members were
considered distinct variables, all three variables would be initialized
at the same time, so that would need re-wording too.
I stopped looking at that point, there may be more examples, in either
direction. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Wed Jun 27, 2012 5:03 pm Post subject: Re: C scope and struct definitions |
|
|
James Kuyper <jameskuyper (AT) verizon (DOT) net> writes:
| Quote: | On 06/27/2012 02:26 AM, Jens Gustedt wrote:
Am 27.06.2012 01:45, schrieb Keith Thompson:
...
What exactly do you mean by "variable"?
I refer to it as non-normative term that is used in the standard to
refer to an identifier with object type.
Terms can't be normative, only statements can be normative. Identifiers
identify things, and the things that they identify may have a type, but
identifiers themselves do not. Personally, I use the definition "A
variable is a named object.". That definition fits every appearance of
the word "variable" as a noun in the C99 standard; I haven't checked yet
whether that's still true for C2011.
|
By "non-normative", I meant that neither the term nor its definition
appears in normative text in the standard.
"A variable is a named object" is a reasonable definition. On the
other hand, it implies that a const-qualified object is a "variable",
even though it can't vary. And I've seen enough inconsistent uses
of the word that I generally avoid it except in very simple and
obvious cases.
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Wed Jun 27, 2012 5:11 pm Post subject: Re: C scope and struct definitions |
|
|
ram (AT) zedat (DOT) fu-berlin.de (Stefan Ram) writes:
| Quote: | James Kuyper <jameskuyper (AT) verizon (DOT) net> writes:
identifiers themselves do not. Personally, I use the definition "A
variable is a named object.".
That would be too vague for my taste. For example, after
int a[3];
one might say that »a[1]« was a variable, because someone
called it »joe«.
|
It seems sufficiently obvious to me that "named" means that the object
has a name (which is a single identifier) in the program text.
| Quote: | A more important point is: Your definition makes being a
variable a property of objects: By your definition, a
variable is a /special kind of object/: an object that is
/named/. An object is given by a region of storage and a
type. Now, when such an object is given, can one determine
whether this given object is a variable by your definition?
Usually not, because there is no way to find names starting
at the object! (At run-time most names are lost, and at
compile time there are no objects yet, so names and objects
rarely seem to exist at the same time at all.)
|
Yes, but I don't see that as a problem. In a running program, it doesn't
matter whether a given object is a "variable" or not; there's no need to
have a way to determine this.
We just use the word "variable" to refer to a named object, as in:
int x; /* define an int variable named "x" */
| Quote: | So, my definition would choose a wording more like
»A variable is a /pair of a name and an object/, where
the name refers to the object[ and ...]«.
|
Hmm. I've always thought of a "variable" as a kind of
object. Applying it to a "pair of a name and an object", as you
suggest, seems (a) counterintuitive, and (b) not particularly useful.
[...]
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Wed Jun 27, 2012 5:15 pm Post subject: Re: C scope and struct definitions |
|
|
Kenneth Brody <kenbrody (AT) spamcop (DOT) net> writes:
| Quote: | On 6/26/2012 6:18 PM, Alan Curry wrote:
[...]
A variable is a name, a value, an address, or some combination of those
possibilities, depending on when you look at it.
But, we were talking about "identifiers", of which "variables" are just a
small subset.
|
I've seen several people say that a variable is a kind of identifier.
I've always thought of a variable as a kind of *object*.
For example, given:
int xyz;
I'd say that "xyz is a variable", but what I mean by that is that the
object whose name is "xyz" is a variable. The identifier "xyz" isn't a
"variable"; it's the *name* of a variable.
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
| Back to top |
|
 |
Stefan Ram Guest
|
Posted: Wed Jun 27, 2012 6:01 pm Post subject: Re: C scope and struct definitions |
|
|
James Kuyper <jameskuyper (AT) verizon (DOT) net> writes:
| Quote: | I stopped looking at that point, there may be more examples, in either
direction.
|
If one searches for »variable« and then strips all uses of
»variable length/argument/number«, »condition variable«,
uses in footnotes, or uses related to concurrency and
»system variables«, what remains are 3 places:
- »the "integer promotions" require that the abstract
machine promote the value of each variable to int size«
- »behavior, but the value of the variable cannot
be used until a proper value is stored in it.«
- »variable c should be declared as int.«
And it seems that »variable« can be replace by »object« in
these three cases.
BTW: ISO/IEC 14882:2003(E) says:
»A variable is introduced by the declaration of an
object. The variable's name denotes the object.« |
|
| Back to top |
|
 |
Stefan Ram Guest
|
Posted: Wed Jun 27, 2012 6:12 pm Post subject: Re: C scope and struct definitions |
|
|
ram (AT) zedat (DOT) fu-berlin.de (Stefan Ram) writes:
| Quote: | BTW: ISO/IEC 14882:2003(E) says:
»A variable is introduced by the declaration of an
object. The variable's name denotes the object.«
|
And ISO 2382-2 says:
»variable - An entity whose value may be indeterminate,
or indeterminate between known limits, until an actual
value is assigned to it in a given application.«
(which should be similar to ANSDIT).
N1570 even gives ISO 2382 as a normative reference, but only
the first part ISO 2382-1, not ISO 2382-2.
The ISO-2382-2 definition does not require a variable to
have a name, which is as in Java, where variables do not
need to have names, too. So it is similar to »object« in C.
The wording of the ISO-2382-2 definition also seems too
sloppy to me, since it emphasizes a special property (that
the value of a variable might be indeterminate), before
taking care to explicitly establish the foundations, i.e.,
that a variable is an entity that can have a value at all
and what it means for an entity to »have« a value. |
|
| Back to top |
|
 |
Stefan Ram Guest
|
Posted: Wed Jun 27, 2012 6:28 pm Post subject: Re: C scope and struct definitions |
|
|
ram (AT) zedat (DOT) fu-berlin.de (Stefan Ram) writes:
| Quote: | And ISO 2382-2 says:
|
»ISO 2382« is »Information technology. Vocabulary«,
but I should have chosen »ISO 2382-7« which is specifically
»Part 7: Computer programming«. For »variable«, it refers to
»Part 15: Programming languages« which eventually gives:
»variable - A quadruple, established by a declaration or
an implicit declaration, that consists of an identifier,
a set of data attributes, one or more addresses, and
data values, where the relationship between the
addresses and the data values may vary.«
One arrives at my »pair of a name and an object«, when on
considers that the other three parts of the quadruple in C
are already part of the notion of »object«. |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Wed Jun 27, 2012 7:48 pm Post subject: Re: C scope and struct definitions |
|
|
ram (AT) zedat (DOT) fu-berlin.de (Stefan Ram) writes:
[...]
| Quote: | BTW: ISO/IEC 14882:2003(E) says:
»A variable is introduced by the declaration of an
object. The variable's name denotes the object.«
|
That's the 2003 C++ standard. The 2011 standard (as of the N3337 draft)
expands on that a bit:
A *variable* is introduced by the declaration of a reference other
than a non-static data member or of an object. The variable’s name
denotes the reference or object.
I find that definition unsatisfying. It says how a variable is
introduced, and that it has a name, but it doesn't say what a
variable *is* -- which is exactly what a definition is supposed
to do.
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
| Back to top |
|
 |
Stefan Ram Guest
|
Posted: Wed Jun 27, 2012 8:01 pm Post subject: Re: C scope and struct definitions |
|
|
Keith Thompson <kst-u (AT) mib (DOT) org> writes:
| Quote: | That's the 2003 C++ standard. The 2011 standard (as of the N3337 draft)
expands on that a bit:
A *variable* is introduced by the declaration of a reference other
than a non-static data member or of an object. The variable's name
denotes the reference or object.
I find that definition unsatisfying. It says how a variable is
introduced, and that it has a name, but it doesn't say what a
variable *is* -- which is exactly what a definition is supposed
to do.
|
Yes, a definition actually should have the form that it
gives a superordinate concept already defined before and
specific properties, where the most general superordinate
concept is »entity«, which is something without any more
specific properties (in general English language, not in C).
For example: »an even number (term to be defined) is
an integral number (superordinate concept already defined)
n, so that n % 2 == 0 (specific properties).« |
|
| Back to top |
|
 |
Powered by phpBB © 2001, 2006 phpBB Group
|