Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/CLI: Console::WriteLine() or cout?

Tags:

c++-cli

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!

like image 507
Bavilo Avatar asked Sep 26 '14 13:09

Bavilo


People also ask

What is console WriteLine ()?

WriteLine(Object) Writes the text representation of the specified object, followed by the current line terminator, to the standard output stream. WriteLine(String, Object, Object, Object, Object)

What is the difference between console WriteLine and console write?

While Write() and WriteLine() both are the Console Class methods. The only difference between the Write() and WriteLine() is that Console. Write is used to print data without printing the new line, while Console. WriteLine is used to print data along with printing the new line.

How does WriteLine () method works?

WriteLine() method displays the output and also provides a new line character it the end of the string, This would set a new line for the next output.

How do I get console WriteLine output in Visual Studio?

In Visual Studio uppermost menu choose Debug > Windows > Output. It shows all Console. WriteLine("Debug MyVariable: " + MyVariable) when you get to them.


2 Answers

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

like image 161
Stígandr Avatar answered Sep 18 '22 23:09

Stígandr


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.

like image 32
Ben Voigt Avatar answered Sep 18 '22 23:09

Ben Voigt