Velocity Reviews > !!<====== Existence of a variable - Debunk a myth ======>!!

# !!<====== Existence of a variable - Debunk a myth ======>!!

Zero
Guest
Posts: n/a

 12-14-2008
Hi there,

I am a little bit irritated!
Consider a function fA which has a local variable iA (storage class is
auto).
Now, some books tell you,
that a variable does not exist until a value is passed to it, like
iA = 5;
Why is is then possible to write commands like
printf("%d", &iA);
Is this a random value or is it really the location of the variable?

Or consider the following code:
int iA;
int piA;

piA = &iA;

As some books say, iA does not exist, because not value is stored,
but to which address points piA.

So what really happens, when the function fA is entered?

Greetings from Zeh Mau

Harald van Dĳk
Guest
Posts: n/a

 12-14-2008
On Sun, 14 Dec 2008 02:34:55 -0800, Zero wrote:
> !!<====== Existence of a variable - Debunk a myth ======>!!

That's a bit excessive, no?

> Hi there,
>
> I am a little bit irritated!
> Consider a function fA which has a local variable iA (storage class is
> auto).
> Now, some books tell you,
> that a variable does not exist until a value is passed to it, like iA =
> 5;

If that's what those books say, those books are wrong. iA does exist as an
object, but it doesn't have a value yet.

> Why is is then possible to write commands like printf("%d", &iA);

To print a pointer value, use %p and cast to void *:
printf("%p", (void *) &iA);

%d is for printing integers. Pointers are not integers, and are not always
represented as integers.

Printing the address of iA is possible because iA exists already.

> Is this a random value or is it really the location of the variable?

It is the address of iA.

> Or consider the following code:
> int iA;
> int piA;
>
> piA = &iA;

This is also fine.

> As some books say, iA does not exist, because not value is stored, but
> to which address points piA.

piA points, as you would expect, to iA.

> So what really happens, when the function fA is entered?

Memory is reserved for iA as soon as the block (here, the function) is
entered, but it is not filled until you initialise or assign to iA.
(Compilers may do something different, for example reserve memory for iA
as soon as it is used -- including when its address is taken -- so long as
a valid program can't detect it.)

> What is the

Decent books on C should address this. What is it you're currently

Keith Thompson
Guest
Posts: n/a

 12-14-2008
Zero <(E-Mail Removed)> writes:
[...]
> Consider a function fA which has a local variable iA (storage class is
> auto).
> Now, some books tell you,
> that a variable does not exist until a value is passed to it, like
> iA = 5;

(Terminology: a value can be "passed" to a function. I think you mean
until a value is assigned to it.)

What books say this? An auto variable exists from the point of its
declaration until the end of its scope.

> Why is is then possible to write commands like
> printf("%d", &iA);
> Is this a random value or is it really the location of the variable?

It's possible to write that statement (not command), but it invokes
undefined behavior; "%d" expects an int argument, and you're giving it
a pointer value. However, you can meaningfully write:

printf("%p", (void*)&iA);

> Or consider the following code:
> int iA;
> int piA;
>
> piA = &iA;
>
> As some books say, iA does not exist, because not value is stored,
> but to which address points piA.

Any books that say that are wrong. iA exists; it just doesn't have a
meaningful value.

[...]

--
Keith Thompson (The_Other_Keith) http://www.velocityreviews.com/forums/(E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Richard Tobin
Guest
Posts: n/a

 12-14-2008
In article <(E-Mail Removed)>,
Zero <(E-Mail Removed)> wrote:

>Or consider the following code:
>int iA;
>int piA;

(I'm assuming you meant "int *piA;" here.)

>piA = &iA;
>
>As some books say, iA does not exist, because not value is stored,
>but to which address points piA.

This is not only legal, it's a perfectly reasonable thing to do,
because you can then set the value of iA by indirecting through
piA:

*piA = 6;

So from the point of view of the language, the variable certainly does
exist.

If you're looking for a way in which the assertion could be true, at
the implementation level it's possible that the system may not
allocate any real memory for the variable until it is assigned to.
Many operating systems extend the stack automatically when it is
accessed (in units of pages), so it is conceivable that your program
could fail at the point of assignment because there is insufficient
memory even though it was possible to take the address.

-- Richard
--
Please remember to mention me / in tapes you leave behind.

Zero
Guest
Posts: n/a

 12-14-2008
sorry, it must be...

Or consider the following code:
int iA;
int * piA;

Zeh Mau

Zero
Guest
Posts: n/a

 12-14-2008
> What books say this? *An auto variable exists from the point of its
> declaration until the end of its scope.

At the moment, it read "C the complete reference" written by Herbert
Schildt.

In the book, he says, when defining a variable, which means a value is
assigned to it,
storage is reserved for the object.
On the other hand, when declaring an object, only the type of object
and its name
is made public. So no storage is reserved.

Zeh Mau

August Karlstrom
Guest
Posts: n/a

 12-14-2008
Zero wrote:
> At the moment, it read "C the complete reference" written by Herbert
> Schildt.

http://catb.org/~esr/jargon/html/B/bullschildt.html

August

Martin Ambuhl
Guest
Posts: n/a

 12-14-2008
Zero wrote:

> Consider a function fA which has a local variable iA (storage class is
> auto).

Unless you are a vampire, there is no reason to use the Transylvanian
Heresy naming convention.

> Now, some books tell you,
> that a variable does not exist until a value is passed to it, like
> iA = 5;

I seriously doubt that any books on C tell you that.
Any auto variable exists at least from the moment that its declaration
in encountered.
An auto variable has no defined value until one is given it, either by
initialization or assignment.

> Why is is then possible to write commands like
> printf("%d", &iA);
> Is this a random value or is it really the location of the variable?

1) The variable exists from its declaration until control leaves the
smallest enclosing block. It has an address; the address has a value.
The contents of that variable will not have a defined value until given
one by assignment or initialization.

2) You are wrong to claim that 'printf("%d", &iA);' is a permissible
statement. The reason is that &iA is an address and %d is the specifier
for a signed int. These are conceptually different and may have very
different representations. You want
printf("%p", (void *)&iA);
%p is the specifier to print a pointer, and it expects a
pointer-to-void, so the cast (void *) is used to make sure that the
address &iA is in an appropriate form.

By the way, calling that statement a 'command' will create an impression
in the minds of real programmers, and that impression will not be one
you want.

> Or consider the following code:
> int iA;
> int piA;
>
> piA = &iA;
>
> As some books say, iA does not exist, because not value is stored,
> but to which address points piA.

No book on the C programming language says such a thing.
You need to learn to read more carefully.

> So what really happens, when the function fA is entered?

Any real book on C,

Martin Ambuhl
Guest
Posts: n/a

 12-14-2008
Zero wrote:
>> What books say this? An auto variable exists from the point of its
>> declaration until the end of its scope.

>
> At the moment, it read "C the complete reference" written by Herbert
> Schildt.

Herbert Schildt is well-known for his illegal code, his incoreect
understanding of C, his claiming that implementation-specific constructs
are standard, and his refusal to fix his errors. Unfortunately, he has
a very readable style that appeals to those who don't know better.

But I cannot believe that even Herbert Schildt would claim such utter
nonsense. A direct quotation would be nice instead of your paraphrase.
The odds are strong that you have misunderstood rather than Schildt
wrote such a thing. Still, his books are garbage and you should replace
that book with a real book on C as soon as possible.

Keith Thompson
Guest
Posts: n/a

 12-14-2008
Zero <(E-Mail Removed)> writes:
>> What books say this? *An auto variable exists from the point of its
>> declaration until the end of its scope.

I wrote the above. Please don't snip attribution lines, like the
"Zero <(E-Mail Removed)> writes:" line above.

> At the moment, it read "C the complete reference" written by Herbert
> Schildt.

Herbert Schildt writes bad books, but I think in this case you've
misunderstood him.

> In the book, he says, when defining a variable, which means a value
> is assigned to it, storage is reserved for the object. On the other
> hand, when declaring an object, only the type of object and its name
> is made public. So no storage is reserved.

An object *definition* causes an object to be created; this includes
reserving storage for it. And object *declaration* merely declares
that an object exists; it might be defined elsewhere. A definition is
also a declaration.

At file scope (outside any function), a line like
int obj = 42;
*defines* the object "obj". A line like:
int obj;
declares it, but may or may not define it.

Inside a function, any object declaration (unless it contains the word
"extern") is a definition, whether it includes an initialization or
not.

It's also important to understand the difference between assignment
and initialization. An initialization is part of a declaration. An
assignment is an expression (typically an expression statement) that
modifies the value of an object. They're distinct constructs that do
very similar things.

The presence of absence of an initialization in a declaration can
*sometimes* affect whether that declaration is a definition or not.
That's probably all Schildt meant to say, though I wouldn't be
surprised if he got it wrong (you haven't quoted his exact words).
But an initialization or assignment itself does not cause the object
to be created; the definition does that.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"