青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

C++ Programmer's Cookbook

{C++ 基礎} {C++ 高級} {C#界面,C++核心算法} {設計模式} {C#基礎}

Managed, Unmanaged, Native: What Kind of Code Is This?(轉)

With the release of Visual Studio .NET 2003 (formerly known as Everett) on April 24th, many developers are now willing to consider using the new technology known as managed code. But especially for C++ developers, it can be a bit confusing. That's because C++,

What Is Managed Code?

 Managed Code is what Visual Basic .NET and C# compilers create. It compiles to Intermediate Language (IL), not to machine code that could run directly on your computer. The IL is kept in a file called an assembly, along with metadata that describes the classes, methods, and attributes (such as security requirements) of the code you've created. This assembly is the one-stop-shopping unit of deployment in the .NET world. You copy it to another server to deploy the assembly there—and often that copying is the only step required in the deployment.

Managed code runs in the Common Language Runtime. The runtime offers a wide variety of services to your running code. In the usual course of events, it first loads and verifies the assembly to make sure the IL is okay. Then, just in time, as methods are called, the runtime arranges for them to be compiled to machine code suitable for the machine the assembly is running on, and caches this machine code to be used the next time the method is called. (This is called Just In Time, or JIT compiling, or often just Jitting.)

As the assembly runs, the runtime continues to provide services such as security, memory management, threading, and the like. The application is managed by the runtime.

Visual Basic .NET and C# can produce only managed code. If you're working with those applications, you are making managed code. Visual C++ .NET can produce managed code if you like: When you create a project, select one of the application types whose name starts with .Managed., such as .Managed C++ application..

What Is Unmanaged Code?

Unmanaged code is what you use to make before Visual Studio .NET 2002 was released. Visual Basic 6, Visual C++ 6, heck, even that 15-year old C compiler you may still have kicking around on your hard drive all produced unmanaged code. It compiled directly to machine code that ran on the machine where you compiled it—and on other machines as long as they had the same chip, or nearly the same. It didn't get services such as security or memory management from an invisible runtime; it got them from the operating system. And importantly, it got them from the operating system explicitly, by asking for them, usually by calling an API provided in the Windows SDK. More recent unmanaged applications got operating system services through COM calls.

Unlike the other Microsoft languages in Visual Studio, Visual C++ can create unmanaged applications. When you create a project and select an application type whose name starts with MFC, ATL, or Win32, you're creating an unmanaged application.

This can lead to some confusion: When you create a .Managed C++ application., the build product is an assembly of IL with an .exe extension. When you create an MFC application, the build product is a Windows executable file of native code, also with an .exe extension. The internal layout of the two files is utterly different. You can use the Intermediate Language Disassembler, ildasm, to look inside an assembly and see the metadata and IL. Try pointing ildasm at an unmanaged exe and you'll be told it has no valid CLR (Common Language Runtime) header and can't be disassembled—Same extension, completely different files.

What about Native Code?

The phrase native code is used in two contexts. Many people use it as a synonym for unmanaged code: code built with an older tool, or deliberately chosen in Visual C++, that does not run in the runtime, but instead runs natively on the machine. This might be a complete application, or it might be a COM component or DLL that is being called from managed code using COM Interop or PInvoke, two powerful tools that make sure you can use your old code when you move to the new world. I prefer to say .unmanaged code. for this meaning, because it emphasizes that the code does not get the services of the runtime. For example, Code Access Security in managed code prevents code loaded from another server from performing certain destructive actions. If your application calls out to unmanaged code loaded from another server, you won't get that protection.

The other use of the phrase native code is to describe the output of the JIT compiler, the machine code that actually runs in the runtime. It's managed, but it's not IL, it's machine code. As a result, don't just assume that native = unmanaged.

Does Managed Code Mean Managed Data?

Again with Visual Basic and C#, life is simple because you get no choice. When you declare a class in those languages, instances of it are created on the managed heap, and the garbage collector takes care of lifetime issues. But in Visual C++, you get a choice. Even when you're creating a managed application, you decide class by class whether it's a managed type or an unmanaged type. This is an unmanaged type:

class Foo
{
private:
   int x;
public:
    Foo(): x(0){}
    Foo(int xx): x(xx) {}
};

This is a managed type:

__gc class Bar
{
private:
   int x;
public:
    Bar(): x(0){}
    Bar(int xx): x(xx) {}
};

The only difference is the __gc keyword on the definition of Bar. But it makes a huge difference.

Managed types are garbage collected. They must be created with new, never on the stack. So this line is fine:

Foo f;

But this line is not allowed:

Bar b;

If I do create an instance of Foo on the heap, I must remember to clean it up:

Foo* pf = new Foo(2);
// . . .
delete pf;

The C++ compiler actually uses two heaps, a managed an unmanaged one, and uses operator overloading on new to decide where to allocate memory when you create an instance with new.

If I create an instance of Bar on the heap, I can ignore it. The garbage collector will clean it up some after it becomes clear that no one is using it (no more pointers to it are in scope).

There are restrictions on managed types: They can't use multiple inheritance or inherit from unmanaged types, they can't allow private access with the friend keyword, and they can't implement a copy constructor, to name a few. So, you might not want your classes to be managed classes. But that doesn't mean you don't want your code to be managed code. In Visual C++, you get the choice.


---------------------------------------------------------

About the Author

Kate Gregory is a founding partner of Gregory Consulting Limited (www.gregcons.com). In January 2002, she was appointed MSDN Regional Director for Toronto, Canada. Her experience with C++ stretches back to before Visual C++ existed. She is a well-known speaker and lecturer at colleges and Microsoft events on subjects such as .NET, Visual Studio, XML, UML, C++, Java, and the Internet. Kate and her colleagues at Gregory Consulting specialize in combining software develoment with Web site development to create active sites. They build quality custom and off-the-shelf software components for Web pages and other applications. Kate is the author of numerous books for Que, including Special Edition Using Visual C++ .NET.

posted on 2005-11-21 10:47 夢在天涯 閱讀(2365) 評論(11)  編輯 收藏 引用 所屬分類: CPlusPlusManage c++ /CLI

評論

# re: Managed, Unmanaged, Native: What Kind of Code Is This?(轉) 2006-04-19 12:19 夢在天涯

非托管

在.net 編程環境中,系統的資源分為托管資源和非托管資源。
對于托管的資源的回收工作,是不需要人工干預回收的,而且你也無法干預他們的回收,所能夠做的只是了解.net CLR如何做這些操作。也就是說對于您的應用程序創建的大多數對象,可以依靠 .NET Framework 的垃圾回收器隱式地執行所有必要的內存管理任務。

對于非托管資源,您在應用程序中使用完這些非托管資源之后,必須顯示的釋放他們,例如System.IO.StreamReader的一個文件對象,必須顯示的調用對象的Close()方法關閉它,否則會占用系統的內存和資源,而且可能會出現意想不到的錯誤。

我想說到這里,一定要清楚什么是托管資源,什么是非托管資源了?

最常見的一類非托管資源就是包裝操作系統資源的對象,例如文件,窗口或網絡連接,對于這類資源雖然垃圾回收器可以跟蹤封裝非托管資源的對象的生存期,但它不了解具體如何清理這些資源。還好.net Framework提供了Finalize()方法,它允許在垃圾回收器回收該類資源時,適當的清理非托管資源。如果在MSDN Library 中搜索Finalize將會發現很多類似的主題,這里列舉幾種常見的非托管資源:ApplicationContext,Brush,Component,ComponentDesigner,Container,Context,Cursor,FileStream,Font,Icon,Image,Matrix,Object,OdbcDataReader,OleDBDataReader
,Pen,Regex,Socket,StreamWriter,Timer,Tooltip 等等資源。可能在使用的時候很多都沒有注意到!

關于托管資源,就不用說了撒,像簡單的int,string,float,DateTime等等,.net中超過80%的資源都是托管資源。

非托管資源如何釋放,.NET Framework 提供 Object.Finalize 方法,它允許對象在垃圾回收器回收該對象使用的內存時適當清理其非托管資源。默認情況下,Finalize 方法不執行任何操作。默認情況下,Finalize 方法不執行任何操作。如果您要讓垃圾回收器在回收對象的內存之前對對象執行清理操作,您必須在類中重寫 Finalize 方法。然而大家都可以發現在實際的編程中根本無法override方法Finalize(),在C#中,可以通過析構函數自動生成 Finalize 方法和對基類的 Finalize 方法的調用。

例如:

~MyClass()

{

// Perform some cleanup operations here.

}

該代碼隱式翻譯為下面的代碼。

protected override void Finalize()

{

try

{

// Perform some cleanup operations here.

}

finally

{

base.Finalize();

}

}

但是,在編程中,并不建議進行override方法Finalize(),因為,實現 Finalize 方法或析構函數對性能可能會有負面影響。一個簡單的理由如下:用 Finalize 方法回收對象使用的內存需要至少兩次垃圾回收,當垃圾回收器回收時,它只回收沒有終結器(Finalize方法)的不可訪問的內存,這時他不能回收具有終結器(Finalize方法)的不可以訪問的內存。它改為將這些對象的項從終止隊列中移除并將他們放置在標記為“準備終止”的對象列表中,該列表中的項指向托管堆中準備被調用其終止代碼的對象,下次垃圾回收器進行回收時,就回收并釋放了這些內存。
  回復  更多評論   

# re: Managed, Unmanaged, Native: What Kind of Code Is This?(轉) 2010-06-20 00:06 DollySANDERS32

I think that to receive the <a href="http://lowest-rate-loans.com">loans</a> from banks you should present a firm motivation. But, once I've got a term loan, because I wanted to buy a bike.   回復  更多評論   

# re: Managed, Unmanaged, Native: What Kind of Code Is This?(轉) 2010-06-24 17:26 dissertation writing

It is cheap to find the buy dissertation get the hot data just like this good topic and bring it to thesis writing. And the essay writers want to thanks you for it!   回復  更多評論   

公告

EMail:itech001#126.com

導航

統計

  • 隨筆 - 461
  • 文章 - 4
  • 評論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1819135
  • 排名 - 5

最新評論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
      <noscript id="pjuwb"></noscript>
            <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
              <dd id="pjuwb"></dd>
              <abbr id="pjuwb"></abbr>
              亚洲国产福利在线| 香蕉久久夜色| 亚洲欧洲一区二区三区| 国产婷婷色一区二区三区| 国产毛片一区二区| 国产综合色精品一区二区三区| 激情欧美一区二区| 亚洲日本欧美天堂| 农村妇女精品| 久久国产精品黑丝| 久久大综合网| 日韩亚洲欧美成人| 午夜伦欧美伦电影理论片| 国产精品久线观看视频| 国产综合亚洲精品一区二| 久久精品国内一区二区三区| 最新高清无码专区| 欧美日韩综合视频| 亚洲高清自拍| 99热免费精品在线观看| 亚洲欧美精品suv| 欧美激情国产日韩精品一区18| 国产精品区一区二区三| 一本大道久久a久久综合婷婷| 久久精品一区二区国产| 久久黄色级2电影| 欧美视频在线观看| 久久精品国产69国产精品亚洲| 久久精品国产亚洲一区二区三区| 欧美福利视频一区| 亚洲综合色自拍一区| 亚洲国产精品一区二区尤物区| 午夜精品一区二区三区在线视 | 亚洲乱码精品一二三四区日韩在线| 欧美中文字幕| 国产精品视频内| 欧美91视频| 国产精品无码专区在线观看| 免费成人黄色片| 久久gogo国模裸体人体| 9国产精品视频| 久久国产一区二区三区| 国产一区二区看久久| 久久精品国产77777蜜臀| 欧美/亚洲一区| 亚洲片国产一区一级在线观看| 久久一区国产| 久久国产精品久久久久久| 在线亚洲伦理| 日韩午夜免费视频| 在线色欧美三级视频| 欧美国产日本在线| 久久中文欧美| 亚洲激情成人| 日韩写真在线| 亚洲美女尤物影院| 一区二区黄色| 国产麻豆91精品| 一本大道久久a久久综合婷婷| 影音先锋在线一区| 亚洲二区免费| 欧美视频一区二区三区…| 欧美黄色aa电影| 在线欧美亚洲| 久久免费午夜影院| 亚洲一区二区3| 久久爱www久久做| 久久激情网站| 国产在线乱码一区二区三区| 午夜精品久久久久久久| 亚洲国产高清一区| 老司机凹凸av亚洲导航| 久久尤物视频| 亚洲第一区在线| 欧美77777| 亚洲精品久久久久久久久久久久 | 久久久噜噜噜久久中文字幕色伊伊| 影音先锋久久精品| 久久青青草综合| 亚洲国产婷婷| 亚洲视频一区二区在线观看 | 亚洲国产精品悠悠久久琪琪| 亚洲欧洲精品一区二区三区不卡| 久久人人97超碰人人澡爱香蕉| 蜜臀久久99精品久久久画质超高清| 黄色精品一区二区| 国产精品99久久久久久久久 | 久久色中文字幕| 亚洲国产日韩欧美在线99| 亚洲看片一区| 国产精品欧美精品| 欧美在线一区二区| 亚洲国产成人在线| 亚洲欧美日本视频在线观看| 久久尤物视频| 亚洲精品一区二区三区樱花| 亚洲在线1234| 有坂深雪在线一区| 欧美精品色一区二区三区| 欧美一区二区三区免费看| 欧美黄色aa电影| 亚洲性视频网址| 亚洲一区二区3| 伊人久久大香线蕉综合热线| 欧美搞黄网站| 欧美主播一区二区三区| 亚洲国产色一区| 欧美综合77777色婷婷| 亚洲电影免费观看高清完整版在线观看 | 久久久青草婷婷精品综合日韩| 欧美日韩日本视频| 亚洲精品日产精品乱码不卡| 欧美亚洲综合另类| 国产精品拍天天在线| 老巨人导航500精品| 亚洲少妇中出一区| 欧美α欧美αv大片| 午夜日韩电影| 国产精品theporn88| 亚洲美女黄色片| 鲁大师成人一区二区三区| 韩日精品在线| 国产精品家庭影院| 香蕉久久久久久久av网站| 亚洲精品日韩在线观看| 久久久噜噜噜久久人人看| 亚洲一区日韩| 亚洲精品一品区二品区三品区| 国产婷婷精品| 国产美女一区| 欧美日韩在线观看视频| 女同一区二区| 久久五月天婷婷| 久久精彩视频| 欧美一区二区日韩一区二区| 久久亚洲午夜电影| 欧美在线一区二区三区| 亚洲一区一卡| 亚洲视频在线视频| 一区二区三区成人| 99av国产精品欲麻豆| 欧美系列电影免费观看| 欧美精品国产一区| 男女精品视频| 欧美大片91| 亚洲欧美国产不卡| 亚洲网站视频福利| 亚洲午夜激情网站| 亚洲午夜精品17c| 亚洲香蕉成视频在线观看| 一区二区精品在线| 一本色道久久综合亚洲精品不| 亚洲精品国产精品乱码不99| 亚洲国产一区二区视频| 亚洲国产欧美一区二区三区丁香婷| 欧美激情1区2区3区| 亚洲国产乱码最新视频| 亚洲精品国产拍免费91在线| 最新国产成人av网站网址麻豆| 亚洲国产日韩欧美综合久久| 亚洲激情不卡| 一区二区欧美在线观看| 亚洲小说欧美另类婷婷| 欧美伊人久久| 久久夜色精品| 欧美日韩亚洲综合在线| 国产精品手机视频| 黑人极品videos精品欧美裸| 1024日韩| 一本久久精品一区二区| 亚洲欧美清纯在线制服| 久久午夜色播影院免费高清| 欧美88av| 亚洲一区二区三区在线视频| 91久久精品美女高潮| 亚洲最新合集| 欧美一区日本一区韩国一区| 久久夜色精品一区| 欧美日韩一级片在线观看| 国产日韩精品在线| 亚洲区欧美区| 久久国内精品自在自线400部| 欧美成年人视频网站| 一本色道88久久加勒比精品 | 99re66热这里只有精品4| 亚洲女人天堂成人av在线| 久久综合99re88久久爱| 欧美日韩综合一区| 韩国v欧美v日本v亚洲v| 中文欧美日韩| 蜜臀a∨国产成人精品| 一区二区精品在线| 美女视频一区免费观看| 久久精品欧洲| 国产精品二区三区四区| 亚洲韩国日本中文字幕| 欧美在线观看视频一区二区三区| 亚洲国产成人精品久久久国产成人一区| 亚洲视频在线播放| 欧美成人精品在线观看|