Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя hzyfr

Автор hzyfr, 11 лет назад, По-английски

As it knowns to all c++ coders, re-defining loop variable is a undefined behavior that leads to a unexpected result of your program, or sometimes Runtime error.

This often happens when you want to write a long loop statement:


for(int i = 0; i <= n; ++i) { //do something very complicated for(int i = 0; i <= m; ++i) { ...... } }

Usually after you use the outer loop variable i to do something, the variable i become useless, but you want to do other works inside the loop , and you forget that you have defined i outside.

Then when you run your program you will find something strange happens. Maybe the program never stops or the program enters the first loop and only run one time then stops. I think most of the coders have such experience. Also this bug is not easy to discover(though fixing it is really simple work).

However this error can easily be detected by compilers, if it check every definitions inside a loop that defines temporary loop variable(s). I would say that if the compiler does that work and give us a warning then it will not be a disgusted, hard-to-find bug anymore. So I really hope the compilers will support such a feature in the near future.

  • Проголосовать: нравится
  • +14
  • Проголосовать: не нравится

»
11 лет назад, # |
  Проголосовать: нравится +16 Проголосовать: не нравится

em, AFAIK it's perfectly valid and visible variable is just one in inner loop

  • »
    »
    11 лет назад, # ^ |
    Rev. 2   Проголосовать: нравится +11 Проголосовать: не нравится

    Yes, I think that this is not undefined behavior. Consequences of this bug (such as array index overflow) can have undefined behavior, but not shadowing itself.

    Moreover, there are situations, when shadowing is very useful. But I personally prefer to avoid it everywhere.

»
11 лет назад, # |
Rev. 2   Проголосовать: нравится +16 Проголосовать: не нравится
➜  test git:(master) ✗ cat a.cpp
#include <iostream>
#include <cmath>
typedef long long li;
using namespace std;

int main() {
	cout.precision(20);
	for(int i = 0; i < 5; ++i){
		for(int i = 0; i < 2; ++i) {
			cout << i;
		}
	}
}
➜  test git:(master) ✗ g++ a.cpp -Wshadow
a.cpp: In function ‘int main()’:
a.cpp:9:11: warning: declaration of ‘i’ shadows a previous local [-Wshadow]
   for(int i = 0; i < 2; ++i) {
           ^
a.cpp:8:10: warning: shadowed declaration is here [-Wshadow]
  for(int i = 0; i < 5; ++i){
          ^
➜  test git:(master) ✗ 

-Werror may be also interesting to you

  • »
    »
    11 лет назад, # ^ |
      Проголосовать: нравится +8 Проголосовать: не нравится

    Thank you ! Maybe I don't set the compiler on my computer. I will add -Wall command while compiling.