Analysis of hosted and non-hosted C ++ code

Source: Internet
Author: User

With the release of Visual Studio. NET 2003 in April 24, many developers began to consider usingCodeNew technology. However, C ++ developers may encounter some problems. Because c ++ is special.

What is managedCode?

ManagedCodeIs the Code Compiled by the Visual Basic. NET and C # compilers. The compiler compiles the code into an intermediate language (IL) instead of a machine code that can run directly on your computer. An intermediate language is encapsulated in an assembly file. A program contains all metadata describing the classes, methods, and attributes you create (such as security requirements. This Assembly is an all-in-one shopping unit in the. NET world. You can copy the Assembly to another server to deploy it. Generally, this copy operation is the only operation in the deployment process.

ManagedCodeRun in the Common Language Runtime Library (CLR. This Runtime Library provides various services for your running code. Generally, it loads and verifies the Assembly to ensure the correctness of the intermediate language. When some methods are called, The Runtime Library compiles the specific methods into a mechanical code suitable for the Local Computer to run, and then caches the compiled mechanical code for future calls. (This is instant compilation)

As the Assembly runs, the Runtime Library continuously provides various services, such as security, memory management, and thread management. This program is "hosted" in the Runtime Library.

Visual Basic. NET and C # Can only generate hostingCode. If you write programs in this type of language, the generated code is managed code. If you want to, Visual C ++. Net can generate managed code. When creating a project, select a project whose name starts with. managed. For example,. Managed C ++ application.

What isUnmanagedCode?

UnmanagedCode is the Code created before Visual Studio. NET 2002 is released. For example, Visual Basic 6 and Visual C ++ 6. The worst thing is that, even the code generated by the old C compiler that is still residual in your hard disk and has more than 15 years of history is not managed. The managed code is directly compiled into the mechanical code of the target computer. These codes can only run on computers that compile and translate them, or on other computers with the same processor or almost the same processor. Unmanaged code cannot enjoy services provided by some runtime libraries, such as security and memory management. If the unmanaged code requires memory management and other services, it must explicitly call the interfaces of the operating system. Generally, they call the APIS provided by the Windows SDK for implementation. In recent cases, unmanaged programs use the COM interface to obtain operating system services.

Unlike other programming languages on Visual Studio, Visual C ++ can be createdUnmanagedProgram. When you create a project and select a project whose name starts with MFC, ATL, or Win32, the project will generate an unmanaged program.

This will lead to some confusion: When you create a local c‑based program, it creates an intermediate language set and an executable file with an extension name of .exe. When you create an MFC program, it is built as a Windows nativeCodeThe executable file. The extension name of this file is also .exe. The internal structures of these two files are completely different. You can use the intermediate language anti-assembler (ildasm) to view the internal and intermediate language metadata of the Assembly. If you try to viewUnmanagedIf you modify an executable file, the anti-assembler will tell you that the executable file does not contain a legal CLR header, so it cannot be decompiled. The two files have the same extension, but they are completely different.

NativeCodeWhat is it?

NativeCodeThis phrase can be used in two different contexts. Many people will integrate native codeUnmanagedCode is the same: the code built with older tools deliberately uses Visual C ++ and runs directly on the computer, and is not hosted in the Runtime Library. This can be a complete program, a COM component, or a DLL file that can be called by hosted code using COM intero or platform call (pinvoke, com intero or platform call (pinvoke) can help you migrate to the new technical platform and reuse the two powerful tools of old code. I would like to say that the Code is not managed because it emphasizes the code that cannot take advantage of the services provided by the Runtime Library. For example, in managed code, code access security services can prevent code loaded on another server from running specific operations. If your code runs unmanaged code, you cannot use this protection service.

NativeCodeAnother meaning is to describe the output of the instant compiler, the mechanical code that actually runs in the Runtime Library. These codes are managed codes, but not intermediate languages, but mechanical codes. So do not simply assume that native is equivalentUnmanaged.

ManagedCodeIt means hosting data?

For Visual Basic and C #, life is simple, because you have no other options. When you declare a class in those languages, the instance of this class will be created in the managed heap, And the Garbage Collector (GC) will help us manage the collection of these objects. However, in Visual C ++, you have another option. Even if you are creating a managed program, you can decide which classes are managed types and which classes areUnmanagedType.

This isUnmanagedType:

Class foo
{
PRIVATE:
Int X;
Public:
Foo (): x (0 ){}
Foo (int xx): X (XX ){}
};

This is the hosting type

_ GC class bar
{
PRIVATE:
Int X;
Public:
Bar (): x (0 ){}
Bar (int xx): X (XX ){}
};

The only difference between them is that the bar class has the _ GC keyword. This keyword will beCodeBring about a huge difference.

The managed type can be recycled by the garbage collector. They must be created with the keyword "new" and will never appear in the stack. So the following lineCodeIt is legal:

Foo F;

But this lineCodeIt is invalid:

Bar B;

If I create a foo object in the heap, I must be responsible for clearing this object:

Foo * pF = new Foo (2 );
//...
Delete PF;

The C ++ compiler actually uses two heaps, one for hosting and one for hosting.UnmanagedHeap, and then through the new operator to create instances of different types of classes, allocate different memory.

If I create a bar instance in the heap, I can ignore it. When no otherCodeWhen using the class, the garbage collector automatically clears the class and releases the resources it occupies.

There are some constraints on managed types: they cannot implement multiple inheritance, or inherit fromUnmanagedType; they cannot use the friend keyword for private access, and they cannot implement copy constructor. Therefore, you may not want to declare your class as a managed type. But that doesn't mean you don't want your code to be hosted. In Visual C ++, you can select.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.