Unusual compiler behaviour.

Started by
11 comments, last by okonomiyaki 19 years, 2 months ago
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.
Advertisement
No crash for me. Have you tried running it under the debugger?
-Mike
Running it in the debugger whould cause both values to be initialized to a default value by the debugger.

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

No Probs here.
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?
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.




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.

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.exe
Module: D:\Projects\GUITest\Debug\guitest.exe
File: d:\projects\guitest\guitest.cpp
Line: 5

Run-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.
-Mike
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.
Quote:Original post by ZedFx
Regarding 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]
My stuff.Shameless promotion: FreePop: The GPL god-sim.

This topic is closed to new replies.

Advertisement