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

C++ Programmer's Cookbook

{C++ 基礎(chǔ)} {C++ 高級(jí)} {C#界面,C++核心算法} {設(shè)計(jì)模式} {C#基礎(chǔ)}

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

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 夢(mèng)在天涯 閱讀(2289) 評(píng)論(11)  編輯 收藏 引用 所屬分類: CPlusPlusManage c++ /CLI

評(píng)論

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

非托管

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

對(duì)于非托管資源,您在應(yīng)用程序中使用完這些非托管資源之后,必須顯示的釋放他們,例如System.IO.StreamReader的一個(gè)文件對(duì)象,必須顯示的調(diào)用對(duì)象的Close()方法關(guān)閉它,否則會(huì)占用系統(tǒng)的內(nèi)存和資源,而且可能會(huì)出現(xiàn)意想不到的錯(cuò)誤。

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

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

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

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

例如:

~MyClass()

{

// Perform some cleanup operations here.

}

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

protected override void Finalize()

{

try

{

// Perform some cleanup operations here.

}

finally

{

base.Finalize();

}

}

但是,在編程中,并不建議進(jìn)行override方法Finalize(),因?yàn)椋瑢?shí)現(xiàn) Finalize 方法或析構(gòu)函數(shù)對(duì)性能可能會(huì)有負(fù)面影響。一個(gè)簡(jiǎn)單的理由如下:用 Finalize 方法回收對(duì)象使用的內(nèi)存需要至少兩次垃圾回收,當(dāng)垃圾回收器回收時(shí),它只回收沒有終結(jié)器(Finalize方法)的不可訪問的內(nèi)存,這時(shí)他不能回收具有終結(jié)器(Finalize方法)的不可以訪問的內(nèi)存。它改為將這些對(duì)象的項(xiàng)從終止隊(duì)列中移除并將他們放置在標(biāo)記為“準(zhǔn)備終止”的對(duì)象列表中,該列表中的項(xiàng)指向托管堆中準(zhǔn)備被調(diào)用其終止代碼的對(duì)象,下次垃圾回收器進(jìn)行回收時(shí),就回收并釋放了這些內(nèi)存。
  回復(fù)  更多評(píng)論   

# re: Managed, Unmanaged, Native: What Kind of Code Is This?(轉(zhuǎn)) 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.   回復(fù)  更多評(píng)論   

# re: Managed, Unmanaged, Native: What Kind of Code Is This?(轉(zhuǎn)) 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!   回復(fù)  更多評(píng)論   

公告

EMail:itech001#126.com

導(dǎo)航

統(tǒng)計(jì)

  • 隨筆 - 461
  • 文章 - 4
  • 評(píng)論 - 746
  • 引用 - 0

常用鏈接

隨筆分類

隨筆檔案

收藏夾

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

積分與排名

  • 積分 - 1811983
  • 排名 - 5

最新評(píng)論

閱讀排行榜

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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>
              欧美与欧洲交xxxx免费观看| 久久精品二区| 亚洲激情在线播放| 久久久久欧美精品| 1769国产精品| 亚洲激情网址| 国产精品一区二区a| 久久久久.com| 欧美成人久久| 亚洲一区二区欧美日韩| 亚洲欧美日韩直播| 91久久精品国产91久久| 日韩一二三在线视频播| 国产午夜精品久久久久久免费视 | 亚洲丰满在线| 亚洲国产精品久久久久婷婷老年| 欧美日韩国产丝袜另类| 欧美一区二区三区久久精品茉莉花 | 亚洲一区二区三区中文字幕| 国产精品亚洲аv天堂网| 久久精品盗摄| 欧美成人精品| 欧美在线视频a| 欧美大片18| 久久久久国产一区二区三区| 欧美国产日韩xxxxx| 欧美一区二区三区视频免费播放| 久热综合在线亚洲精品| 午夜亚洲精品| 欧美精品黄色| 久久久免费精品| 欧美日韩一区在线观看| 老色批av在线精品| 国产精品美女诱惑| 亚洲片在线资源| 激情成人av在线| 亚洲视频第一页| 亚洲精品久久视频| 久久久久久久精| 欧美一区二区三区婷婷月色 | 国产日韩在线看| 亚洲美女在线国产| 亚洲国产精品久久精品怡红院| 亚洲天堂男人| 中文国产成人精品久久一| 久久久精品五月天| 久久久999精品| 国产精品嫩草影院一区二区| 亚洲乱码国产乱码精品精天堂 | 蜜臀av性久久久久蜜臀aⅴ四虎| 国产精品久久久久久久久免费樱桃 | 99riav久久精品riav| 久久综合色天天久久综合图片| 性欧美大战久久久久久久久| 欧美日韩精品在线观看| 亚洲黄网站在线观看| …久久精品99久久香蕉国产| 久久精品噜噜噜成人av农村| 久久激五月天综合精品| 国产亚洲高清视频| 欧美亚洲综合久久| 久久久av毛片精品| 国产一区二区三区在线观看精品| 午夜一区二区三区不卡视频| 欧美在线免费视频| 国产日韩欧美高清免费| 香蕉av福利精品导航| 久久精品日韩| 国产酒店精品激情| 欧美一区二区三区男人的天堂| 欧美一区二区三区四区夜夜大片| 国产一区二区成人久久免费影院| 午夜视频久久久久久| 久久九九精品99国产精品| 欧美黄色片免费观看| 国产精品99久久久久久白浆小说| 欧美日韩精品福利| 亚洲一区亚洲| 久久久久久午夜| 亚洲第一视频网站| 欧美精品一区二区三区视频| 日韩亚洲欧美在线观看| 欧美一区二区啪啪| 1000部精品久久久久久久久| 欧美激情在线观看| 亚洲午夜一区二区| 久久综合影视| 亚洲美女在线看| 国产精品欧美激情| 蜜臀av国产精品久久久久| 亚洲精品韩国| 欧美影院成人| 亚洲国产精品一区二区第四页av | 一区二区三区|亚洲午夜| 欧美亚洲综合久久| 亚洲国产一区二区视频| 欧美三区美女| 久久久久国产一区二区三区四区| 亚洲区一区二| 久久精品日韩| 在线一区视频| 激情久久久久| 欧美视频精品一区| 久久一区中文字幕| 国产精品99久久99久久久二8| 久久婷婷丁香| 在线亚洲高清视频| 亚洲成人直播| 国产美女诱惑一区二区| 欧美精品 国产精品| 欧美在线播放视频| 野花国产精品入口| 亚洲国产成人久久综合一区| 久久精品一区二区三区不卡牛牛| 亚洲精品欧美日韩专区| 韩国免费一区| 国产精品一区二区三区四区| 欧美黄色视屏| 老司机一区二区三区| 欧美一区二区三区四区夜夜大片| 国产精品久久一区二区三区| 久久一区激情| 久久精品国产在热久久| 翔田千里一区二区| 亚洲一区二区日本| 日韩视频在线一区二区| 欧美激情片在线观看| 久久久www成人免费无遮挡大片| 亚洲一区二区三区四区五区午夜| 亚洲精品黄色| 亚洲国产婷婷香蕉久久久久久| 韩日视频一区| 国内精品久久久久影院色| 国产欧美日本一区二区三区| 国产精品毛片大码女人| 国产精品久久久久久影视 | 激情亚洲网站| 国产一区二区三区四区五区美女| 国产欧美日韩一级| 国产精品视频1区| 国产精品电影网站| 国产精品第一区| 国产欧美日韩综合精品二区| 国产欧美一区二区三区国产幕精品| 欧美亚州一区二区三区| 欧美色欧美亚洲另类七区| 一区二区91| 欧美成人xxx| 欧美国产亚洲精品久久久8v| 欧美二区在线播放| 免费91麻豆精品国产自产在线观看| 久久久亚洲影院你懂的| 免费在线看成人av| 欧美成人视屏| 91久久精品视频| 一区二区福利| 香蕉久久精品日日躁夜夜躁| 欧美一区二区三区四区视频| 久久亚洲国产精品日日av夜夜| 蜜臀91精品一区二区三区| 欧美精品亚洲| 国产精品日韩高清| 国内精品久久久久久 | 一区二区欧美在线| 亚洲欧美第一页| 久久久久国产一区二区三区四区 | 亚洲无线视频| 久久精品视频网| 欧美成人综合一区| 一区二区三区回区在观看免费视频| 亚洲综合三区| 噜噜噜躁狠狠躁狠狠精品视频| 欧美日本韩国在线| 国产性色一区二区| 亚洲久久一区| 久久精品国产成人| 91久久精品美女高潮| 午夜日韩av| 欧美日韩成人一区二区| 国产主播精品| 一本久道久久久| 久久久久.com| 9久草视频在线视频精品| 久久成人精品视频| 欧美偷拍另类| 亚洲国产精品精华液2区45 | a91a精品视频在线观看| 久久久99爱| 一本大道久久a久久精二百| 久久久www免费人成黑人精品| 欧美久久久久免费| 在线观看精品视频| 午夜精品亚洲| 亚洲精品在线视频观看| 久久精品人人做人人综合| 国产精品久久久久久久久借妻| 亚洲欧洲精品一区二区三区波多野1战4| 欧美亚洲在线视频| 一区二区三区四区五区在线| 欧美福利一区|