Sign in to follow this  

Unusual compiler behaviour.

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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 initialized
Linking...

Build Time 0:16
Build 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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Quote:
Original post by Krylloan
A very wierd situation.


This program compiles under VC++7, but crashes on execution.

*** Source Snippet Removed ***

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.


It's either a compiler bug, or a bug in the OS or drivers, or some other program is misbehaving really badly (aka, maybe some nasty spyware, virus, or piece of adware). edit: or possibly it's a buggy library, the issue of which only occurs when that assignment is done.

That's a simple enough program my money's on the compiler or library. If it dosn't go away when you assign those variables on init (with the b=a), I'd go on to specifically state it's probably a bug in the compiler's implementation of the debugging information. If it dosn't go away when you assign those variables on init, WITHOUT the b=a, I'd go on to say it's probably one of the libraries.

Share this post


Link to post
Share on other sites
When built in debug mode, whether or not run through the debugger it should (and does on mine) give a run-time check error about the uninitialized variable thing. When built in release mode, I think it's specified that this will cause undefined behavior. For me nothing happens, it may crause sometimes, etc., it just depends on the state of the system.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this