Pages: [1]   Go Down
Author Topic: redefined variable issues arduino IDE  (Read 778 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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'. smiley
Logged

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

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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'. smiley

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
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8669
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6108
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A global variable has a much wider scope then a local variable
Yes, it is a wider scope, but it isn't special.
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 1
Posts: 47
I'm glad I bought an Arduino Leonardo
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

East Anglia (UK)
Online Online
Faraday Member
**
Karma: 109
Posts: 4038
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Rome, Italy
Offline Offline
Sr. Member
****
Karma: 20
Posts: 442
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: February 11, 2013, 09:33:24 am by spatula » Logged

Pages: [1]   Go Up
Jump to: