redefined variable issues arduino IDE

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

//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;
}

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'. :slight_smile:

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'. :slight_smile:

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.

So you missed this part?

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

#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:

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

johnwasser:
So you missed this part?

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

#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:

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!

S_Flex:
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.

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

Yes, it is a wider scope, but it isn't special.

S_Flex:
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:

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.

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!

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

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

I would like the strictest warning levels.

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.

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.