-
August 20th, 2007, 02:23 AM
#1
Dumb question about assignment...
Well maybe it's an intelligent question. Let's find out.... consider these 2 examples:-
Code:
// First version
while (whatever)
{
int a;
a = 6;
// rest of loop
}
// Now for an alternative version
while (whatever)
{
int a = 6;
// rest of loop
}
In the first example, 'a' gets initialised sometime after being declared. Therefore it's value at the start of each iteration of the 'while' loop is guaranteed to be 6.
But what about the second example - where 'a' gets initialised at the same time as being declared? With my compiler (MSVC++) 'a' gets reset to 6 on each iteration of the loop but is that likely to be true for all compilers? Or are there some where 'a' would only get initialised once? It's something I've never been 100% certain about.
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
August 20th, 2007, 02:40 AM
#2
Re: Dumb question about assignment...
The assignment are the same. Maybe you're mixing this up with static local variables which are assigned only once and then retain the last value assigned to them?
-
August 20th, 2007, 02:43 AM
#3
Re: Dumb question about assignment...
IMHO, I think that a gets reset to 6 on each iteration in the second snippet. I have tried experimenting on this before and found out that the only way to let "a" be initialized only once inside a loop is to declare it as "static". Well, I might be wrong.
/** The only stupid question is the one you never ask. */
-
August 20th, 2007, 03:02 AM
#4
Re: Dumb question about assignment...
Okay - so if I did something like this:-
Code:
while (whatever)
{
static int a = 6;
int b;
b = a;
// rest of loop
a = 9;
}
'b' would be 6 for the first iteration of the loop and 9 for the second iteration?
"A problem well stated is a problem half solved.” - Charles F. Kettering
-
August 20th, 2007, 03:18 AM
#5
Re: Dumb question about assignment...
yes it would, a would not be initialised to 6 after the first iteration so it would be 9 for subsequent iterations.
Of course a would remain 9 if this function were entered again, and you would have to do the above for a good reason. (A typical reason might be loading some value that does not change but is a heavy operation to load, i.e. using a caching technique with lazy evaluation).
-
August 20th, 2007, 03:21 AM
#6
Re: Dumb question about assignment...
Originally Posted by John E
Okay - so if I did something like this:-
Code:
while (whatever)
{
static int a = 6;
int b;
b = a;
// rest of loop
a = 9;
}
'b' would be 6 for the first iteration of the loop and 9 for the second iteration?
Yep, since "a" would not be initialized again.
EDIT: I noticed that I'm always 3 minutes too late too reply
/** The only stupid question is the one you never ask. */
-
August 20th, 2007, 03:43 AM
#7
Re: Dumb question about assignment...
Originally Posted by Sarevok
EDIT: I noticed that I'm always 3 minutes too late too reply
Thanks guys..!
"A problem well stated is a problem half solved.” - Charles F. Kettering
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|