# Unusual compiler behaviour.

This topic is 5056 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

A very wierd situation. This program compiles under VC++7, but crashes on execution.
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
int a, b;
b = a;

return 0;
}


All the program does is move an uninitialised variable from a to b. AFAIK this is, while bad practice, legal and easily acheivable (?spellig). Note, the program could be optimised to just do nothing but return 0. Is this a compiler bug? Can anyone explain this? Compiler: MSVC++ 7. (Default debug build). OS: Windows XP SP1. Architecture: Standard x86.

##### Share on other sites
No crash for me. Have you tried running it under the debugger?

##### Share on other sites
Running it in the debugger whould cause both values to be initialized to a default value by the debugger.

CHeers
Chris

##### Share on other sites
Quote:
 ------ Build started: Project: test, Configuration: Debug Win32 ------Compiling...test.cpp\test.cpp(5) : warning C4700: local variable 'a' used without having been initializedLinking...Build Time 0:16Build log was saved at "file://\Debug\BuildLog.htm"test - 0 error(s), 1 warning(s)---------------------- Done ---------------------- Build: 1 succeeded, 0 failed, 0 skipped

No Probs here.

##### Share on other sites
How do you know the program crashed at runtime? All it should do is start a process for a split second and close - there shouldn't even be any windows to open. If you're compiling with the console option on, you would see a console flash on the screen for a second but that would be it. Are you getting some kind of error message?

##### Share on other sites
To chollida1 / Anon Mike: Yep, running it under the debugger makes it proceed without a crash.

To ZedFx: It compiles fine for me also, with the warning. It's when I run it that I get the crash.

I actually found this when I was running a simple test program on data types, where some of the types didn't exist so my switch/case did not do the test for them and therefore didn't write the data to the variables that were printed afterwards. I simplified the program right down to the code I posted and got the same problem.

Odd.

##### Share on other sites
To TheBluMage:

It's compiled under standard Win32 App, not console.

I get the typical windows error:
"TypesTest has encountered a serious error and needs to close, blah blah etc".
When I run the generated executable. If I remove the offending "b = a;" then then program completes without error.

##### Share on other sites
Ok I actually built it inside VC this time instead of using the command line. Is this what you mean by "crash"?:

Quote:
 ---------------------------Microsoft Visual C++ Debug Library---------------------------Debug Error!Program: D:\Projects\GUITest\Debug\guitest.exeModule: D:\Projects\GUITest\Debug\guitest.exeFile: d:\projects\guitest\guitest.cppLine: 5Run-Time Check Failure #3 - The variable 'a' is being used without being defined.(Press Retry to debug the application)---------------------------Abort Retry Ignore ---------------------------

If so that's not a crash, it's a debugging feature.

If that's not the problem what *exactly* are you seeing?

**edit**

Ok just read the above message. I suspect you are linking with the wrong c runtime dlls or something and the above debug spew is going off into never-never land.

##### Share on other sites
Regarding the message written by Anon Mike

#include <windows.h>int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {    int a, b = 0;    b = a;        return 0;}

should do it, I always initialise my variables, because this runtime error is a pain.

##### Share on other sites
Quote:
 Original post by ZedFxRegarding the message written by Anon Mike*** Source Snippet Removed ***should do it, I always initialise my variables, because this runtime error is a pain.

In that code 'a' remains uninitialised. [razz]

1. 1
2. 2
Rutin
18
3. 3
khawk
15
4. 4
A4L
14
5. 5

• 10
• 13
• 26
• 10
• 11
• ### Forum Statistics

• Total Topics
633745
• Total Posts
3013667
×