Simple program gone wrong... Problem with devkitpro compiler

Roroy
Posts: 17
Joined: Sat Sep 22, 2012 7:00 am

Re: Simple program gone wrong... Problem with devkitpro comp

Post by Roroy » Wed Sep 26, 2012 3:03 pm

I know about local and global variables.

Maybe I didn't clarify my question...
As you guys said, yes, any variable created inside { and } is used only inside { and } and then destroyed as it gets to }.


Pretend that there is a variable (with a value of 5) created outside that scope that has been set to one.
Then that variable is changed to another value (now with a value of 7) inside that scope.
Then after the scope, the variable is called upon in the program.
That variable gives the value 7 to the program.

My question is, why is the value 7 given back (instead of the value 5) if everything inside the scope is destroyed?

elhobbs
Posts: 358
Joined: Thu Jul 02, 2009 1:19 pm

Re: Simple program gone wrong... Problem with devkitpro comp

Post by elhobbs » Wed Sep 26, 2012 3:50 pm

your code example does not reflect the question you are asking, so it is a little hard to answer. in the question you asked the variable should change to 7 since you did not indicate that a new local variable was created. In the code example you provided you did create a new local variable. That being said you should use descriptive variable names and avoid scoping confusion issues like this - use unique variable names in the same function and do not create local variables with the same name as a global variable.

examples:
different variable same name

Code: Select all

int a = 5;
if(1) {
   int a = 7; //this is a new "a"
}
//at this point a is still 5
same variable referenced throughout

Code: Select all

int a = 5;

if(1) {
   a = 7; //this is the same "a" - notice the lack of a type specification
}
//at this point a is now 7
and to be clear; I recommended that you read up on scope. I did not say you did not know what local or global variables are. however, you have demonstrated that you do not understand the scope of local and global variables - hence the recommendation.

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Simple program gone wrong... Problem with devkitpro comp

Post by mtheall » Wed Sep 26, 2012 3:55 pm

As you guys said, yes, any variable created inside { and } is used only inside { and } and then destroyed as it gets to }.
That's exactly right.
Pretend that there is a variable (with a value of 5) created outside that scope that has been set to one.
Then that variable is changed to another value (now with a value of 7) inside that scope.
Then after the scope, the variable is called upon in the program.
That variable gives the value 7 to the program.
You failed to see one thing. You did not create a new variable inside the new scope! All you did was change the value of a variable which already exists. Since it wasn't created in the new scope, it does not disappear when the scope disappears.

Code: Select all

#include <iostream>
using namespace std;

int main() {
  int test = 1;
  int a = 0; <-----
                  |
  if(test == 1) { |
    a = 7; -------- refers to this variable, so now replaces 0 with 7
  }               |
          ---------
          |
  cout << a << endl; prints 7
  return 0;
}

Code: Select all

#include <iostream>
using namespace std;

int main() {
  int test = 1;

  if(test == 1) {
    int a = 7;
  }     |
        --- these a's are not the same! in fact, the second one doesn't even exist. you will get a compile failure
          |
  cout << a << endl;
  return 0;
}

Roroy
Posts: 17
Joined: Sat Sep 22, 2012 7:00 am

Re: Simple program gone wrong... Problem with devkitpro comp

Post by Roroy » Thu Sep 27, 2012 8:54 am

mtheall wrote:
You failed to see one thing. You did not create a new variable inside the new scope! All you did was change the value of a variable which already exists. Since it wasn't created in the new scope, it does not disappear when the scope disappears.
Ohhh...I see now.

So only variables created inside scopes get destroyed?
Variables created outside the scope yet their value changed inside the scope, that value does not get destroyed?

mtheall
Posts: 210
Joined: Thu Feb 03, 2011 10:47 pm

Re: Simple program gone wrong... Problem with devkitpro comp

Post by mtheall » Thu Sep 27, 2012 3:09 pm

A value never gets destroyed. A variable is storage for a value.

Post Reply

Who is online

Users browsing this forum: No registered users and 26 guests