C ++ gcc vs visual c ++

advertisements

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..


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 warnings or diagnostics) 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 -pendantic (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).