Dear all,
I have written a code possibly 10k lines long by now, there are no random functions used throughout the process.
The funny thing - while I kept repeating to run the same program, sometimes the 10k lines long program will generate an answer of 31 and sometimes 30. How is this possible? No random functions were involved! Does that mean the computer was wrong?
Bosco
No. It means the calculation is indeterminate, or it means that the state of the computer matters (and presumably, it shouldn't), or you've got a bug. For example, maybe there's a bug in which you read a value after it has been deleted. Sometimes the value is still there, and it works fine, and sometimes it has been changed.
Thanks Moschops,
I think I can offer a supplementary information here which I have just found out 30 sec ago.
If I just kept on randomly delete some words in the long code, add them back then compile it again for 2 or 3 times. The codes are still identical afterwards. However, after doing such thing 2 or 3 times the program gives more or less correct result afterwards, i.e. change from 30/31 to 24/25.
Is this explainable then?
Thanks again.
Regards,
Bosco
I have a const int CONSTANT=1000 in the header file; this value will result in an answer of 8 after the 10k lines of code.
Then I change the CONSTANT in the header file to 100, compile it, then run the program, it still gives 8 - which is wrong.
Then i go to a random cpp page within my project (I have 1 main page then 10 implementation pages), randomly delete a colon or so then add the colon back, then compile the identical code again.
The vast, vast majority of the time that people think about pointing the finger at the computer or the compiler, they're wrong. That's just how it is. With 10k lines of code, the odds are that the code (and/or setup) is in some way hideously indeterminate. I've not seen it, so there's not quite a guarantee, but if recompiling the same code appears to change the output, then I'm pretty sure that's going to be the case :P
Please don't take it personally; all I can do is play the odds here, and the odds are definitely tilted away from it being a compiler bug or hardware insanity. The vast, vast majority of the time I (and other people) see "crazy" behaviour like this, it turns out not to be the compiler or the hardware.
You mean that's going to be the case the compiler is wrong? Because after recompiling it, the code becomes more or less correct - 24/25 are more correct than 8/9. So I thought it has to be the problem of compiler if compiling the same code can give different answers?
Actually I think its two bugs here - the 30/31 is one bug (which I have some idea what it MIGHT be now), and yet the recompiling is another bug...
But anyway... The recompiling issue here first...
Actually - maybe a further information here - after I change the CONSTANT from 1000 to 100 in the header file, immediately compiles it at the header page (which I assume Dev C++ 4.9.9.2 will immediately compile the whole project), the compilation takes 1 sec to complete. And the answer is wrong.
But if I then go to a random cpp change, delete a colon then add a colon then then compile again, this time the compiler will take 4 or 5 seconds to complete.
Could it be that - the compiler has some assumptions that if no cpp files are changed then it will compile something less - something like that?
Well at least use a compiler from this decade. The compiler that comes with Dev C++ 4.9.9.2 is horrifically old and full of bugs. Use a modern compiler.
Could it be that - the compiler has some assumptions that if no cpp files are changed then it will compile something less - something like that?
I think you're mixing up compiler and IDE. The compiler does what it's told. The IDE keeps track of what needs to be compiled. Dev Cpp 4.9.9.2 is bad. Bad bad bad. Don't use it, don't use the compiler that comes with it.
Thanks... To be honest I am quite noob with computers in general... What freeware (with accompanying compiler (and IDE - whatever it is)) would you suggest me to use for writing C++ then? And if I change to a new freeware, can I just open my projects in Dev C++ 4.9.9.2 as usual?
Actually... I just download Code::Blocks10.05 as per your suggestion of NOT to use Dev C++ XD. However, I now see a drag-down list of lots of C++ compilers... Can you suggest to me which one I should choose...?
Whichever (good, recent) one you have. The IDE is not the compiler; Code::Blocks is no more than a set of tools to help you write plain text files, and to remove from you the need to manually tell the compiler and linker what to do. As such, Code::Blocks has been made to be able to interact with a variety of compilers. It is up to you to get one; at least one of the download options for Code::Blocks comes with a compiler, but other options do not.
Hmm... Is "GNU GCC Compiler" ok? Like I said I am quite noob and I am not sure which one's are good and recent...
Other choices of compilers available include Microsoft Visual C++ 2005/2008, Digital Mars Compiler, LCC Compiler, Intel C/C++ Compiler, Tiny C Compiler, GDC D Compiler, SDCC Compiler Digital Mars D Compiler, GNU ARM GCC Compiler, GNU AVR GCC Compiler etc.
Sorry but I am really clueless which one to choose...
But if I then go to a random cpp change, delete a colon then add
a colon then then compile again, this time the compiler will take 4 or 5
seconds to complete.
Visual Studio 10 does the same thing. I think this is probably normal. If you don't make any changes since the last compile, the compiler detects this. I don't know if it just doesn't recompile it or if it takes a big shortcut?