Jump to content
  • Advertisement
Sign in to follow this  
Krylloan

Unusual compiler behaviour.

This topic is 4877 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!