How to let my compiler more stupid (wrong index)?

  • A+

I have encounter a horrible situation. I usually use visual code to edit my code, also compile and execute in it(F5). But I found vscode is too smart or ignore some warning message for me. And output the right answer, which also work fine in Ideone. But in window cmd or dev C++ my code can't output anything, just return a big number.

And I find some situation will occur the thing I mention above.

The code like this

for (i = 0; i < g.size(); i++) {     int source;     int dest;     int minWeight = 999;      for (j = 0; i < g[j].size(); j++)     {         // no edge, come to next condition         if (!g[i][j])             continue;         if (g[i][j] < minWeight)         {             source = i;             dest = j;             minWeight = g[i][j];         }     }     if         updateGroup(index, index[source], index[dest]);     else         updateGroup(index, index[source], index[dest]); } 

You may found that the second for loops have wrong condition statement, it should change j = 0; i < g[j].size(); j++ to j = 0; j < g[i].size(); j++

So I wonder to know

  1. Are there any way let my vscode more strict?

  2. Why it still can output right answer in vscode and ideone?

  3. How to avoid or be easier to found where my code wrong when this kind of no message error?

Really hope someone can help me, and appreciate all of your suggestion!!


There is no way for the compiler or computer to read your mind and guess what you meant to write instead of what you really did mean.

Even when this mistake results in a bug, it cannot know that you did not intend to write this, or that you meant to write some other specific thing instead.

Even when this bug results in your program having undefined behaviour, it is not possible to detect many cases of undefined behaviour, and it is not worthwhile for a compiler author to attempt to write code to do this, because it's too hard and not useful enough. Even if they did, the compiler could still not guess what you meant instead.

Remember, loops like this don't have to check or increment the same variable that you declared in the preamble; that's just a common pattern (now superseded by the safer ranged-for statement). There's nothing inherently wrong with having a loop that increments i but checks j.

Ultimately, the solution to this problem is to write tests for your code, which is why many organisations have dedicated Quality Assurance teams to search for bugs, and why you should already be testing your code before committing it to your project.

Remember to concentrate and pay close attention and read your code, and eventually such typos will become less common in your work. Of course once in a while you will write a bug, and your tests will catch it. Sometimes your tests won't catch it, which is when your customers will eventually notice it and raise a complaint. Then, you release a new version that fixes the bug.

This is all totally normal software development practice. That's what makes it fun! 😊


:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: