A question about variables...

C

Chad

Say I have something like...

int sum = 0;
int i = 3;

sum = sum + i;

Would 'sum + i' refer to the value 3? Or does 'sum' just get replaced
with 0 and 'i' with 3 before it gets evaluted as 0 + 3.


Chad
 
S

Shao Miller

Say I have something like...

int sum = 0;
int i = 3;

sum = sum + i;

Would 'sum + i' refer to the value 3? Or does 'sum' just get replaced
with 0 and 'i' with 3 before it gets evaluted as 0 + 3.

On the last line, 'sum' will evaluate to the last-stored value '0' and
'i' will evaluate to the last-stored value '3' and those two will be
added together to result in '3' and that value will be stored to 'sum'.
And after that, the 'sum = ...' expression evaluates to the value that
was stored: '3'. After all that, that last result goes unused.

sum = sum + i;
sum = 0 + 3;
sum = 3;
3;
 
S

Seebs

Say I have something like...
int sum = 0;
int i = 3;
sum = sum + i;
Would 'sum + i' refer to the value 3? Or does 'sum' just get replaced
with 0 and 'i' with 3 before it gets evaluted as 0 + 3.

I don't think I understand the distinction you're making. In the abstract
semantics of the machine, what happens is:
retrieve value from sum (0)
retrieve value from i (3)
add two retrieved values together (yielding 3)
store this new value (3) in sum

In practice, a good optimizer might well just set sum to 3 in the first
place because it knows what happens.

-s
 
J

James Kuyper

Say I have something like...

int sum = 0;
int i = 3;

sum = sum + i;

Would 'sum + i' refer to the value 3? Or does 'sum' just get replaced
with 0 and 'i' with 3 before it gets evaluted as 0 + 3.

Your question is very odd. My best guess is that it reflects a serious
misunderstanding of the relationship between the source code and the
resulting program. But that doesn't make must sense, either - as I
recall, you've shown a considerably higher level of understanding in
previous messages posted to this newsgroup; at least, they were posted
using the same e-mail address.

The line in the source code which says

sum = sum + i;

Tells the compiler to create a program containing machine instructions
for the computer which tell it to perform the following tasks:

1. Retrieve the value currently stored in the variable named 'sum'.
2. Retrieve the value currently stored in the variable named 'i'.
3. Calculate the total of those two values.
4. Store the result into the variable named 'sum'.

Typically, the instructions will not refer to the variables by name.
Each of those variables are assigned to a different location in memory,
and the generated instructions simply refer to that memory location. All
trace of the character strings "sum" and "i" have disappeared by the
time the program has been created (that's not quite true, if the
variables have external linkage or if you compile in debugging mode -
but it's accurate enough for the purposes of this explanation).

Therefore, "0" does not replace "0", because the program doesn't contain
"sum", it contains machine instructions.
 
J

James Kuyper

On 05/20/2011 10:14 PM, James Kuyper wrote:
....
Therefore, "0" does not replace "0", because the program doesn't contain

Correction:
.... "0" does not replace "sum" ...
 
C

Chad

Your question is very odd. My best guess is that it reflects a serious
misunderstanding of the relationship between the source code and the
resulting program. But that doesn't make must sense, either - as I
recall, you've shown a considerably higher level of understanding in
previous messages posted to this newsgroup; at least, they were posted
using the same e-mail address.

I usually do. However, I tried to *over think* the problem. It
happens.

Chad
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,149
Latest member
Vinay Kumar Nevatia0
Top