Discussion:
Are we checking if the variable has been assigned before using it?
(too old to reply)
Noureen Syed
2004-07-13 14:58:40 UTC
Permalink
As I had asked Jaewook yesterday about what should happen in the case:

int a, b;
a <- 10;
a = a + b;

Jaewook mentioned that we don't care about what b is and basically take
whatever garbage value is in the stack for it and do the addition anyways.
Now depending on that garbage, this would get caught as an error, once type
checking is implemented because we are not adding two integers or maybe it
won't get caught.

Basically, do we need to keep track of whether a variable has been assigned
a value or not before it is used in an expression?

The confusion arises because I was told by Jaewook that I don't need to keep
track of that. However, when a colleague of mine checked with Troy, Troy
mentioned that an error should be generated when "b" is used in the above
example before having a value assigned to it. So that means we keep track of
it now???

Please clarify this.

Thank you.

ns
Hansel Ip
2004-07-14 03:09:47 UTC
Permalink
Instead of having to keep track of unassigned variables and throw
errors...can we simply initialize all new int variables to zero on the
stack? And default the new boolean values to 'false' (zero)?

That way for the example code,

int a,b;
a <- 42;
a <- a+b;

'a' will simply remain the same, unaffected by the "unassigned" value b?

Thanks . .
Troy Mark Gonsalves
2004-07-20 17:32:33 UTC
Permalink
Post by Noureen Syed
int a, b;
a <- 10;
a = a + b;
Jaewook mentioned that we don't care about what b is and basically take
whatever garbage value is in the stack for it and do the addition anyways.
Now depending on that garbage, this would get caught as an error, once type
checking is implemented because we are not adding two integers or maybe it
won't get caught.
It won't get caught. Type checking is done at compile time. At run time,
the garbage taken for b will be (successfully) interpreted as an integer.
Remember the "Eye of the Beholder"-slide from class...it's just
32-bits...the CPU doesn't know that it's garbage, it wants an integer, any
32-bit string is a valid integer so the operation will be performed
without generating an error (unless the Mips simulator crashes because of
an overflow).
Post by Noureen Syed
Basically, do we need to keep track of whether a variable has been assigned
a value or not before it is used in an expression?
No.
Post by Noureen Syed
The confusion arises because I was told by Jaewook that I don't need to keep
track of that. However, when a colleague of mine checked with Troy, Troy
mentioned that an error should be generated when "b" is used in the above
example before having a value assigned to it. So that means we keep track of
it now???
I don't recall saying that, It's likely that I said something else
and I was misinterpreted.

Note: initializing your variables to 0 (as Hansel suggested) is nice, but
not required for this assignment. Whether variables are given
an initial value automatically is something that is usually specified
in the language definition...It's not specified for SL, so it's up to the
SL-programmer to make sure that they initialize their variables
appropriately.

Continue reading on narkive:
Loading...