Go Down

Topic: redefined variable issues arduino IDE (Read 999 times) previous topic - next topic

S_Flex

I have found the arduino IDE is not finding all redefined variables in a sketch. This can lead to major programing issues that the compiler should catch!


This is my example code and the IDE versions tested and failed are 1.0.3 and 1.5.2

Code: [Select]

//Compiler dose not find all redefinition error's in the sketch

// define i in this scope
int i = 0;

// Uncomment 'int i' and we will get redefinition error
//int i = 1;


void setup() {
// we should get redefinition error here
int i = 1;
}

void loop() {
// we should get redefinition error
int i = 1;
}


johnwasser

It's only a redefinition if you do it in the same scope.  Each function is a different scope and can have local variables with the same name as a variable in Global scope.

You just have to be careful about your use of variables.

And don't give global variables stupid names like 'i'. :)
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

S_Flex


It's only a redefinition if you do it in the same scope.  Each function is a different scope and can have local variables with the same name as a variable in Global scope.

You just have to be careful about your use of variables.

And don't give global variables stupid names like 'i'. :)


All the other redefined variables are in the same scope as the first defined "i".
Yes each function has its own scope but those variables defined in that scope would be called local variables, when you define a variable in a global scope like the first defined "i" the hole sketch is its scope and all the functions in that scope can use "i" and should error if "i" is redefined in those functions and block's.

If I can use "i" in the function before its redefined and after its redefined, then "i" is within that scope.

This link doesn't explain the issue but can give you an idea of what global and local scope variables are.
http://www.tutorialspoint.com/cplusplus/cpp_variable_scope.htm

johnwasser

So you missed this part?
Quote

A program can have same name for local and global variables but value of local variable inside a function will take preference. For example:
Code: [Select]

#include <iostream>
using namespace std;

// Global variable declaration:
int g = 20;

int main ()
{
  // Local variable declaration:
  int g = 10;

  cout << g;

  return 0;
}

When the above code is compiled and executed, it produces following result:

10



You might be further amused to know that each block {} is a separate scope:
Code: [Select]

int var = 1;
void setup() {
  Serial.println(var);
  int var = 20;
  Serial.println(var);
  {
    Serial.println(var);
    int var = 300;
    Serial.println(var);
  }
  Serial.println(var);
}


Should display:

20
20
300
300
20
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

S_Flex


So you missed this part?
Quote

A program can have same name for local and global variables but value of local variable inside a function will take preference. For example:
Code: [Select]

#include <iostream>
using namespace std;

// Global variable declaration:
int g = 20;

int main ()
{
  // Local variable declaration:
  int g = 10;

  cout << g;

  return 0;
}

When the above code is compiled and executed, it produces following result:

10



You might be further amused to know that each block {} is a separate scope:
Code: [Select]

int var = 1;
void setup() {
  Serial.println(var);
  int var = 20;
  Serial.println(var);
  {
    Serial.println(var);
    int var = 300;
    Serial.println(var);
  }
  Serial.println(var);
}


Should display:

20
20
300
300
20


No I didn't miss that part. A global variable has a much wider scope then a local variable and if the compiler does not see it as an error then whats the point in having a "redefinition error" if the compiler doesn't find all the errors in the variables scope.

It's giving priority over one variable scope then the other!

johnwasser


No I didn't miss that part. A global variable has a much wider scope then a local variable and if the compiler does not see it as an error then whats the point in having a "redefinition error" if the compiler doesn't find all the errors in the variables scope.

It's giving priority over one variable scope then the other!


Yes.  To dis-ambiguate the names there must be an order of precedence.  The most local scope always wins and it doesn't matter where in the scope the variable is defined.

That's the rule.  Just get used to it.
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

James C4S


A global variable has a much wider scope then a local variable

Yes, it is a wider scope, but it isn't special.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Nick Gammon


A global variable has a much wider scope then a local variable and if the compiler does not see it as an error then whats the point in having a "redefinition error" if the compiler doesn't find all the errors in the variables scope.

It's giving priority over one variable scope then the other!


That's how local declarations work. It's good in a number of ways. For example:

Code: [Select]
int i = 42;

void foo ()
  {
  int i;
  for (i = 0; i < 10; i++)
    Serial.println (i);
  }


In this example the programmer can use a local variable "i" without having to worry if s/he is corrupting another variable, of the same name, somewhere else. It's a feature.


I have found the arduino IDE is not finding all redefined variables in a sketch. This can lead to major programing issues that the compiler should catch!


It's not the Arduino IDE. It's the g++ compiler. The compiler shouldn't catch them because it isn't a bug. It's no good redefining in your head what C++ should do, and then posting a bug report when it doesn't do it.

Note, under some warning levels (which we don't appear to have turned on) you would get a warning about a "shadowed" variable "i". The warning is because, although it isn't illegal, possibly you did not intend to redefine "i".

S_Flex


Note, under some warning levels (which we don't appear to have turned on) you would get a warning about a "shadowed" variable "i". The warning is because, although it isn't illegal, possibly you did not intend to redefine "i".


Is there a way to turn on all warning's from my side?

I would like the strictest warning levels.

UKHeliBob

I am not sure if the output will include the ones you want but have a look at File/Preferences in the IDE and check the verbose output checkboxes.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

spatula

#10
Feb 11, 2013, 03:27 pm Last Edit: Feb 11, 2013, 03:33 pm by spatula Reason: 1
Look for files named Makefile under your arduino root directory. They all belong to the avr compiler, the one in the TemplateProject directory may suit your needs.

[added] what you are looking for is a way to add -Wshadow to your compiler options.

Go Up