CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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?

  3. #3
    Join Date
    Aug 2005
    Location
    The Matrix
    Posts
    159

    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. */

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    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

  5. #5
    Join Date
    Oct 2000
    Location
    London, England
    Posts
    4,773

    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).

  6. #6
    Join Date
    Aug 2005
    Location
    The Matrix
    Posts
    159

    Re: Dumb question about assignment...

    Quote 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. */

  7. #7
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Dumb question about assignment...

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured