2

Please consider the following codes.

code 1

using namespace std;
int main ()
{
    for (int ii = 0; ii < 2; ii++)
    {
        int a = 0;
    }
    return 0;
}

code 2

using namespace std;
int main ()
{
    int a = 0;
    int a = 0;
    return 0;
}

It seems that the two codes are equivalent. But, when I run the second one, the following error is reported:

[Error] redeclaration of 'int a'
[Error] 'int a' previously declared here

Why this happens?

P. Mir
  • 23
  • 1
  • 4

2 Answers2

2

Variables can be redeclared in inner scopes. This is called shadowing. However, what you're trying to do is redeclare the variable in the same scope.

for (...) {
  int a = 0;
}

This creates a variable a several times. At the end of each iteration, the variable is deallocated and a new one is created.

int a = 0;
for (...) {
  int a = 1;
}

This is similar. The second a is a new variable that only exists inside of the loop. Once the loop is over, the original a is back and still has value 0.

int a = 0;
int a = 1;

Here, we are trying to create two variables in the same scope with the same name. The C++ standard explicitly disallows this. There's no mechanical reason that this must be disallowed, but since there are almost no reasons you would want to intentionally do this, the standard forbids it, as it is much more likely that you intended to make an assignment. If you really want two variables with the same name, you can use an explicit block.

int a = 0;
{
  int a = 1;
}

But there's really no reason to do this.

Silvio Mayolo
  • 62,821
  • 6
  • 74
  • 116
  • Your leading statement is most confusing given the context. – juanchopanza Jan 14 '18 at 20:05
  • 'At the end of each iteration, the variable is deallocated', this means that the memory released at the end of each iteration? (I have to declare a variable inside a for loop but because of extremely too many iterations, I doubt the memory is sufficient) – P. Mir Jun 19 '18 at 04:22
  • Not exactly. The variable is deallocated, which means the destructor is called (if it's a class type). A decent compiler will allocate the memory once and then just fill it with new data at each iteration. – Silvio Mayolo Jun 19 '18 at 04:51
  • So, after the first iteration, no new memory is occupied for declaration? – P. Mir Jun 19 '18 at 06:24
  • Probably not. But you're making variables in automatic storage, which means the compiler has a good amount of control over how that exactly works. So the technical answer is: I don't know and the standard doesn't say. – Silvio Mayolo Jun 19 '18 at 14:42
0

They're not the same.

using namespace std;
int main ()
{
    for (int ii = 0; ii < 2; ii++)
    {
        int a = 0;
    }
    return 0;
}

Here you declare and initialize a variable a once. It is then destroyed and recreated as the loop body is executed again.

using namespace std;
int main ()
{
    int a = 0;
    int a = 0;
    return 0;
}

Here you declare and initialize a variable a twice in the same scope, which isn't allowed.

Hatted Rooster
  • 35,759
  • 6
  • 62
  • 122