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

I am returning to school where we must take the C ++ class. I am familiar with the language, but there are a few things that I have never heard of ...

As a rule, my teacher said that simple C ++ is "unsafe." It generates "unsafe code" (whatever that means). This is why we should use C ++ / CLI, which should make "safe" code.

Now ... is this not a CLI, but just a Microsoft.NET extension?

He also tells us to use Console::WriteLine() instead of cout . Because Console::WriteLine() is “safe” and cout is “unsafe”.

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

Thanks!

+7
c ++ - cli
source share
2 answers

To do this is very rude and simple.

Safe

Under “safe code,” your teacher probably means managed code . This is code where you don’t need to “take care” of pointers and memory, you have a garbagecollector that takes care of this for yours. You are dealing with reflections. Examples of such languages ​​are java and C #. The code compiles into "fictional" opcodes (intermediate language, IL for C #), and also compiles and runs in real time (JIT, only at compile time). The code generated by IL must be converted to real code based on its own platform, in java this is one of the things jvm does. You can easily parse code in such languages. And they can work on several platforms without recompilation.

Dangerous

By "unsafe code", a teacher means ordinary native C ++ unmanaged code , where all memory and resource management is done by you. This creates opportunities for human errors, memory leaks, resource leaks, and other memory errors that you usually cannot handle in managed languages. It also compiles into pure bytecode (native build operations codes), which means that you must compile your code for each platform that you intend to target. You will find that you will need to do a lot of code for each platform, depending on what you are going to code. It's nice to see that simple things like threads, which are platform dependent, are now part of the C ++ standard.

Then you have C ++ / CLI, which is basically a mix. You can use managed code from a .net structure in C ++, and it can be used as a bridge and used to create wrappers.

Console::WriteLine() controlled by .net code, safe.

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

Here you will find a related post with a wider answer here and here :)

Edit

As indicated by the Deduplicator below this is also of interest to you

Hope this helps.

Greetings

+6
source share

In the .NET world, "safe" is synonymous with "type-safe" security. In Visual C ++, it turns on /clr:safe .

/clr:safe will not allow you to use std::cout or any other function or type implemented in native code, because the metadata needed by the .NET verifier does not exist for native functions. The MSIL mentioned by Stigger can be used to compile “just in time”, but even when compiling to your own code is done in advance, MSIL is provided along with the compiled code and serves as proof of its type, which the verifier checks.

Standard (own / unmanaged). C ++ checks security at compile time. But this can be disabled using throws and without runtime checks, which C ++ does not provide as part of the language, pointer arithmetic (for example, an array index outside the bounds) can also violate type safety, and also use pointers for freed objects. C ++ is not only a language, but also a standard library where you will find smart pointers and smart collections that perform the necessary runtime checks, so it can be as type-safe as any managed infrastructure.

+4
source share

All Articles