11

I am going back to school where we have to take a C++ class. I am familiar with the language but there's a few things I have never heard of...

Generally, my teacher said that plain C++ is "unsafe". It generates "unsafe code" (whatever that means). That's why we have to use C++/CLI which is supposed to make "safe" code.

Now... isn't CLI just a Microsoft .NET extension?

He is also telling us to use Console::WriteLine() instead of cout. Since Console::WriteLine() is "safe" and cout is "unsafe".

All this seems weird to me... Can anyone clarify this?

Thanks!

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Bavilo
  • 389
  • 1
  • 6
  • 19
  • 13
    .Net has a "safe" and "unsafe" concept, but in general, it sounds like your teacher is an idiot. C++/CLI is a Microsoft extension to allow C++ code to work with and use .Net libraries. Very nice to create a wrapper for pure C++ code. But this should not be taught in a C++ class. – crashmstr Sep 26 '14 at 13:22
  • 5
    What your teacher is telling you is to use Microsoft APIs instead of standard ones. Which is fine if the whole point of the course is **about** teaching you C++/CLI (specifically), but is pretty poor practice if he's to teach you C++ in general. – DevSolar Sep 26 '14 at 13:23
  • 1
    I assume they are referring to [managed code](http://en.wikipedia.org/wiki/Managed_code) vs unmanaged code. – Cory Kramer Sep 26 '14 at 13:23
  • 2
    Also, if you are writing C++, use C++ libraries and classes, if you are writing C++/CLI, use .Net libraries and classes. If you are working on bridging the two, you have to convert back and forth. – crashmstr Sep 26 '14 at 13:28
  • This teacher should take C++ classes himself. – Christian Hackl Sep 26 '14 at 14:46
  • 1
    Well...I assumed that we would be going over everything from beginning to end...Such as types, variables, if/else, etc. But he just jumped right into Visual Studio with the Windows Forms addon and explicitly told us not to touch any of the generated code, as he doesn't have a clue how to work with that. Yes, seems like he is an Idiot – Bavilo Sep 26 '14 at 14:53
  • 1
    That's a danger sign -- new versions of Visual Studio don't have "the Windows Forms addon" for C++/CLI. WinForms is still there and you can still write code, but there are no wizards or GUI editors. – Ben Voigt Sep 26 '14 at 14:57
  • 1
    In the "real" world, one writes UI in C# using WinForms or WPF, with all the glitzy wizards and editors, and number crunching or platform control code in C++/CLI where the native compiler really shines both because it understands the OS header files and also the optimizer is much better. – Ben Voigt Sep 26 '14 at 15:05
  • 1
    The teacher had us import the old Windows Forms template from VS 2010. And yes I have read that Microsoft does not recommend building a new GUI programm using Windows Forms on C++. I guess he is ignoring that, or he doesn't know. – Bavilo Sep 26 '14 at 15:07

2 Answers2

8

To put it very blunt and simple.

Safe

By "safe code" you teacher probably means managed code. That is code where you don't have to "care" about pointers and memory, you have a garbagecollector that takes care of this for your. You are dealing with refrences. Examples of languages built like this is java and c#. Code is compiled to a "fictional" opcodes(intermediate language, IL for C#), and compiles and run realtime(JIT, just in time compilation). The IL generated code, will have to be converted to real native platform based opcodes, in java this is one of things the jvm does. You may easily disassemble code from languages like these. And they may run on several platforms without a recompilation.

Unsafe

By "unsafe code" the teacher means ordinary native c++ unmanaged code, where all memory and resource management is handled by you. This makes room for human error, and memory leaks, resource leaks and other memory errors, you don't usually deal with in managed languages. It also compiles to pure bytecode (native assembly opcodes), which means that you have to compile your code for each platform you intend to target. You will encounter that you will have to make a lot of code specific for each platform, depending on what you are going to code. It's nice to see that simple things such as threading, which where platform dependent, now is a part of the c++ standard.

Then you have c++/CLI, which basicly is a mix. You may use managed code from the .net framework in c++, and it may be used as a bridge, and be used to make wrappers.

Console::WriteLine() is managed .net code, safe.

cout is standard iso c++ from <iostream>, unsafe

You find a related post here, with a broader answer here and here :)

Edit

As pointed out by Deduplicator below this is also of interest for you

Hope it helps.

Cheers

Community
  • 1
  • 1
Stígandr
  • 2,874
  • 21
  • 36
  • 1
    Also of interest, unsafe code in C#: http://msdn.microsoft.com/en-us/library/chfa2zb8.aspx – Deduplicator Sep 26 '14 at 14:32
  • @Deduplicator +1+1 ;) Very good i didn't have time to write an academic answer here, and forgot to mention that keyword. – Stígandr Sep 26 '14 at 14:36
  • 1
    Thanks, that explained it all! – Bavilo Sep 26 '14 at 14:52
  • 2
    If "all memory and resource management is handled by you", you're writing C++ wrong. It should be handled by smart pointers and collection classes. – Ben Voigt Sep 26 '14 at 14:55
  • @BenVoigt it was as stated put bluntly there, I didn't have time to write everything in post there, and it's not exactly academic. I'll see if I get time to revise it later. +1 for your answer. – Stígandr Sep 26 '14 at 14:57
  • But does it really make the code that much unsafer if I use cout/cin in a C++/CLI project, simply to output some variables? – Bavilo Sep 26 '14 at 15:05
  • The difference is a bit deeper than that, because the whole of a C++/CLI programs, including code related to native types, runs on managed code unless otherwise explicitly specified with `#pragma unmanaged`. Even the C++ standard library is a managed-code version thereof (`MSVCMxx.DLL`). – Medinoc Sep 27 '14 at 15:13
4

In the world of .NET, "safe" is synonymous with "verifiable" type safety. In Visual C++, it's enabled by /clr:safe.

/clr:safe will prevent you from using std::cout or any other function or type implemented in native code, because the metadata needed by .NET's verifier does not exist for native functions. MSIL which Stigandr mentioned can be used for just-in-time compilation, but even when compilation to native code is performed ahead of time, the MSIL is provided alongside the compiled native code and serves as a proof of its type safety which the verifier inspects.

Standard (native / unmanaged) C++ does check type safety during compilation. But that can be disabled by casts, and without runtime type checks, which C++ does not provide as part of the language, pointer arithmetic (e.g. array index out of bounds) can also violate type safety, as can using pointers to freed objects. C++ isn't just a language though, it is also a standard library, where you find smart pointers and smart collections that do the necessary runtime checks, so it can be just as type-safe as any managed framework.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 1
    @Alf: Unfortunately the evidence points to the conclusion that the teacher is (incorrectly) using it the way Stigandr said, as a synonym of "managed". – Ben Voigt Sep 26 '14 at 15:02
  • IIRC, in C++/CLI `std::cout` is managed (but still unsafe) code. – Medinoc Sep 27 '14 at 15:27