Reputation: 117
Hello everyone as starting programmer in c++ i was looking into some differences in compilers I imported the same source files for both the gcc compiler (code blocks) and the visual c++ (Visual studio express) and i found some strange behavior that i did not expect.
The visual c++ threw a bunch of errors which were in my opinion quite big... like iterating through vector with different iterators , iterator was from another instance of the vector than the operation was done on with this iterator.... gcc compiled successfully and threw no errors in runtime... while the visual c++ threw a bunch of errors in compilation and then threw a runtime error of 'different iterator type', or dynamic char allocation with new char[str.length()+1] and strcpy_s() into them from string - visual c++ debuger threw runtime error of corrupted heap while code blocks debugger ran just fine.
My question is. Is there really this big of a difference in these compilers and debuggers? Should i worry that my programming is on a bad level if the code runs totally perfect with gcc and code blocks debuger but throws errors in visual studio?
I ve learned to programm in c++ in code blocks, visal c++ has shown me mistakes that i was totally not aware of..
Upvotes: 6
Views: 14738
Reputation: 36597
The problem is with your code, not with your compilers or setup. The types of problems you are describing are examples of undefined behaviour that result from rather bad programming or coding techniques (in fact, some of them are fairly hard to achieve, without going out of your way to write very flawed code).
The thing is, compilers are not required to detect such things. Whether they do or not is a concern of compiler or library quality of implementation. In your case, it appears that your version of VC++ is detecting concerns that g++ is not, which is a point in favour of VC++.
My experience is actually the reverse of that: I find g++ detects more problems than VC++. However, both VC++ and g++ do diagnose problems that the other does not.
Which all just goes to show that your milage will vary. Personally, I'm an advocate of feeding all my code through multiple compilers when possible - precisely because that widens the net of what problems are diagnosed.
And then I exercise a policy of ensuring my code compiles cleanly with all compilers (no diagnostics at all, which includes no warnings) without having to disable any diagnostics, and avoiding use of any code constructs that are designed to suppress compiler diagnostics.
One thing to realise is that compilers, when installed, are typically configured to NOT produce many diagnostics. The reasons for this are historical. It is necessary to turn on the settings to make the compiler give warnings or errors. With g++, command options like -Wall -pedantic
(which can be enabled through Code::Blocks) really increase the number of problems that will be reported. There are similar options for VC++ (although I don't remember them offhand).
Upvotes: 8
Reputation: 66922
GCC's standard library does exactly what you told it, bugs and all. Most of the time, it behaves as you expected, and you don't realize that the bugs are there. Don't be fooled by the fact the program appears to work, it may still have bugs.
Visual Studio has two variants of the standard library. In Release builds, it acts the same as GCC. It does exactly what you told it to do, bugs and all. In Debug builds, it adds a ton of code behind the scenes to detect some of these errors, and will notify you, as you've observed. Fix these!. Note that some of these, like "heap corruption" mean that it detected that a bug occured several seconds ago, and does not mean that the bug is at the free/delete. You should also go to the project properties, and in C++/General, make sure your Warning Level is set to Level3 or even Level4. This will reveal even more bugs at compile time.
The differences in the compilers in this respect aren't that significant, except that in Debug builds, Visual Studio adds tons of error checking that's finding bugs. The other implementations, and Visual Studio in Release builds, don't go out of their way to help you find bugs.
Upvotes: 3
Reputation: 7294
MSVC has "checked iterators" for std::vector
, which perform a number of useful checks. You can turn on some of these types of checks in GCC by compiling with -D_GLIBCXX_DEBUG
. If you want your access to always be bounds-checked, then you need to use std::vector::at()
. Often, for performance reasons, it is better to ensure bounds checking outside of your loop and then use unchecked iterators or indexing in your loop.
Upvotes: 2