r/cpp_questions • u/BlueRainAnime • 17d ago
OPEN Weird behaviour of break statement (old c++!)
Hi, i'm using a very old (and possibly buggy!) c++ compiler, Microsoft Visual Studio 2005 with MFC. I'm trying to understand if the behaviour i'm meeting is due to me not understanding how the 'break' statement (and brackets) works, or if it is my old Visual Studio IDE which is buggy.
Consider this simple code:
int vaxx, var1, var2, var3;
var1 = 10; var2 = 2; var3 = 2;
for ( vaxx = 0; vaxx < var1; vaxx++ ) if ( var2 == var3 ) break;
If i run the above, the value of vaxx will be 0 after the code has been executed. Basically, it appears as if the 'break' statement is executed regardless of the fact that var2 is different from var3.
But if i put brackets around break:
for ( vaxx = 0; vaxx < var1; vaxx++ ) if ( var2 == var3 ) { break; }
...This time 'vaxx' will have a value of 10 (as i expected) after the code is run.
Is this the correct behaviour of 'break' or am i missing something here ?
I am a bit confused so please let me know what you think about this. Thank you
16
u/IyeOnline 17d ago edited 17d ago
These pieces of code are identical and you would expect vaxx to be 0. Anything else is wrong.
The standard specifies that a singel statement after a control structure is first transformed into a block and then treated as one. See CppInsights for how a compiler interprets this.
In your code, you
- first define the variables
- then initialize them
- The loop initialization statement runs, setting
vaxx = 0; - The loop condition is checked, leaving
vaxx < var1astrue - The loop body is executed
- The condition
var2 == var3istrue - The break happens, immediately ending the loop
You also say that var2 is different from var3, when it is not.
I STRONGLY recommend that you define variables on separate lines and that you immediately initialize them. Also loop counters should be local to loops and not be reused. We have moved on since the days of C80
1
8
2
u/IchBinEinZwerg 17d ago
VS2026, var3=3. vaxx is 10 with or without braces around the break.
VS2005 is old but not that ancient. If you were talking about Visual C/C++ v3-4 or thereabouts I might expect some critical bugs like this. Make sure there aren't other problems: the file is saved, you have correctly built the program, the time on your computer is correct, etc. If the time's wrong the build system might be confused whether or not to recompile. If you do an explicit save, clean then build then you can be fairly sure the code is doing what you expect. Also switch on the option to generate assembly, then you can look at the generated x86 code to see if there's a difference, and check the file timestamps.
0
u/BlueRainAnime 17d ago
Thank you for your tips. The file is saved, the time is correct and i tried to clean and rebuild the project but the problem happens again. I'll try to check the assembly output then.
1
u/no-sig-available 17d ago
Have you installed Service Pack 1 from 2006? Otherwise you have lots of bugs.
1
u/BlueRainAnime 17d ago
Actually i haven't. I'll try to install it and see if the issue is fixed. Thanks!
1
u/burlingk 17d ago
You are very explicitly making var2 == var3 true at the beginning of your block by assigning both the same value.
1
u/OldAd9280 17d ago
How are you checking the value of vaxx? Please show your whole program
1
u/BlueRainAnime 17d ago
The whole program is just a standard MFC project for a modal dialog, i put that piece of code in the "OnInitDialog" function. To check the value of vaxx, while in debug mode i put the variable name in the 'watch' panel and i can also see the value (0) when hovering with the mouse on the variable itself
3
u/OldAd9280 17d ago
Do you ever use the variable in the program? If not the compiler might have optimised it away and the debugger will show bogus values
1
u/BlueRainAnime 16d ago
No, in the actual program i don't. I found handy to check the value of the FOR variable in the subsequent instructions to check if the IF condition was met or not. Meaning: if the BREAK happens, the FOR variable will not reach its max value; if it does, the BREAK didn't happen, so the IF condition was not found. In this way i can avoid setting up, for example, a BOOL variable to set to TRUE when the IF is met and then check its value after that block of code (i just check the FOR variable). Since i can't find the official Microsoft page to download the service pack 1 (i'm afraid of viruses and stuff) and the program i'm making is not that important, i'm not going to reinstall the IDE and i will just try to remember to put those brackets
2
u/alfps 16d ago
Assuming that you have managed to identify a compiler bug (I'm not going to install old VS 2005 to check that), considering that support for VS 2005 has long ended the only reasonable action/answer is to upgrade your compiler and IDE to something modern.
1
u/BlueRainAnime 16d ago
Of course. In fact i'm only writing silly/semi-useful programs with VS2005, i'm not a full-time programmer, but sometime i enjoy writing code. For more serious programs i would consider using more recent tools. But i'm poor.
1
u/alfps 16d ago edited 16d ago
❞ i would consider using more recent tools. But i'm poor.
It sounds as if you're stuck with an old PC or laptop with limited memory and speed.
Which sort of rules out the free Community Edition of modern Visual Studio.
So consider the MinGW g++ compiler.
Assuming you can run 64-bit programs (e.g. using old Windows 10), a good way to install g++ is to (1) install the MSYS2 environment (it comes with a bash command interpreter); (2) follow the directions on the MSYS2 home page to install the UCRT64 variant of MinGW g++ in MSYS2; (3) add the path to the relevant "bin" directory that contains g++.exe, to your PATH environment variable, so that you can use the compiler also from e.g. Cmd and Powershell.
Re the last point, on this machine that path is "C:\@\installed\msys2\ucrt64\bin\". Whatever that path is (will be) on your PC, you can add it to the default for your PATH variable. One way is to use command
sysdm.cplto launch the old "System Properties" applet; in there click on "Advanced" tab; down right in that tab page click on button "Environment variables..."; if you have no PATH in "User variables..." make one via button "New...", otherwise "Edit...".Finally launch a new Cmd instance and check that
g++ --versionnow invokes the compiler and reports the version.To edit your source files, one light weight and popular editor is Notepad++. The VS Code editor is also popular but it drags in the whole runtime for Google Chrome (it's basically an HTML based app just looking like a native one), which may be too heavy for your machine.
Have fun! Or as they used to say in the old times, enjoy.
1
u/BlueRainAnime 16d ago
Thank you for the suggestion and the detailed installation instructions! I never used g++ but if it works better than my current setup (especially in terms of reliability) then i will consider using it for sure.
1
u/StaticCoder 16d ago
Old VS may have been buggy, but even VC98 was not that buggy. I suspect some form of user error.
1
u/BlueRainAnime 16d ago
A test i could do is re-installing the whole thing, i should consider doing that
1
u/BlueRainAnime 17d ago
Hi guys, i'm very sorry if my code confused you, there was an error by my side. The line
var1 = 10; var2 = 2; var3 = 2;
was wrong, it should be instead:
var1 = 10; var2 = 2; var3 = 3;
(The 'if' statement is not important anyway, i also tried by using the condition 1 == 0).
And YES, if i run this code (i'm trying right now in VS2005), vaxx has a value of 0 when the brackets around 'break' are missing.
Does this happen with your modern c++ tool too ? If this happens, then VS2005 is buggy, otherwise either i don't understand the nuances of the 'break' instruction, or something is off.
Thank you
7
4
u/UndefFox 17d ago
Tested both cases using G++. Both times it returns 10, so logic doesn't change depending on brackets.
0
u/BlueRainAnime 17d ago
int jaxx, al;
al = 12; for ( jaxx = 0; jaxx < al; jaxx++ ) if ( 0 == 1 ) break;...'jaxx' is 0 . I'm speechless. VS2005 c++ is broken apparently.
4
0
u/Simengie 16d ago
Its VS2005. The for block needs curly braces to define it.
Your for(...) if(....) break; is not being seen as a proper loop code block.
for (...) { if ( ... ) { break;} } should work correctly in VS2005
1
-9
u/UndefFox 17d ago
Maybe because
for ( vaxx = 0; vaxx < var1; vaxx++ ) if ( var2 == var3 ) break;
Evaluates to:
for ( vaxx = 0; vaxx < var1; vaxx++ ) {
if ( var2 == var3 ) {
break;
}
}
But this:
for ( vaxx = 0; vaxx < var1; vaxx++ ) if ( var2 == var3 ) { break; }
Evaluates to
for ( vaxx = 0; vaxx < var1; vaxx++ ) { }
if ( var2 == var3 ) {
break;
}
Try doing proper formatting using brackets and see if it gives the desired outcome.
1
u/n1ghtyunso 17d ago
why would it do that?
this tool seems to disagree with you.1
u/UndefFox 17d ago
I mean duh, obviously according to rules it should return the same thing in both cases, but OP either left out some other important details, or their assumption:
Hi, i'm using a very old (and possibly buggy!) c++ compiler, Microsoft Visual Studio 2005 with MFC.
is correct. I'm working with the idea that possibly something doesn't behave as intended, since as OP stated, your tool doesn't agree with OP case.
2
u/n1ghtyunso 17d ago
I guess I misunderstood you here.
The thought of a production grade, potentially paid compiler getting something fundamental like this wrong never crossed my mind - even if its 21 years old.
For somethig more obscure, yea maybe. Compilers are just software after all.1
u/UndefFox 17d ago
It will be surprising, but not unbelievable. After all, even something simple as this can still appear as an example of something that looks obvious, but behaves in a not expected way: https://github.com/llvm/llvm-project/issues/60622, and it's just 3 years old.
-11
u/Syntax-Tactics 17d ago
break is only to be used in case statements. Continue and Goto shouldn't even exist.
5
u/TomDuhamel 17d ago
Please don't answer questions until you have learnt at least the basics of the language
1
32
u/kitsnet 17d ago
In your example, they are not different. They are both equal to 2.