0

My understanding is that variables that are created inside a while or for loop disposed/deleted from memory after the loop is done executing (see reference 1). But why does the following code show the variable is using the value from PREVIOUS loop? Is that is because stringstream variable have some special power?

The following is my C++ code snippet:

#include <iostream>
#include <sstream>
#include <string>
using namespace std;

int main(){
    string str="1A2";
    stringstream ss(str);
    int intVar;
    while(ss.good()){
        char charVar;
        ss >> intVar;
        ss>> charVar;
        cout<<"intVar="<<intVar << " charVar="<<charVar<<endl;
    }   
}

The following 2 lines are actual output:

intVar=1 charVar=A
intVar=2 charVar=A

Why it is not the following 2 lines?

intVar=1 charVar=A
intVar=2 charVar=
BSMP
  • 3,862
  • 8
  • 31
  • 41
Bruce
  • 1
  • 1
  • 2
    Check what the `operator>>` function *returns*. If you do `if (ss >> charVar) { ... } else { ... }` then the `else` branch would be taken. – Some programmer dude Oct 18 '18 at 06:58
  • Also please read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Doing `while (ss.good())` is just as bad. – Some programmer dude Oct 18 '18 at 06:59
  • 2
    Always initialise your variables. – molbdnilo Oct 18 '18 at 07:02
  • @Some programmer dude : Your suggestion does fix the strange behavior. I will study your suggestions and update for meaningful results. Many thanks. – Bruce Oct 18 '18 at 07:14
  • @molbdnilo : Thanks for this suggestion. But simply provide an initial value do not help. Plus, it is not always possible to give meaningful initial value. – Bruce Oct 18 '18 at 07:19
  • 1
    It doesn't have to be a meaningful value, just something you can *expect*. Otherwise the behavior can be undefined, and you'll just end up scratching your head wondering "WTF!?!". – StoryTeller - Unslander Monica Oct 18 '18 at 07:26

2 Answers2

7

First of all there's a difference between scope and life-time.

The life-time of the variable charVar ends each iteration, but the variable is still in scope inside the loop.

Also you need to know that the value of uninitialized "local" variables is indeterminate and seemingly random. Reading uninitialized variables (which happen as part of the output) leads to undefined behavior, which is what you're experiencing here.

What happens practically is that the memory used for the variable charVar doesn't change, the compiler reuses the same location each iteration. And since you don't modify that memory it will simply retain the last value stored in that memory.

Some programmer dude
  • 363,249
  • 31
  • 351
  • 550
  • Thanks. Here is my two confirmation : 1) After research, I plan to use: `while(!ss.fail())` to replace `while(ss.good())`; 2) When I print out the address of charVar, I can see the memory address does not change: `cout< (&charVar)<< endl;` – Bruce Oct 18 '18 at 15:48
0

This because the loop has not ended before the value was printed and as their was no value to enter during the last iteration in charvar it would retain its value from the second last time. The scope of the variable is only in the loop put it doesn't get destroyed until the very last iteration of the loop. Hope I made sense