锘??xml version="1.0" encoding="utf-8" standalone="yes"?>欧美系列精品,久久综合给合久久狠狠色,亚洲欧美999http://www.shnenglu.com/mymsdn/category/7975.htmlMyMSDN璁板綍寮鍙戞柊鐭ラ亾zh-cnSun, 20 Mar 2011 12:47:46 GMTSun, 20 Mar 2011 12:47:46 GMT60鍏充簬protobuf涓殑field_number鑼冨洿鐨勮В閲?/title><link>http://www.shnenglu.com/mymsdn/archive/2011/03/19/142184.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Fri, 18 Mar 2011 18:01:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2011/03/19/142184.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/142184.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2011/03/19/142184.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/142184.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/142184.html</trackback:ping><description><![CDATA[<!--MyMSDN style!--><link rel="stylesheet" type="text/css" > <style type="text/css"> body{background-color:#eeeeee;} </style> <div>浠ヤ笅鏄潵鑷狦oogle Protobuf Documents閲岀殑涓鍙ヨ瘽錛?</div> <div id="hvzpftn" class="quote">As you can see, each field in the message definition has a unique numbered tag. These tags are used to identify your fields in the <a >message binary format</a>, and should not be changed once your message type is in use. Note that tags with values in the range 1 through 15 take one byte to encode. Tags in the range 16 through 2047 take two bytes. So you should reserve the tags 1 through 15 for very frequently occurring message elements. Remember to leave some room for frequently occurring elements that might be added in the future. </div> <div>榪欓噷瑕佸仛涓涓В閲婏紝灝辨槸涓轟粈涔堟槸1鍒?5錛屼互鍙?6鍒?047鍛紵</div> <div> <ol> <li>1鍒?5錛屼粎浣跨敤1bytes銆傛瘡涓猙yte鍖呭惈涓や釜閮ㄥ垎錛屼竴涓槸field_number涓涓槸tag錛屽叾涓璮ield-number灝辨槸protobuf涓瘡涓煎悗絳夊彿鍚庣殑鏁板瓧錛堝湪C++鍜孞ava涓紝濡傛灉涓嶈緗繖涓鹼紝鍒欏畠鏄殢鏈虹殑錛屽鏋滃湪Python涓紝涓嶈緗紝瀹冨垯涓嶈澶勭悊錛堣繖涓湪<a >message binary format</a>涓殑Field Order涓鑺備腑鏈夋彁鍒幫級銆傞偅涔堟垜浠彲浠ヨ涓鴻繖涓猣ield_number鏄繀欏葷殑銆傞偅涔堜竴涓猙yte鐢ㄦ潵琛ㄨ揪榪欎釜鍊煎氨鏄?span style="color: red">0</span><span style="color: blue">00000</span><span style="color: green">000</span>錛屽叾涓孩鑹茶〃紺烘槸鍚︽湁鍚庣畫瀛楄妭錛屽鏋滀負0琛ㄧず娌℃湁涔熷氨鏄繖鏄竴涓瓧鑺傦紝钃濊壊閮ㄥ垎琛ㄧずfield-number錛岀豢鑹查儴鍒嗗垯鏄痺ire_type閮ㄥ垎錛岃〃紺烘暟鎹被鍨嬨備篃灝辨槸(field_number << 3) | wire_type銆傚叾涓瓀ire_type鍙湁3浣嶏紝琛ㄧず鏁版嵁綾誨瀷銆傞偅涔堣兘澶熻〃紺篺ield_number鐨勫氨鏄?浣嶈摑鑹茬殑鏁板瓧錛?浣嶆暟瀛楄兘澶熻〃杈劇殑鏈澶ц寖鍥村氨鏄?-15錛堝叾涓?鏄棤鏁堢殑錛夈? <li>16鍒?047錛屼笌涓婇潰鐨勮鍒欏叾瀹炵被浼鹼紝涓嬮潰浠?bytes涓轟緥瀛愶紝閭d箞灝辨湁<span style="color: red">1</span><span style="color: blue">0000000 <span style="color: red">0</span>0000</span><span style="color: green">000</span>錛屽叾涓孩鑹查儴鍒嗕緷鐒舵槸絎﹀彿浣嶏紝鍥犱負姣忎釜byte鐨勭涓浣嶉兘鐢ㄦ潵琛ㄧず涓嬩竴byte鏄惁鍜岃嚜宸辨湁鍏籌紝閭d箞瀵逛簬>1byte鐨勬暟鎹紝絎竴浣嶄竴瀹氭槸1錛屽洜涓鴻繖閲屽亣璁炬槸2byte錛岄偅涔堢浜屼釜byte鐨勭涓浣嶄篃鏄孩鑹詫紝鍒ㄩ櫎榪欎袱浣嶏紝鍐嶆墸鎺?涓獁ire_type浣嶏紝鍓╀笅11浣嶏紙2*8-2-3錛夛紝鑳藉琛ㄨ揪鐨勬暟瀛楄寖鍥村氨鏄?047錛?<sup>11</sup>錛夈?/li></ol></div> <div> 鍙傝冭祫鏂欙細<br /> <ol><li>http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/proto.html</li> <li>http://code.google.com/apis/protocolbuffers/docs/encoding.html</li></ol> </div><img src ="http://www.shnenglu.com/mymsdn/aggbug/142184.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2011-03-19 02:01 <a href="http://www.shnenglu.com/mymsdn/archive/2011/03/19/142184.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>zig-zag緙栫爜鏂瑰紡http://www.shnenglu.com/mymsdn/archive/2011/03/19/zig-zag-encoding.htmlvolnetvolnetFri, 18 Mar 2011 16:36:00 GMThttp://www.shnenglu.com/mymsdn/archive/2011/03/19/zig-zag-encoding.htmlhttp://www.shnenglu.com/mymsdn/comments/142182.htmlhttp://www.shnenglu.com/mymsdn/archive/2011/03/19/zig-zag-encoding.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/142182.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/142182.htmlhttp://goo.gl/2wRKb
鐢ㄤ綅榪愮畻鏉ヨ〃紺烘妸涓涓礋鏁拌漿鎹㈡垚zig-zag緙栫爜錛屽氨鏄?span class="pun">

int32鏄細(n << 1) ^ (n >> 31)
int64鏄細(n << 1) ^ (n >> 63)
涔熷氨鏄錛屽鏋滄槸璐熸暟錛屽浜?2浣嶆渶澶氳兘鐪佸幓30鏍鹼紙鍏朵腑1鏍兼槸絎﹀彿浣嶏紝鍙︿竴涓唬琛ㄦ渶灝忓?錛屾澶勫亣璁?#8220;姝h礋0”涓嶅悎娉曪級銆傚悓鐞嗭紝64浣嶆渶澶氳兘鐪佸幓62浣嶃傚綋鐒舵瘮杈冩瀬绔殑鏄墍鏈夌殑浣嶆暟閮借鐢ㄤ笂浜嗐?br>



volnet 2011-03-19 00:36 鍙戣〃璇勮
]]>
IsWow64騫朵笉鑳界敤鏉ユ嫻嬫槸鍚︽槸Windows 32bit緋葷粺榪樻槸64bit緋葷粺http://www.shnenglu.com/mymsdn/archive/2010/09/06/125966.htmlvolnetvolnetSun, 05 Sep 2010 18:06:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/09/06/125966.htmlhttp://www.shnenglu.com/mymsdn/comments/125966.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/09/06/125966.html#Feedback10http://www.shnenglu.com/mymsdn/comments/commentRss/125966.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/125966.html濡備綍褰撳墠鎿嶄綔緋葷粺鏄笉鏄?4浣嶏紵濡備綍鍒ゆ柇褰撳墠搴旂敤紼嬪簭鏄惁鍦╓ow64涓嬭繍琛岋紵

棣栧厛浠涔堟槸Wow64錛熷緢澶氭湅鍙嬩竴鐪嬪埌64灝辮涓鴻繖涓柟娉曟槸鍒ゆ柇褰撳墠緋葷粺鏄惁鏄?4bit鐨勶紝鍏跺疄涓嶇劧銆俉ow64鏄疻indows-On-Windows64鐨勬剰鎬濓紝瀹冩槸鎸囧湪64浣嶇殑鎿嶄綔緋葷粺涓婏紙涓嶆槸鎸?4浣嶇殑CPU錛夎繍琛?2浣嶅簲鐢ㄧ▼搴忕殑鍏煎騫沖彴銆?/p>

涓嬮潰鏄疢SDN涓竴孌礗sWow64鐨勫簲鐢ㄧ▼搴忥細

BOOL IsWow64() 
{ 
    typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL); 
    LPFN_ISWOW64PROCESS fnIsWow64Process; 
    BOOL bIsWow64 = FALSE; 
    fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle(_T("kernel32")), "IsWow64Process"); 
    if (NULL != fnIsWow64Process) 
    {
        fnIsWow64Process(GetCurrentProcess(),&bIsWow64); 
    } 
    return bIsWow64; 
}

涓嬮潰鐨勪唬鐮佺敤鏉ユ嫻嬭繖涓▼搴忕殑緇撴灉錛?/p>

if( IsWow64() == TRUE )
{
    _tprintf(_T("IsWow64() == TRUE\n"));
}
else
{
    _tprintf(_T("IsWow64() == FALSE\n"));
}

璁╂垜浠紪璇戜竴涓嬭繖涓▼搴忋?/p>

鎴戜滑闇瑕佺殑鏄?4浣嶇殑鎿嶄綔緋葷粺錛屾瘮濡俋P64bit錛學indows 2008 R2絳夐兘鏄?4bit鎿嶄綔緋葷粺銆?/p>

鍦?4浣嶇殑鎿嶄綔緋葷粺涓婅繍琛岀殑kernel32.dll涓紝灝嗕細瀹炵幇IsWow64Process鏂規硶錛岃屽湪32浣嶇郴緇熶腑鎻愪緵鐨刱ernel32.dll涓垯娌℃湁鎻愪緵鐩稿叧鍑芥暟鐨勫疄鐜般?/p>

姣旇緝榪鋒儜浜虹殑鍒欐槸bIsWow64錛屽叾瀹炰粩緇嗙湅MSDN涓殑Remark錛屼細鍙戠幇錛?/p>

If the application is a 64-bit application running under 64-bit Windows, the Wow64Process parameter is set to FALSE.涔熷氨鏄64浣嶅簲鐢ㄧ▼搴忚窇鍦?4浣嶇殑鎿嶄綔緋葷粺涓婏紝bIsWow64鐨勫煎皢鏄疐ALSE鑰屼笉鏄疶RUE銆?

鍥犳鎴戜滑闇瑕佸垎鍒皢鎴戜滑鐨勭▼搴忕紪璇戞垚Win32騫沖彴鍜寈64騫沖彴鐨勶紝濡傛灉浣犵敤Visual Studio榪涜緙栬瘧錛岄粯璁ゅ畨瑁呭垯鍙寘鍚?2浣嶇殑緙栬瘧鍣?閾炬帴鍣紝鍗充究浣犳槸鍦?4浣嶆搷浣滅郴緇熶笂瀹夎錛屼篃鏄竴鏍風殑銆備綘闇瑕佸湪VC++鑺傜偣涓嬪嬀閫墄64閫夐」鎵嶅彲浠ワ紝Itanium鍒欓渶瑕佸湪Server騫沖彴涓嬪畨瑁呮墠鍙嬀閫夈傜劧鍚庡湪緙栬瘧鐨勬椂鍊欙紝鍒嗗埆閫夋嫨Win32鍜寈64榪涜緙栬瘧銆?

image

緙栬瘧鍚庯紝榪愯錛岀粨鏋滃鎴戜滑鍒嗘瀽鐨勪竴鏍鳳細

鍦?4浣嶇郴緇熶笂榪愯Win32緙栬瘧閰嶇疆鐨勭粨鏋滄槸IsWow64() == TRUE錛岃寈64緙栬瘧閰嶇疆鐨勭粨鏋滄槸IsWow64() == FALSE銆?

濡傛灉鎯寵鐭ラ亾褰撳墠緋葷粺鏄惁鏄?4浣嶇殑錛屽垯鍙互閫氳繃涓嬮潰鐨勬柟娉曪細

BOOL Is64bitSystem()
{
    SYSTEM_INFO si;
    GetNativeSystemInfo(&si);

    if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 ||    
        si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64 )
    {
        return TRUE;
    }
    else
    {
        return FALSE;
    } 
}

娉ㄦ剰錛氶渶瑕佹敞鎰忔槸GetNativeSystemInfo  鍑芥暟浠嶹indows XP 寮濮嬫墠鏈夛紝 鑰?IsWow64Process  鍑芥暟浠?Windows XP with SP2 浠ュ強 Windows Server 2003 with SP1 寮濮嬫墠鏈夈?



volnet 2010-09-06 02:06 鍙戣〃璇勮
]]>
鑻辯壒灝?amp;reg; 澶氱嚎紼嬪簲鐢ㄥ紑鍙戞寚鍗?/title><link>http://www.shnenglu.com/mymsdn/archive/2010/09/05/125912.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Sat, 04 Sep 2010 18:20:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2010/09/05/125912.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/125912.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2010/09/05/125912.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/125912.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/125912.html</trackback:ping><description><![CDATA[     鎽樿: http://software.intel.com/zh-cn/articles/intel-guide-for-developing-multithreaded-applications/ 鑻辯壒灝斅?澶氱嚎紼嬪簲鐢ㄥ紑鍙戞寚鍗?鎻愪氦鏂版枃绔? Published On :  2010騫?2鏈?5鏃?20:00 璇勭駭 璇風櫥褰曞悗璇勭駭錛佸綋鍓嶅垎鏁幫細 0 鐢?0 鐢ㄦ埛 璇風櫥褰曞悗璇勭駭錛佸綋鍓嶅垎鏁幫細 0 ...  <a href='http://www.shnenglu.com/mymsdn/archive/2010/09/05/125912.html'>闃呰鍏ㄦ枃</a><img src ="http://www.shnenglu.com/mymsdn/aggbug/125912.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2010-09-05 02:20 <a href="http://www.shnenglu.com/mymsdn/archive/2010/09/05/125912.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>[杞琞C/C++涓姩鎬侀摼鎺ュ簱鐨勫垱寤哄拰璋冪敤http://www.shnenglu.com/mymsdn/archive/2010/08/28/124983.htmlvolnetvolnetFri, 27 Aug 2010 16:30:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/08/28/124983.htmlhttp://www.shnenglu.com/mymsdn/comments/124983.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/08/28/124983.html#Feedback1http://www.shnenglu.com/mymsdn/comments/commentRss/124983.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/124983.html杞澆鑷細http://developer.51cto.com/art/200702/39608.htm

鍔ㄦ佽繛鎺ュ簱鐨勫垱寤烘楠わ細

涓銆佸垱寤篘on-MFC DLL鍔ㄦ侀摼鎺ュ簱

1銆佹墦寮File 鈥?gt; New 鈥?gt; Project閫夐」錛岄夋嫨Win32 Dynamic-Link Library 鈥?gt;sample project 鈥?gt;宸ョ▼鍚嶏細DllDemo

2銆佹柊寤轟竴涓?h鏂囦歡DllDemo.h

#ifdef DllDemo_EXPORTS
#define DllAPI __declspec(dllexport)
#else
#define DllAPI __declspec(dllimport)
extern "C" //鍘熸牱緙栬瘧
{
DllAPI int __stdcall Max(int a,int b); //__stdcall浣塊潪C/C++璇█鍐呰兘澶熻皟鐢ˋPI
}
#endif

3銆佸湪DllDemo.cpp鏂囦歡涓鍏llDemo.h鏂囦歡錛屽茍瀹炵幇Max(int,int)鍑芥暟

#include "DllDemo.h"
DllAPI int __stdcall Max(int a,int b)
{
if(a==b)
return NULL;
else if(a>b)
return a;
else
return b;
}

4銆佺紪璇戠▼搴忕敓鎴愬姩鎬佽繛鎺ュ簱

浜屻佺敤.def鏂囦歡鍒涘緩鍔ㄦ佽繛鎺ュ簱DllDemo.dll

1銆佸垹闄llDemo宸ョ▼涓殑DllDemo.h鏂囦歡銆?

2銆佸湪DllDemo.cpp鏂囦歡澶達紝鍒犻櫎 #include DllDemo.h璇彞銆?

3銆佸悜璇ュ伐紼嬩腑鍔犲叆涓涓枃鏈枃浠訛紝鍛藉悕涓篋llDemo.def騫跺啓鍏ュ涓嬭鍙ワ細

LIBRARY MyDll

EXPORTS

Max@1

4銆佺紪璇戠▼搴忕敓鎴愬姩鎬佽繛鎺ュ簱銆?

鍔ㄦ侀摼鎺ョ殑璋冪敤姝ラ錛?/strong>

涓銆侀殣寮忚皟鐢?/strong>

1銆佸緩绔婦llCnslTest宸ョ▼

2銆佸皢鏂囦歡DllDemo.dll銆丏llDemo.lib鎷瘋礉鍒癉llCnslTest宸ョ▼鎵鍦ㄧ殑鐩綍

3銆佸湪DllCnslTest.h涓坊鍔犲涓嬭鍙ワ細

#define DllAPI __declspec(dllimport)
#pragma comment(lib錛?DllDemo.lib") //鍦ㄧ紪杈戝櫒link鏃訛紝閾炬帴鍒癉llDemo.lib鏂囦歡
extern "C"
{
DllAPI int __stdcall Max(int a,int b);
}

4銆佸湪DllCnslTest.cpp鏂囦歡涓坊鍔犲涓嬭鍙ワ細

#include "DllCnslTest.h"http://鎴栬?#include "DllDemo.h"
void main()
{
int value;
value = Max(2,9);
printf("The Max value is %d\n",value);
}

5銆佺紪璇戝茍鐢熸垚搴旂敤紼嬪簭DllCnslTest.exe

浜屻佹樉寮忚皟鐢?/strong>

1銆佸緩绔婦llWinTest宸ョ▼銆?

2銆佸皢鏂囦歡DllDemo.dll鎷瘋礉鍒癉llWinTest宸ョ▼鎵鍦ㄧ殑鐩綍鎴朩indows緋葷粺鐩綍涓嬨?

3銆佺敤vc/bin涓嬬殑Dumpbin.exe鐨勫皬紼嬪簭錛屾煡鐪婦LL鏂囦歡(DllDemo.dll)涓殑鍑芥暟緇撴瀯銆?

4銆佷嬌鐢ㄧ被鍨嬪畾涔夊叧閿瓧typedef錛屽畾涔夋寚鍚戝拰DLL涓浉鍚岀殑鍑芥暟鍘熷瀷鎸囬拡銆?

渚嬶細

typedef int(*lpMax)(int a,int b); //姝よ鍙ュ彲浠ユ斁鍦?h鏂囦歡涓?/p>

5銆侀氳繃LoadLibray()灝咲LL鍔犺澆鍒板綋鍓嶇殑搴旂敤紼嬪簭涓茍榪斿洖褰撳墠DLL鏂囦歡鐨勫彞鏌勩?

渚嬶細

HINSTANCE hDll; //澹版槑涓涓狣ll瀹炰緥鏂囦歡鍙ユ焺
hDll = LoadLibrary("DllDemo.dll");//瀵煎叆DllDemo.dll鍔ㄦ佽繛鎺ュ簱

6銆侀氳繃GetProcAddress()鍑芥暟鑾峰彇瀵煎叆鍒板簲鐢ㄧ▼搴忎腑鐨勫嚱鏁版寚閽堛?

渚嬶細

lpMax Max;
Max = (lpMax)GetProcAddress(hDLL,"Max");
int value;
value = Max(2,9);
printf("The Max value is %d",value);

7銆佸嚱鏁拌皟鐢ㄥ畬姣曞悗錛屼嬌鐢‵reeLibrary()鍗歌澆DLL鏂囦歡銆?pre>

FreeLibrary(hDll);

8銆佺紪璇戝茍鐢熸垚搴旂敤紼嬪簭DllWinTest.exe

娉細鏄懼紡閾炬帴搴旂敤紼嬪簭緙栬瘧鏃朵笉闇瑕佷嬌鐢ㄧ浉搴旂殑Lib鏂囦歡銆?/p>

涓嬭澆錛?font style="background-color: #cccccc">Visual Studio 2008楠岃瘉閫氳繃錛夛細http://www.shnenglu.com/Files/mymsdn/DllCnsTest.7z



volnet 2010-08-28 00:30 鍙戣〃璇勮
]]>
涓や釜鏈夌敤鐨勫畯錛?amp;ldquo;紱佹綾繪垚鍛樺鍒?amp;rdquo;浠ュ強&ldquo;紱佹闅愬紡鏋勯?amp;rdquo;http://www.shnenglu.com/mymsdn/archive/2010/08/11/123003.htmlvolnetvolnetTue, 10 Aug 2010 16:34:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/08/11/123003.htmlhttp://www.shnenglu.com/mymsdn/comments/123003.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/08/11/123003.html#Feedback1http://www.shnenglu.com/mymsdn/comments/commentRss/123003.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/123003.html紱佹綾繪垚鍛樺鍒訛紝鍏舵牳蹇冨氨鍦ㄤ簬涓嶅厑璁哥被澶栭儴鐪嬭澶嶅埗鍑芥暟錛屽寘鎷?#8220;鎷瘋礉鏋勯犲嚱鏁?#8221;銆?#8220;operator =閲嶈澆”銆?/p>
#define DISALLOW_COPY_AND_ASSIGN(TypeName) \
private:                     \
TypeName(const TypeName&);               \
TypeName& operator=(const TypeName&)
紱佹闅愬紡鏋勯狅紝鍒欏彲浠ュ皢榛樿鏋勯犲嚱鏁伴殣钘忚搗鏉ワ紝鍦ㄥぇ澶氭暟緙栬瘧鍣ㄤ腑涔熷彲浠ュ鏋勯犲嚱鏁板鍔爀xplicit鍏抽敭瀛楁潵閬垮厤闅愬紡鏋勯犮?
#define DISALLOW_IMPLICIT_CONSTRUCTORS(TypeName) \
private:                     \
TypeName();                                    \
DISALLOW_COPY_AND_ASSIGN(TypeName)
鏇村瑙i噴璇﹁銆奙ore Effective C++銆?

volnet 2010-08-11 00:34 鍙戣〃璇勮
]]>
[C++]__declspec鍏抽敭瀛?/title><link>http://www.shnenglu.com/mymsdn/archive/2010/07/22/121059.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Thu, 22 Jul 2010 12:46:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2010/07/22/121059.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/121059.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2010/07/22/121059.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/121059.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/121059.html</trackback:ping><description><![CDATA[<p><span style="color: blue">__declspec</span>鍏抽敭瀛?/p><pre class="gc-code"><span style="color: green">// keyword__declspec.cpp : 瀹氫箟鎺у埗鍙板簲鐢ㄧ▼搴忕殑鍏ュ彛鐐廣? // // ms-help://MS.VSCC.v90/MS.MSDNQTR.v90.chs/kernel_d/hh/Kernel_d/64bitAMD_6db3322a-fe6d-4287-9eda-a9c1378e715d.xml.htm // The sizeof value for any structure is the offset of the final member, // plus that member's size, rounded up to the nearest multiple of the largest // member alignment value or the whole structure alignment value, // whichever is greater. </span><span style="color: blue">#include </span><span style="color: #a31515">"stdafx.h" </span><span style="color: blue">__declspec</span>( <span style="color: blue">align</span>( 32) ) <span style="color: blue">struct </span><span style="color: #010001">Struct__declspec_1 </span>{ <span style="color: blue">int </span><span style="color: #010001">a</span>; <span style="color: blue">int </span><span style="color: #010001">b</span>; }; <span style="color: blue">__declspec</span>( <span style="color: blue">align</span>( 32) ) <span style="color: blue">struct </span><span style="color: #010001">Struct__declspec_2 </span>{ <span style="color: blue">__declspec</span>( <span style="color: blue">align</span>( 64) ) <span style="color: blue">int </span><span style="color: #010001">a</span>; <span style="color: blue">int </span><span style="color: #010001">b</span>; }; <span style="color: blue">__declspec</span>( <span style="color: blue">align</span>( 8 ) ) <span style="color: blue">struct </span><span style="color: #010001">Struct__declspec_3 </span>{ <span style="color: blue">int </span><span style="color: #010001">a</span>; <span style="color: green">//4 bytes </span><span style="color: blue">int </span><span style="color: #010001">b</span>; <span style="color: green">//4 bytes </span><span style="color: blue">int </span><span style="color: #010001">c</span>; <span style="color: green">//4 bytes </span>}; <span style="color: blue">__declspec</span>( <span style="color: blue">align</span>( 8 ) ) <span style="color: blue">struct </span><span style="color: #010001">Struct__declspec_4 </span>{ <span style="color: blue">int </span><span style="color: #010001">a</span>; <span style="color: green">//4 bytes </span><span style="color: blue">int </span><span style="color: #010001">b</span>; <span style="color: green">//4 bytes </span>}; <span style="color: blue">struct </span><span style="color: #010001">StructNormal </span>{ <span style="color: blue">int </span><span style="color: #010001">a</span>; <span style="color: green">//4 bytes </span><span style="color: blue">int </span><span style="color: #010001">b</span>; <span style="color: green">//4 bytes </span><span style="color: blue">int </span><span style="color: #010001">c</span>; <span style="color: green">//4 bytes </span>}; <span style="color: blue">int </span><span style="color: #010001">_tmain</span>(<span style="color: blue">int </span><span style="color: #010001">argc</span>, <span style="color: #010001">_TCHAR</span>* <span style="color: #010001">argv</span>[]) { <span style="color: #010001">printf</span>( <span style="color: #a31515">"sizeof Struct__declspec_1 is %d.\n"</span>, <span style="color: blue">sizeof</span>( <span style="color: #010001">Struct__declspec_1 </span>)); <span style="color: green">//32 </span><span style="color: #010001">printf</span>( <span style="color: #a31515">"sizeof Struct__declspec_2 is %d.\n"</span>, <span style="color: blue">sizeof</span>( <span style="color: #010001">Struct__declspec_2 </span>)); <span style="color: green">//64 </span><span style="color: #010001">printf</span>( <span style="color: #a31515">"sizeof Struct__declspec_3 is %d.\n"</span>, <span style="color: blue">sizeof</span>( <span style="color: #010001">Struct__declspec_3 </span>)); <span style="color: green">//16 </span><span style="color: #010001">printf</span>( <span style="color: #a31515">"sizeof Struct__declspec_4 is %d.\n"</span>, <span style="color: blue">sizeof</span>( <span style="color: #010001">Struct__declspec_4 </span>)); <span style="color: green">//8 </span><span style="color: #010001">printf</span>( <span style="color: #a31515">"sizeof StructNormal is %d.\n"</span>, <span style="color: blue">sizeof</span>( <span style="color: #010001">StructNormal </span>)); <span style="color: green">//12 </span><span style="color: blue">return </span>0; } </pre><img src ="http://www.shnenglu.com/mymsdn/aggbug/121059.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2010-07-22 20:46 <a href="http://www.shnenglu.com/mymsdn/archive/2010/07/22/121059.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>[C++]鍐呭瓨綆$悊錛?錛?/title><link>http://www.shnenglu.com/mymsdn/archive/2010/07/06/memory_manager-1.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Tue, 06 Jul 2010 14:45:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2010/07/06/memory_manager-1.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/119492.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2010/07/06/memory_manager-1.html#Feedback</comments><slash:comments>17</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/119492.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/119492.html</trackback:ping><description><![CDATA[     鎽樿: 鍜屽ぇ澶氭暟鍐呭瓨綆$悊鐨勫垵琛蜂竴鑷達紝甯屾湜鑳藉鎺у埗鍐呭瓨鍒嗛厤鍜屽洖鏀訛紝鍑忓皯鍐呭瓨紕庣墖錛屼笖閫氬父榪欐牱鐨勫唴瀛橀兘浼氶寮涓孌佃繛緇唴瀛樼┖闂達紝鐒跺悗鎴戜滑鑷繁鏉ョ鐞嗚繖孌靛唴瀛樸傚綋鐒墮氬父榪欐牱鐨勯渶姹傞兘寰堝悎鐞嗭紝浣嗘槸瀹炵幇璧鋒潵鍒欓氬父涓嶈兘瀹岀編錛屾瘮濡傦細鏁堢巼銆佺畻娉曠殑閫夋嫨銆佸浣曞噺灝戝唴瀛樼鐗囥佽窡韙鐞嗗唴瀛樺垎閰嶃佹ц兘媯嫻嬨佸緋葷粺鍐呭瓨浣跨敤鐨勭粺璁°佸瀮鍦懼洖鏀剁瓑銆備笅闈㈡槸鎴戣繎鏈熷疄鐜扮殑涓涓潪甯哥畝闄嬬殑紼嬪簭錛岀敋鑷沖彲鑳借繛鍩烘湰鐨勮姹傞兘鏃犳硶杈懼埌錛屽ぇ瀹跺府蹇欑湅鐪嬶紝瀹冪┒绔熸湁澶氬皯緙虹偣...  <a href='http://www.shnenglu.com/mymsdn/archive/2010/07/06/memory_manager-1.html'>闃呰鍏ㄦ枃</a><img src ="http://www.shnenglu.com/mymsdn/aggbug/119492.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2010-07-06 22:45 <a href="http://www.shnenglu.com/mymsdn/archive/2010/07/06/memory_manager-1.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>緙栬瘧鏃х▼搴忕殑鏃跺欙紝鍙兘闇瑕佹墜鍔ㄥ叧闂璘AC閫夐」錛屽惁鍒欏彲鑳界▼搴忔棤娉曡繍琛?/title><link>http://www.shnenglu.com/mymsdn/archive/2010/07/02/119127.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Thu, 01 Jul 2010 17:14:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2010/07/02/119127.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/119127.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2010/07/02/119127.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/119127.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/119127.html</trackback:ping><description><![CDATA[<p>浠婂ぉ鍦╒S2008涓嬬紪璇慥S鑷甫Sample涓殑涓涓緥瀛愶紝TstCon錛屽洜涓轟箣鍓嶇殑鏈哄櫒瑁呯殑鏄疿P緋葷粺錛岃紼嬪簭鏇炬垚鍔熺紪璇戣繃錛屼笉榪囦粖澶╁湪Windows Server 2008 R2涓嬫墦寮澶辮觸錛屽湪鏈嬪弸鐨刉indows 7 Ultimate涓嬫墦寮涔熷け璐ワ紝璇曠敤浠ョ鐞嗗憳韜喚榪愯錛屽け璐ワ紝璇曠敤淇敼鍏煎鎬ч夐」涓篧indows XP SP3/SP2鏂瑰紡錛屽潎澶辮觸銆?/p> <p>鍏跺疄榪欎釜鏃跺欏彲鑳芥槸涓浜沄ista浠ヤ笂鐗堟湰鐨凮S鎵鎻愪緵鐨勬柊鍔熻兘寮曡搗鐨勯檺鍒躲傚氨褰撳墠鐨勮繖涓緥瀛愯岃█錛屾槸鍥犱負緙栬瘧鐨勬椂鍊欙紝鍚敤鐢ㄦ埛甯愭埛鎺у埗(UAC)榛樿涓衡滄槸鈥濇墍鑷達紝灝嗚В鍐蟲柟妗堝唴鎵鏈夊伐紼嬮変腑錛屽彸閿睘鎬э紝淇敼鈥滈厤緗睘鎬р?>鈥滈摼鎺ュ櫒鈥?>鈥滄竻鍗曟枃浠垛?>鈥滃惎鐢ㄧ敤鎴峰笎鎴鋒帶鍒?UAC)鈥濅負鈥滃惁鈥濓紝閲嶆柊鐢熸垚瑙e喅鏂規錛屽嵆鍙?/p> <p><a href="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/f6ec3fce6e75_FC5/image_2.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" class="wlDisabledImage" title="image" border="0" alt="image" src="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/f6ec3fce6e75_FC5/image_thumb.png" width="770" height="564"></a></p><img src ="http://www.shnenglu.com/mymsdn/aggbug/119127.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2010-07-02 01:14 <a href="http://www.shnenglu.com/mymsdn/archive/2010/07/02/119127.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>size_t鐨勫掑簭閬嶅巻闂http://www.shnenglu.com/mymsdn/archive/2010/06/30/119021.htmlvolnetvolnetWed, 30 Jun 2010 15:51:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/06/30/119021.htmlhttp://www.shnenglu.com/mymsdn/comments/119021.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/06/30/119021.html#Feedback11http://www.shnenglu.com/mymsdn/comments/commentRss/119021.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/119021.html鍑嗙‘鍦拌榪樻槸緇忛獙涓嶈凍錛岃繖涔堢畝鍗曠殑浜嬪眳鐒舵兂浜嗗ソ鍑犲垎閽燂紝褰撶劧涔熻鎬猇S鍦ㄦ病鏈夐噸鏂扮敓鎴愬墠鐨勮寮傜幇璞°?/p>

浠婃櫄鍦ㄧ被涓姞鍏ヤ袱涓暟緇勭敤鏉ュ仛璁℃暟錛屽洜涓轟箣鍓嶄唬鐮佹湁鎵鏀瑰姩錛孷S緙栬瘧錛堝閲忥級鐨勭粨鏋滃眳鐒跺嚭鐜頒簡鏃犳暟嬈$殑紼嬪簭宕╂簝錛屽鎴戜竴搴︽鐤戞槸涓嶆槸鎴戠殑鏁扮粍鍐欑殑鏈夐棶棰樸傚洤銆傛渶鍚庢棤濂堜箣涓嬶紝鐐逛簡閲嶆柊鐢熸垚錛屽眳鐒墮『鍒╅氳繃浜嗭紝寰堢敓姘旓紝鎰ゆ掍腑銆?/p>

浣嗘槸鍙﹀鍗村彂鐜頒簡涓涓棶棰橈紝涔熷氨鏄綋size_t鐢ㄤ綔寰幆鐨勬椂鍊欍傚洜涓轟互鍓嶉兘鏄敤int鍋氬驚鐜殑錛岀幇鍦ㄦ崲鎴恥nsigned int錛堜篃灝辨槸size_t錛夊悗錛屼竴涓嬪瓙娌″弽搴旇繃鏉ワ紝灝遍『鎵嬭繖涔堝啓浜嗭細

 

for( size_t i = MAX - 1; i >= 0; --i)

{

//鈥︹?/p>

}

涔嶄竴鐪嬩技涔庢病鍟ラ棶棰橈紝鍥犱負鎴戝驚鐜唴鐨勪唬鐮佹槸鍒犻櫎璧勬簮鐨勶紝鍥犳紼嬪簭涔熼棰戝穿婧冦?/p>

step over鐨勭粨鏋滄墠璁╀漢鎯婅錛屽洜涓哄綋size_t i = 0鐨勬椂鍊欙紝--i鐨勭粨鏋滄槸鏃犵┓澶э紝鑰屾棤絀峰ぇ鍒欒偗瀹氭弧瓚砳>=0鐨勬潯浠訛紝鎵浠ュ綋鎴戜滑鏈熷緟紼嬪簭鍋滀綇鐨勬椂鍊欙紝紼嬪簭鏄笉浼氬仠浣忕殑銆?/p>

淇鐨勬柟寮忥細

1銆佷嬌鐢ㄦ鍚戦亶鍘嗐?/p>

2銆佸鍔犲垽鏂潯浠訛紙i>=0 && i < MAX錛夛紝浣嗚繖閲屼篃鍙兘瀛樺湪闂錛屽洜涓簊ize_t鍙兘琚畾涔変負unsigned int錛屼絾鏄疢AX鍙兘鏄釜鏇村ぇ鐨勬暟錛屾瘮濡倁nsigned long long錛屽綋鐒惰繖鏍風殑姣旇緝涓嶆槸寰堟湁鎰忎箟錛屾垨鑰呬細瀹炵幇涓浜涜漿鎹紝浣嗘槸濡傛灉榪欑鎯呭喌鍙戠敓鐨勮瘽錛岀▼搴忓彲鑳借繕鏄細閫氳繃涓涓殢鏈虹殑i榪涘叆鍒頒竴涓湭鐭ョ殑絀洪棿涓紝浠庤岄犳垚宕╂簝銆傝屼笖澧炲姞鍒ゆ柇鏉′歡涔熶嬌寰楃▼搴忕殑榪愯鎴愭湰鎻愰珮銆?/p>

volnet 2010-06-30 23:51 鍙戣〃璇勮
]]>
C++ Traitshttp://www.shnenglu.com/mymsdn/archive/2010/04/26/cpp-traits.htmlvolnetvolnetMon, 26 Apr 2010 07:31:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/04/26/cpp-traits.htmlhttp://www.shnenglu.com/mymsdn/comments/113597.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/04/26/cpp-traits.html#Feedback2http://www.shnenglu.com/mymsdn/comments/commentRss/113597.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/113597.html鏈潵鎯沖ソ濂藉啓鍐機++ Traits鐨勶紝鍒氬垰鎺ュ埌interview鐨勯氱煡錛屽厛璐翠唬鐮侊紝鏀瑰ぉ鍐嶈銆?/p>
/*
 * cpp_traits.cpp
 *
 *  Created on: 2010-4-26
 *      Author: volnet@tom.com
 */
#include <iostream>
// kinds of types for overloading.
struct undefined_type {};
struct int32_type {};
struct int64_type {};

// typedef some has_trivial_* for difference types.
template <class T>
struct type_traits {
    typedef undefined_type has_trivial_type;
};

// define the partial specialization functions.
template <>
struct type_traits<int> {
    typedef int32_type has_trivial_type;
};
template <>
struct type_traits<long> {
    typedef int64_type has_trivial_type;
};

// the dispatcher method for all kinds of types.
template <class T>
void type_detect(T& p){
    typedef typename type_traits<T>::has_trivial_type trivial_type;
    type_detect(p, trivial_type());
}

// define the functions for dispatching.
template <class T>
void type_detect(T& p, undefined_type) {
    std::cout << p;
    std::cout << " // It's a undefined type, we have NOT found the dispatcher function." << std::endl;
}
template <class T>
void type_detect(T& p, int32_type) {
    std::cout << p;
    std::cout << " // It's a int32" << std::endl;
}
template <class T>
void type_detect(T& p, int64_type) {
    std::cout << p;
    std::cout << " // It's a int64" << std::endl;
}

int main(void) {
    int int32num = 2010;
    type_detect(int32num);

    long int64num = 2010L;
    type_detect(int64num);

    std::string str = "2010";
    type_detect(str);

    std::cout << "-------end of program." << std::endl;
    return EXIT_SUCCESS;
}


volnet 2010-04-26 15:31 鍙戣〃璇勮
]]>
[緲昏瘧]楂樻晥浣跨敤auto_ptrhttp://www.shnenglu.com/mymsdn/archive/2010/04/07/Using-auto_ptr-Effectively.htmlvolnetvolnetWed, 07 Apr 2010 11:08:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/04/07/Using-auto_ptr-Effectively.htmlhttp://www.shnenglu.com/mymsdn/comments/111882.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/04/07/Using-auto_ptr-Effectively.html#Feedback10http://www.shnenglu.com/mymsdn/comments/commentRss/111882.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/111882.html鏈枃鏉ヨ嚜C/C++鐢ㄦ埛鏃ュ織錛?7錛?0錛夛紝1999騫?0鏈?/i>  鍘熸枃閾炬帴

澶ч儴鍒嗕漢閮藉惉璇磋繃auto_ptr鎸囬拡錛屼絾鏄茍闈炴墍鏈変漢閮芥瘡澶╀嬌鐢ㄥ畠銆備笉浣跨敤瀹冩槸涓嶆槑鏅虹殑錛堝彲鑰葷殑錛夛紝鍥犱負auto_ptr鐨勮璁″垵琛鋒槸涓轟簡瑙e喅C++璁捐鍜岀紪鐮佺殑鏅亶闂錛屽皢瀹冪敤濂藉彲浠ュ啓鍑烘洿鍋ュ.鐨勪唬鐮併傛湰鏂囨寚鍑哄浣曟紜嬌鐢╝uto_ptr浠ヤ嬌紼嬪簭鍙樺緱瀹夊叏錛屼互鍙婂浣曢伩寮鍗遍櫓錛岃屼笉鏄竴鑸嬌鐢╝uto_ptr鐨勬伓涔犳墍鑷寸殑鍒涘緩闂存瓏鎬у拰闅句互璇婃柇鐨勯棶棰樸?

涓轟粈涔堝畠鏄竴涓滆嚜鍔ㄢ濇寚閽?

auto_ptr鍙槸璁歌澶氬鏅鴻兘鎸囬拡涓殑涓縐嶃傝澶氬晢涓氬簱鎻愪緵璁稿鏇村己澶х殑鏅鴻兘鎸囬拡錛屽彲浠ュ畬鎴愭洿澶氱殑浜嬫儏銆備粠鍙互綆$悊寮曠敤璁℃暟鍒版彁渚涙洿鍏堣繘鐨勪唬鐞嗘湇鍔$瓑銆傚簲璇ユ妸auto_ptr璁や負鏄櫤鑳芥寚閽堜腑鐨勭鐗笶scort[娉ㄩ噴]錛氫竴涓熀浜庣畝鍗曚笖閫氱敤鐩殑鐨勬櫤鑳芥寚閽堬紝鏃㈡病鏈夊皬鍙戞槑涔熸病鏈変赴瀵岀殑鐗規畩鐩殑鏇翠笉闇瑕侀珮鎬ц兘錛屼絾鏄兘灝嗚澶氭櫘閫氱殑浜嬫儏鍋氬ソ錛屽茍涓旇兘澶熼傚悎鏃ュ父浣跨敤鐨勬櫤鑳芥寚閽堛?

auto_ptr鍋氳繖鏍蜂竴浠朵簨錛氭嫢鏈変竴涓姩鎬佸垎閰嶅唴瀛樺璞★紝騫朵笖鍦ㄥ畠涓嶅啀闇瑕佺殑鏃跺欏飽琛岃嚜鍔ㄦ竻鐞嗙殑鑱岃矗銆傝繖閲屾湁涓病鏈変嬌鐢╝uto_ptr鎸囬拡鐨勪笉瀹夊叏鐨勪緥瀛愶細

    // Example 1(a): Original code
    //
    void f()
    {
      T* pt( new T );

      /*...more code...*/

      delete pt;
    }

鎴戜滑姣忓ぉ閮藉儚榪欐牱鍐欎唬鐮侊紝濡傛灉f()鍙槸涓涓笁琛岀▼搴忥紝涔熸病鍋氫粈涔堝浣欑殑浜嬫儏錛岃繖鏍峰仛褰撶劧鍙互寰堝ソ宸ヤ綔銆備絾鏄鏋渇()娌℃湁鎵цdelete璇彞錛屾瘮濡傜▼搴忔彁鍓嶈繑鍥烇紙return錛変簡錛屾垨鑰呭湪鎵ц鐨勬椂鍊欐姏鍑哄紓甯鎬簡錛岀劧鍚庡氨瀵艱嚧宸茬粡鍒嗛厤鐨勫璞℃病鏈夎鍒犻櫎錛屽洜姝ゆ垜浠氨鏈変簡涓涓粡鍏哥殑鍐呭瓨娉勬紡銆?

涓涓嬌Example錛?錛夊畨鍏ㄧ殑鍔炴硶鏄敤涓涓滄櫤鑳解濈殑鎸囬拡鎷ユ湁榪欎釜鎸囬拡錛屽綋閿姣佺殑鏃跺欙紝鍒犻櫎閭d釜琚寚鐨勮嚜鍔ㄥ垎閰嶇殑瀵硅薄銆傚洜涓鴻繖涓櫤鑳芥寚閽堣綆鍗曞湴鐢ㄤ負鑷姩瀵硅薄錛堣繖灝辨槸錛屽綋瀹冪寮瀹冪殑浣滅敤鍩熺殑鏃跺欒嚜鍔ㄩ攢姣佸璞★級錛屾墍浠ュ畠琚О浣溾滆嚜鍔ㄢ濇寚閽堛?

    // Example 1(b): Safe code, with auto_ptr
    //
    void f()
    {
      auto_ptr<T> pt( new T );

      /*...more code...*/

    } // cool: pt's destructor is called as it goes out
      // of scope, and the object is deleted automatically

鐜板湪榪欐浠g爜灝嗕笉浼氬啀T瀵硅薄涓婂彂鐢熸硠婕忎簡錛屼笉蹇呭湪鎰忚繖涓柟娉曟槸姝e父閫鍑鴻繕鏄紓甯擱鍑猴紝鍥犱負pt鐨勬瀽鏋勫嚱鏁板皢鎬繪槸鍦ㄥ爢鏍堝脊鍑虹殑鏃跺欒璋冪敤銆傛竻鐞嗗伐浣滃皢鑷姩榪涜銆?/p>

鏈鍚庯紝浣跨敤auto_ptr鍜屼嬌鐢ㄥ唴寤烘寚閽堜竴鏍峰湴瀹規槗錛屽鏋滆鈥滄敹鍥炩濊祫婧愬茍涓斿啀嬈℃墜鍔ㄧ鐞嗙殑璇濓紝鎴戜滑鍙互璋冪敤release()錛?/p>

    // Example 2: Using an auto_ptr
    //
    void g()
    {
      T* pt1 = new T;
      // right now, we own the allocated object

      // pass ownership to an auto_ptr
      auto_ptr<T> pt2( pt1 );

      // use the auto_ptr the same way
      // we'd use a simple pointer
      *pt2 = 12;       // same as "*pt1 = 12;"
      pt2->SomeFunc(); // same as "pt1->SomeFunc();"

      // use get() to see the pointer value
      assert( pt1 == pt2.get() );

      // use release() to take back ownership
      T* pt3 = pt2.release();

      // delete the object ourselves, since now
      // no auto_ptr owns it any more
      delete pt3;

    } // pt2 doesn't own any pointer, and so won't
      // try to delete it... OK, no double delete

鏈鍚庯紝鎴戜滑鍙互浣跨敤auto_ptr鐨剅eset()鏂規硶灝哸uto_ptr閲嶇疆鍚戝彟涓涓璞°傚鏋渁uto_ptr宸茬粡鑾峰緱涓涓璞★紝榪欎釜榪囩▼灝卞儚鏄畠鍏堝垹闄ゅ凡緇忔嫢鏈夌殑瀵硅薄錛屽洜姝よ皟鐢╮eset()錛屽氨鍍忔槸鍏堥攢姣佷簡auto_ptr錛岀劧鍚庨噸寤轟簡涓涓柊鐨勫茍鎷ユ湁璇ユ柊瀵硅薄錛?

    // Example 3: Using reset()
    //
    void h()
    {
      auto_ptr<T> pt( new T(1) );

      pt.reset( new T(2) );
        // deletes the first T that was
        // allocated with "new T(1)"

    } // finally, pt goes out of scope and
      // the second T is also deleted

鍖呰鎸囬拡鏁版嵁鎴愬憳

鍚屾牱錛宎uto_ptr涔熷彲浠ヨ鐢ㄤ簬瀹夊叏鍦板寘瑁呮寚閽堟暟鎹垚鍛樸傝冭檻涓嬮潰浣跨敤Pimpl idiom錛堟垨鑰咃紝緙栬瘧鍣ㄩ槻鐏錛夌殑渚嬪瓙錛?sup>[1]

    // Example 4(a): A typical Pimpl
    //

    // file c.h
    //
    class C
    {
    public:
      C();
      ~C();
      /*...*/
    private:
      class CImpl; // forward declaration
      CImpl* pimpl_;
    };

    // file c.cpp
    //
    class C::CImpl { /*...*/ };

    C::C() : pimpl_( new CImpl ) { }
    C::~C() { delete pimpl_; }

綆鍗曞湴璇達紝灝辨槸C鐨勭鏈夌粏鑺傝瀹炵幇涓轟竴涓崟鐙殑瀵硅薄錛岃棌鍖夸簬涓涓寚閽堜箣涓傝鎬濊礬瑕佹眰C鐨勬瀯閫犲嚱鏁拌礋璐d負闅愯棌鍦ㄧ被鍐呴儴鐨勮緟鍔┾淧impl鈥濆璞″垎閰嶅唴瀛橈紝騫朵笖C鐨勬瀽鏋勫嚱鏁拌礋璐i攢姣佸畠銆備嬌鐢╝uto_ptr錛屾垜浠細鍙戠幇榪欓潪甯稿鏄擄細

    // Example 4(b): A safer Pimpl, using auto_ptr
    //

    // file c.h
    //
    class C
    {
    public:
      C();
      /*...*/
    private:
      class CImpl; // forward declaration
      auto_ptr<CImpl> pimpl_;
    };

    // file c.cpp
    //
    class C::CImpl { /*...*/ };

    C::C() : pimpl_( new CImpl ) { }

鐜板湪錛屾瀽鏋勫嚱鏁頒笉闇瑕佹媴蹇冨垹闄impl_鎸囬拡浜嗭紝鍥犱負auto_ptr灝嗚嚜鍔ㄥ鐞嗗畠銆備簨瀹炰笂錛屽鏋滄病鏈夊叾瀹冮渶瑕佹樉寮忓啓鏋愭瀯鍑芥暟鐨勫師鍥狅紝鎴戜滑瀹屽叏涓嶉渶瑕佽嚜瀹氫箟鏋愭瀯鍑芥暟銆傛樉鐒訛紝榪欐瘮鎵嬪姩綆$悊鎸囬拡瑕佸鏄撳緱澶氾紝騫朵笖灝嗗璞℃墍鏈夋潈鍖呭惈榪涘璞℃槸涓涓笉閿欑殑涔犳儻錛岃繖姝f槸auto_ptr鎵鎿呴暱鐨勩傛垜浠皢鍦ㄦ渶鍚庡啀嬈″洖欏捐繖涓緥瀛愩?

鎵鏈夋潈錛屾簮錛屼互鍙婅皟鐢ㄨ?Sinks)

瀹冩湰韜緢婕備寒錛屽茍涓斿仛寰楅潪甯稿ソ錛氫粠鍑芥暟浼犲叆鎴栦紶鍑?span style="font-family: 'Courier New'" class="Code">auto_ptrs錛屾槸闈炲父鏈夌敤鐨勶紝姣斿鍑芥暟鐨勫弬鏁版垨鑰呰繑鍥炲箋?/p>

璁╂垜浠湅鐪嬩負浠涔堬紝棣栧厛鎴戜滑鑰冭檻褰撴嫹璐漚uto_ptr鐨勬椂鍊欎細鍙戠敓浠涔堬細涓涓猘uto_ptr鑾峰緱涓涓嫢鏈夋寚閽堢殑瀵硅薄錛屽茍涓斿湪鍚屼竴鏃墮棿鍙厑璁告湁涓涓猘uto_ptr鍙互鎷ユ湁榪欎釜瀵硅薄銆傚綋浣犳嫹璐濅竴涓猘uto_ptr鐨勬椂鍊欙紝浣犺嚜鍔ㄥ皢婧恆uto_ptr鐨勬墍鏈夋潈錛屼紶閫掔粰鐩爣auto_ptr錛涘鏋滅洰鏍嘺uto_ptr宸茬粡鎷ユ湁浜嗕竴涓璞★紝榪欎釜瀵硅薄灝嗗厛琚噴鏀俱傚湪鎷瘋礉瀹屼箣鍚庯紝鍙湁鐩爣auto_ptr鎷ユ湁鎸囬拡錛屽茍涓旇礋璐e湪鍚堥傜殑鏃墮棿閿姣佸畠錛岃屾簮灝嗚璁劇疆涓虹┖錛坣ull錛夛紝騫朵笖涓嶈兘鍐嶈褰撲綔鍘熸湁鎸囬拡鐨勪唬琛ㄦ潵浣跨敤銆?

渚嬪錛?/p>

    // Example 5: Transferring ownership from
    //            one auto_ptr to another
    //
    void f()
    {
      auto_ptr<T> pt1( new T );
      auto_ptr<T> pt2;

      pt1->DoSomething(); // OK

      pt2 = pt1;  // now pt2 owns the pointer,
                  // and pt1 does not

      pt2->DoSomething(); // OK

    } // as we go out of scope, pt2's destructor
      // deletes the pointer, but pt1's does nothing

浣嗘槸瑕侀伩鍏嶉櫡闃卞啀嬈′嬌鐢ㄥ凡緇忓け鍘繪墍鏈夋潈鐨刟uto_ptr錛?

    // Example 6: Never try to do work through
    //            a non-owning auto_ptr
    //
    void f()
    {
      auto_ptr<T> pt1( new T );
      auto_ptr<T> pt2;

      pt2 = pt1;  // now pt2 owns the pointer, and
                  // pt1 does not

      pt1->DoSomething();
                  // error! following a null pointer
    }

璋ㄨ浜庡績錛屾垜浠幇鍦ㄧ湅鐪媋uto_ptr濡備綍鍦ㄦ簮鍜岃皟鐢ㄨ呬箣闂村伐浣溿傗滄簮鈥濊繖閲屾槸鎸囦竴涓嚱鏁幫紝鎴栬呭叾瀹冨垱寤轟竴涓柊璧勬簮鐨勬搷浣滐紝騫朵笖閫氬父灝嗙Щ浜ゅ嚭璧勬簮鐨勬墍鏈夋潈銆備竴涓滆皟鐢ㄨ呪濆嚱鏁板弽杞繖涓叧緋伙紝涔熷氨鏄幏寰楀凡緇忓瓨鍦ㄥ璞$殑鎵鏈夋潈錛堝茍涓旈氬父榪樿礋璐i噴鏀懼畠錛夈傝屼笉鏄湁涓涓簮鍜岃皟鐢ㄨ咃紝榪斿洖騫朵笖鍒╃敤涓涓澶存寚閽堬紙璇戣呮敞錛氳屼笉鏄嬌鐢ㄤ竴涓眬閮ㄥ彉閲忔潵浼犻掕繖涓寚閽堬級錛岃櫧鐒訛紝閫氳繃涓涓澶存寚閽堟潵鑾峰緱涓涓祫婧愰氬父寰堝ソ錛?

    // Example 7: Sources and sinks
    //

    // A creator function that builds a new
    // resource and then hands off ownership.
    //
    auto_ptr<T> Source()
    {
      return auto_ptr<T>( new T );
    }

    // A disposal function that takes ownership
    // of an existing resource and frees it.
    //
    void Sink( auto_ptr<T> pt )
    {
    }

    // Sample code to exercise the above:
    auto_ptr<T> pt( Source() ); // takes ownership

娉ㄦ剰涓嬮潰鐨勫井濡欑殑鍙樺寲錛?

  1. Source()鍒嗛厤浜嗕竴涓柊瀵硅薄騫朵笖浠ヤ竴涓畬鏁村畨鍏ㄧ殑鏂瑰紡灝嗗畠榪斿洖緇欒皟鐢ㄨ咃紝騫惰璋冪敤鑰呮垚涓烘寚閽堢殑鎷ユ湁鐫銆傚嵆浣胯皟鐢ㄨ呭拷鐣ヤ簡榪斿洖鍊鹼紙鏄劇劧錛屽鏋滆皟鐢ㄨ呭拷鐣ヤ簡榪斿洖鍊鹼紝浣犲簲璇ヤ粠鏉ユ病鏈夊啓榪囦唬鐮佹潵鍒犻櫎榪欎釜瀵硅薄錛屽鍚э紵錛夛紝鍒嗛厤鐨勫璞′篃灝嗚鑷姩瀹夊叏鍦板垹闄ゃ?/p>

    鍦ㄦ湰鏂囩殑鏈鍚庯紝鎴戝皢婕旂ず榪斿洖涓涓猘uto_ptr鏄竴涓ソ涔犳儻銆傝榪斿洖鍊煎寘瑁硅繘涓浜涗笢瑗挎瘮濡俛uto_ptr閫氬父鏄嬌寰楀嚱鏁板彉寰楀己鍋ョ殑鏈夋晥鏂瑰紡銆?

  2. Sink()閫氳繃浼犲肩殑鏂瑰紡鑾峰緱瀵硅薄鎵鏈夋潈銆傚綋鎵ц瀹孲ink()鐨勬椂鍊欙紝褰撶寮浣滅敤鍩熺殑鏃跺欙紝鍒犻櫎鎿嶄綔灝嗚鎵ц錛堝彧瑕丼ink()娌℃湁灝嗘墍鏈夋潈杞Щ錛夈備笂闈㈡墍鍐欑殑Sink()鍑芥暟瀹為檯涓婂茍娌℃湁瀵瑰弬鏁板仛浠諱綍浜嬫儏錛屽洜姝よ皟鐢ㄢ淪ink(pt);鈥濆氨絳変簬鍐欎簡鈥減t.reset(0);鈥濓紝浣嗘槸澶ч儴鍒嗙殑Sink鍑芥暟閮藉皢鍦ㄩ噴鏀懼畠涔嬪墠鍋氫竴浜涘伐浣溿?

涓嶅彲浠ュ仛鐨勪簨鎯咃紝浠ュ強涓轟粈涔堜笉鑳藉仛

璋ㄨ錛氬崈涓囦笉瑕佷互鎴戜箣鍓嶆病鏈夋彁鍒扮殑鏂瑰紡浣跨敤auto_ptrs銆傛垜宸茬粡鐪嬭榪囧緢澶氱▼搴忓憳璇曠潃鐢ㄥ叾浠栨柟寮忓啓auto_ptrs灝卞儚浠栦滑鍦ㄤ嬌鐢ㄥ叾瀹冨璞′竴鏍楓備絾闂鏄痑uto_ptr騫朵笉鍍忓叾浠栧璞°傝繖閲屾湁浜涘熀鏈師鍒欙紝鎴戝皢鎶婂畠浠彁鍑烘潵浠ュ紩璧蜂綘鐨勬敞鎰忥細

For auto_ptr, copies are NOT equivalent. 錛堝鍒禷uto_ptr灝嗕笌鍘熸潵鐨勪笉鐩哥瓑錛?

褰撲綘璇曠潃鍦ㄤ竴鑸殑浠g爜涓嬌鐢?span style="font-family: 'Courier New'" class="Code">auto_ptrs鐨勬椂鍊欙紝瀹冨皢鎵ц鎷瘋礉錛屽茍涓旀病鏈変換浣曟彁紺猴紝鎷瘋礉鏄笉鐩哥瓑鐨勶紙緇撴灉錛屽畠紜疄灝辨槸鎷瘋礉錛夈傜湅涓嬮潰榪欐浠g爜錛岃繖鏄垜鍦–++鏂伴椈緇勭粡甯哥湅瑙佺殑錛?

    // Example 8: Danger, Will Robinson!
    //
    vector< auto_ptr<T> > v;

    /* ... */

    sort( v.begin(), v.end() );

鍦ㄦ爣鍑嗗鍣ㄤ腑浣跨敤auto_ptrs鎬繪槸涓嶅畨鍏ㄧ殑銆備竴浜涗漢鍙兘瑕佸憡璇変綘錛屼粬浠殑緙栬瘧鍣ㄦ垨鑰呯被搴撹兘澶熷緢濂藉湴緙栬瘧瀹冧滑錛岃屽彟涓浜涗漢鍒欏憡璇変綘鍦ㄦ煇涓涓祦琛岀殑緙栬瘧鍣ㄧ殑鏂囨。涓湅鍒拌繖涓緥瀛愶紝涓嶈鍚粬浠殑銆?

闂鏄?span style="font-family: 'Courier New'" class="Code">auto_ptr騫朵笉瀹屽叏絎﹀悎涓涓彲浠ユ斁榪涘鍣ㄧ被鍨嬬殑鍓嶆彁錛屽洜涓烘嫹璐?span style="font-family: 'Courier New'" class="Code">auto_ptrs鏄笉絳変環鐨勩傞鍏堬紝娌℃湁浠諱綍涓滆タ璇存槑錛寁ector涓嶈兘鍐沖畾澧炲姞騫跺埗閫犲嚭鈥滄墿灞曗濈殑鍐呴儴鎷瘋礉銆傚啀嬈★紝褰撲綘璋冪敤涓涓竴鑸嚱鏁扮殑鏃跺欙紝瀹冨彲鑳戒細鎷瘋礉鍏冪礌錛屽氨鍍弒ort()閭f牱錛屽嚱鏁板繀欏繪湁鑳藉姏鍋囪鎷瘋礉鏄瓑浠風殑銆傝嚦灝戜竴涓祦琛岀殑鎺掑簭鎷瘋礉鈥滄牳蹇冣濈殑鍏冪礌錛屽鏋滀綘璇曠潃璁╁畠涓?span style="font-family: 'Courier New'" class="Code">auto_ptrs涓璧峰伐浣滅殑璇濓紝瀹冨皢鎷瘋礉涓浠解滄牳蹇冣濈殑auto_ptr瀵硅薄錛堝洜姝よ漿縐繪墍鏈夋潈騫朵笖灝嗘墍鏈夋潈杞Щ緇欎竴涓復鏃跺璞★級錛岀劧鍚庡鍏朵綑鐨勫厓绱犱篃閲囧彇鐩稿悓鐨勬柟寮忥紙浠庣幇鏈夋垚鍛樺垱寤烘洿澶氱殑鎷ユ湁鎵鏈夋潈鐨刟uto_ptr錛夛紝褰撴帓搴忓畬鎴愬悗錛屾牳蹇冨厓绱犲皢琚攢姣侊紝騫朵笖浣犲皢閬囧埌涓涓棶棰橈細榪欑粍搴忓垪閲岃嚦灝戜竴涓猘uto_ptr錛堜篃灝辨槸鍒氭墠琚帀鍖呯殑閭d釜鏍稿績鍏冪礌錛変笉鍐嶆嫢鏈夊璞℃墍鏈夋潈錛岃岄偅涓湡瀹炵殑鎸囬拡宸茬粡闅忕潃涓存椂瀵硅薄鐨勯攢姣佽岃鍒犻櫎浜嗭紒

浜庢槸鏍囧噯濮斿憳浼氬洖閫騫跺笇鏈涘仛涓浜涜兘澶熷府鍔╀綘閬垮厤榪欎簺琛屼負鐨勪簨鎯咃細鏍囧噯鐨刟uto_ptr琚晠鎰忚璁℃垚褰撲綘甯屾湜鍦ㄤ嬌鐢ㄦ爣鍑嗗鍣ㄧ殑鏃跺欎嬌鐢ㄥ畠鏃舵墦鏂綘錛堟垨鑰咃紝鑷沖皯錛屽湪澶ч儴鍒嗙殑鏍囧噯搴撳疄鐜頒腑鎵撴柇浣狅級銆備負浜嗚揪鍒拌繖涓洰鐨勶紝鏍囧噯濮斿憳浼氬埄鐢ㄨ繖鏍蜂竴涓妧宸э細璁?span style="font-family: 'Courier New'" class="Code">auto_ptr's鐨勬嫹璐濇瀯閫犲嚱鏁板拰璧嬪兼搷浣滅鐨勫彸鍊鹼紙rhs錛夋寚鍚戦潪甯擱噺銆傚洜涓烘爣鍑嗗鍣ㄧ殑鍗曞厓绱爄nsert()鍑芥暟錛岄渶瑕佷竴涓父閲忎綔涓哄弬鏁幫紝鍥犳auto_ptrs鍦ㄨ繖閲屽氨涓嶅伐浣滀簡銆傦紙璇戣呮敞錛氬彸鍊間笉鑳借祴鍊肩粰闈炲父閲忥級

浣跨敤const auto_ptr鏄竴涓ソ涔犳儻

灝嗕竴涓猘uto_ptr璁捐鎴?span style="font-family: 'Courier New'" class="Code">const auto_ptrs灝嗕笉鍐嶄涪澶辨墍鏈夋潈錛氭嫹璐濅竴涓猚onst auto_ptr鏄繚娉曠殑錛堣瘧鑰呮敞錛氭病鏈夎繖鏍風殑鏋勯犲嚱鏁幫級錛屽疄闄呬笂浣犲彲浠ラ拡瀵瑰畠鍋氱殑鍞竴浜嬫儏灝辨槸閫氳繃operator*()鎴栬卭perator->()瑙e紩鐢ㄥ畠鎴栬呰皟鐢╣et()鏉ヨ幏寰楁墍鍖呭惈鐨勬寚閽堢殑鍊箋傝繖鎰忓懗鐫鎴戜滑鏈変竴涓畝鍗曟槑浜嗙殑椋庢牸鏉ヨ〃杈句竴涓粷涓嶄涪澶辨墍鏈夋潈鐨刟uto_ptr錛?

    // Example 9: The const auto_ptr idiom
    //
    const auto_ptr<T> pt1( new T );
        // making pt1 const guarantees that pt1 can
        // never be copied to another auto_ptr, and
        // so is guaranteed to never lose ownership

    auto_ptr<T> pt2( pt1 ); // illegal
    auto_ptr<T> pt3;
    pt3 = pt1;              // illegal
    pt1.release();          // illegal
    pt1.reset( new T );     // illegal

榪欏氨鏄垜瑕佽鐨刢osnt錛佸洜姝ゅ鏋滅幇鍦ㄤ綘瑕佸悜涓栫晫璇佹槑浣犵殑auto_ptr鏄笉浼氳鏀瑰彉騫朵笖灝嗘繪槸鍒犻櫎鍏舵墍鏈夋潈錛屽姞涓奵onst灝辨槸浣犺鍋氱殑銆俢onst auto_ptr椋庢牸鏄湁鐢ㄧ殑錛屼綘蹇呴』灝嗗畠璋ㄨ浜庡績銆?

auto_ptr浠ュ強寮傚父瀹夊叏

鏈鍚庯紝auto_ptr瀵瑰啓鍑哄紓甯稿畨鍏ㄧ殑浠g爜鏈夋椂鍊欓潪甯稿繀瑕侊紝鎬濊冧笅闈㈢殑浠g爜錛?/p>

    // Example 10(a): Exception-safe?
    //
    String f()
    {
      String result;
      result = "some value";
      cout << "some output";
      return result;
    }

璇ュ嚱鏁版湁涓や釜鍙鐨勪綔鐢細瀹冭緭鍑轟竴浜涘唴瀹癸紝騫朵笖榪斿洖涓涓猄tring銆傚叧浜庡紓甯稿畨鍏ㄧ殑璇︾粏璇存槑瓚呭嚭浜嗘湰鏂囩殑鑼冨洿[2]錛屼絾鏄垜浠兂瑕佸彇寰楃殑鐩爣灝辨槸寮哄紓甯稿畨鍏ㄧ殑淇濋殰錛屽綊緇撲負紜繚鍑芥暟鐨勫師瀛愭р斺斿鏋滄湁寮傚父錛屾墍鏈夌殑浣滅敤涓璧峰彂鐢熸垨鑰呴兘涓嶅彂鐢熴?

铏界劧鍦ㄤ緥10(a)涓殑浠g爜闈炲父綺懼閥錛岀湅璧鋒潵鐩稿綋鎺ヨ繎浜庡紓甯稿畨鍏ㄧ殑浠g爜錛屼絾浠嶇劧鏈変竴浜涘皬鐨勭憰鐤碉紝灝卞儚涓嬮潰鐨勫鎴蜂唬鐮佹墍紺猴細

    String theName;
    theName = f();

鍥犱負緇撴灉閫氳繃鍊艱繑鍥烇紝鍥犳String鐨勬嫹璐濇瀯閫犲嚱鏁板皢琚皟鐢紝鑰屾嫹璐濊祴鍊兼搷浣滅琚皟鐢ㄦ潵灝嗙粨鏋滄嫹璐濆埌theName涓傚鏋滀換浣曚竴涓嫹璐濆け璐ヤ簡錛宖()灝卞畬鎴愪簡鎵鏈夊畠鐨勫伐浣滀互鍙婃墍鏈夊畠鐨勪換鍔★紙榪欏緢濂斤級錛屼絾鏄粨鏋滄槸鏃犳硶鎸藉洖鐨勶紙鍝庡摕鎴戠殑濡堝憖錛?

鎴戜滑鍙互鍋氱殑鏇村ソ鍚楋紝鏄惁鍙互閫氳繃閬垮厤鎷瘋礉鏉ラ伩鍏嶈繖涓棶棰橈紵渚嬪錛屾垜浠彲浠?璁╁嚱鏁版湁涓涓潪甯擱噺寮曠敤鍙傛暟騫跺悜涓嬮潰榪欐牱榪斿洖鍊鹼細

    // Example 10(b): Better?
    //
    void f( String& result )
    {
      cout << "some output";
      result = "some value";
    }

榪欑湅璧鋒潵寰堟錛屼絾瀹為檯涓嶆槸榪欐牱鐨勶紝榪斿洖result鐨勮祴鍊肩殑鍑芥暟鍙畬鎴愪簡涓涓姛鑳斤紝鑰屽皢鍏跺畠浜嬫儏鐣欑粰浜嗘垜浠傚畠浠嶇劧浼氬嚭閿欍傚洜姝よ繖涓仛娉曚笉鍙彇銆?

瑙e喅榪欎釜闂鐨勪竴涓柟娉曟槸榪斿洖涓涓寚鍚戝姩鎬佸垎閰嶆寚閽堢殑String瀵硅薄錛屼絾鏄渶濂界殑瑙e喅鏂規鏄鎴戜滑鍋氱殑鏇村錛岃繑鍥炰竴涓寚閽堝寘鍚湪auto_ptr錛?

    // Example 10(c): Correct (finally!)
    //
    auto_ptr<String> f()
    {
      auto_ptr<String> result = new String;
      *result = "some value";
      cout << "some output";
      return result;  // rely on transfer of ownership;
                      // this can't throw
    }

榪欓噷鏄竴涓妧宸э紝褰撴垜浠湁鏁堥殣钘忔墍鏈夌殑宸ヤ綔鏉ユ瀯閫犵浜屼釜鍔熻兘錛堣繑鍥炲鹼級褰撶‘淇濆畠鍙互琚畨鍏ㄨ繑鍥炵粰璋冪敤鑰呭茍涓斿湪絎竴涓姛鑳斤紙鎵撳嵃娑堟伅錛夊畬鎴愮殑鏃跺欐病鏈夋姏鍑烘搷浣溿傛垜浠煡閬撲竴鏃out瀹屾垚錛岃繑鍥炲煎皢鎴愬姛浜ゅ埌璋冪敤鑰呮墜涓紝騫朵笖鏃犺濡備綍閮戒細姝g‘娓呯悊錛氬鏋滆皟鐢ㄨ呮帴鍙楄繑鍥炲鹼紝璋冪敤鑰呭皢寰楀埌榪欎釜鎷瘋礉鐨刟uto_ptr涓存椂瀵硅薄鐨勬墍鏈夋潈錛涘鏋滆皟鐢ㄨ呮病鏈夋帴鍙楄繑鍥炲鹼紝涔熷氨鏄拷鐣ヨ繑鍥炲鹼紝鍒嗛厤鐨凷tring灝嗗湪涓存椂auto_ptr琚攢姣佺殑鏃跺欒嚜鍔ㄦ竻鐞嗐傝繖縐嶅畨鍏ㄦ墿灞曠殑浠d環鍛紵灝卞儚鎴戜滑緇忓父瀹炵幇鐨勫己寮傚父瀹夊叏涓鏍鳳紝寮哄畨鍏ㄩ氬父娑堣椾竴浜涙晥鐜囷紙閫氬父姣旇緝灝忥級鈥斺旇繖閲屾寚棰濆鐨勫姩鎬佸唴瀛樺垎閰嶃備絾鏄綋鎴戜滑鍦ㄦ晥鐜囧拰姝g‘鎬т箣闂村仛鍑洪夋嫨鐨勮瘽錛屾垜浠氬父浼氶夋嫨鍚庤咃紒

璁╂垜浠吇鎴愬湪鏃ュ父宸ヤ綔涓嬌鐢╝uto_ptr鐨勪範鎯俛uto_ptr瑙e喅浜嗗父瑙佺殑闂錛屽茍涓旇兘澶熶嬌浣犵殑浠g爜鍙樺緱鏇村畨鍏ㄥ拰鍋ュ.錛岀壒鍒槸瀹冨彲浠ラ槻姝㈠唴瀛樻硠婕忎互鍙婄‘淇濆己瀹夊叏銆傚洜涓哄畠鏄爣鍑嗙殑錛屽洜姝ゅ畠鍦ㄤ笉鍚岀被搴撳拰騫沖彴涔嬮棿鏄彲縐繪鐨勶紝鍥犳鏃犺浣犲湪鍝噷浣跨敤瀹冿紝瀹冮兘灝嗘槸瀵圭殑銆?

鑷磋阿

This article is drawn from material in the new book Exceptional C++: 47 engineering puzzles, programming problems, and exception-safety solutions by Herb Sutter, 漏 2000 Addison Wesley Longman Inc., which contains further detailed treatments of points touched on briefly in this article, including exception safety, the Pimpl (compiler-firewall) Idiom, optimization, const-correctness, namespaces, and other C++ design and programming topics.

娉ㄩ噴

  1. Pimpl椋庢牸鍙互鏈夋晥鍑忓皯欏圭洰鏋勫緩鏃墮棿錛屽洜涓哄畠鍦–縐佹湁閮ㄥ垎鏀瑰彉鐨勬椂鍊欙紝闃繪瀹㈡埛浠g爜寮曡搗騫挎硾鐨勯噸鏂扮紪璇戙傛洿澶氬叧浜嶱impl椋庢牸浠ュ強濡備綍閮ㄧ講緙栬瘧鍣ㄥ錛屽弬鑰冭繖鏈?a href="xc++.htm">Exceptional C++鐨勬潯嬈?6鍒?0銆傦紙Addison-Wesley, 2000錛?

  2. See the article originally published in C++ Report and available on the Effective C++ CD (Scott Meyers, Addison-Wesley, 1999) and Items 8 to 19 in Exceptional C++ (Herb Sutter, Addison-Wesley, 2000).



volnet 2010-04-07 19:08 鍙戣〃璇勮
]]>
[杞琞浠嶤++鐨凴eturn Value Optimization (RVO)鍒癈#鐨剉alue typehttp://www.shnenglu.com/mymsdn/archive/2010/04/06/111777.htmlvolnetvolnetTue, 06 Apr 2010 11:42:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/04/06/111777.htmlhttp://www.shnenglu.com/mymsdn/comments/111777.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/04/06/111777.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/111777.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/111777.html鍏堢湅涓孌電畝鍗曠殑C++浠g爜錛?
Type get(int I){
    return Type(i);
} 

Type t = get(1); 

榪欓噷錛?鎴戜滑浠嶤++鐨勫熀鏈涔夌湅涓婂幓錛?搴旇鏄疶ype(i) 璋冪敤涓嬈℃嫹璐濇瀯閫犲嚱鏁幫紝 鍦ㄥ爢鏍堜腑鐢熸垚涓涓復鏃跺璞★紱鐒跺悗錛岀敤璇ュ璞℃瀯閫犺繑鍥炲璞★紱鐒跺悗瀵硅繖涓復鏃跺璞¤皟鐢ㄦ瀽鏋勫嚱鏁幫紱鍦ㄨ皟鐢ㄨ呮柟錛?鐢ㄨ繑鍥炵殑涓存椂瀵硅薄璋冪敤鎷瘋礉鏋勯犲嚱鏁頒互鍒濆鍖栧璞, 榪斿洖瀵硅薄鐨勬瀽鏋勫嚱鏁板湪榪欎箣鍚庯紝 鍑芥暟榪斿洖涔嬪墠璋冪敤銆?

鎵浠ワ紝 Type t = get(i); 搴旇鏈変笁涓嫹璐濇瀯閫犲嚱鏁板拰涓や釜鏋愭瀯鍑芥暟鐨勮皟鐢?

鍙槸錛?榪樻湁涓縐嶈娉曟槸錛?緙栬瘧鍣ㄥ彲鑳戒細瀵硅繖涓や釜涓存椂瀵硅薄榪涜浼樺寲錛屾渶緇堢殑浼樺寲緇撴灉浼氭槸鍙湁涓嬈$殑鏋勯犲嚱鏁般傚洜涓哄緢鏄庢樉鍦板彲浠ョ湅鍒幫紝 榪欓噷鎴戜滑鍏跺疄鍙槸瑕佺敤涓涓暣鏁版瀯閫犱竴涓猅ype瀵硅薄銆?

鍡? 浼間箮寰堟湁閬撶悊錛?

閭d箞錛?鍝竴縐嶈娉曞鍛紵 娌℃湁璋冩煡灝辨病鏈夊彂璦鏉冿紝浜庢槸鏈漢鐢╒C++6.0鍋氫簡瀹為獙銆?鏀句簡浜沜out<<鈥?.鍦ㄦ嫹璐濇瀯閫犲嚱鏁伴噷錛岃瀵熸墦鍗扮殑緇撴灉錛?緇撴灉鍗存槸璺熸垜鐨剆imple, na茂ve鐨勯嫻嬩竴鑷淬備笁涓嫹璐濇瀯閫犲嚱鏁幫紝 涓や釜鏋愭瀯鍑芥暟銆?

鈥滀綘涓急鏅虹紪璇戝櫒錛佽剳琚嬭繘姘翠簡鍚э紵鈥?蹇樹簡緙栬瘧鍣ㄦ病鑴戣浜?鈥滃緢鏄庢樉鍦ㄨ繖涓緥瀛愰噷鎴戠殑涓や釜涓存椂瀵硅薄閮芥病鏈夌敤鐨勫晩錛佲?

浜庢槸錛屼笂緗戯紝 鏌ヨ祫鏂欙紝 google涓涓嬪惂錛?

涓嬮潰鏄垜鏌ュ埌鐨勪竴浜涚粨鏋滐細

鍏跺疄錛?榪欑瀵瑰間紶閫掔殑浼樺寲鐨勭爺絀訛紝 騫朵笉鍙眬闄愪簬榪斿洖鍊箋傚涓嬮潰榪欎釜渚嬪瓙錛?

void f(T t) { } 
void main(void){ 
    T t1;
    f(t1); 
} 

涔熸湁榪欑鑰冭檻銆?

f(T)鏄寜鍊間紶閫掔殑銆傝涔変笂搴旇鍋氫竴涓鍒訛紝 浣垮緱鍑芥暟鍐呴儴瀵筎鐨勬敼鍙樹笉浼氬獎鍝嶅埌鍘熸潵鐨則1.

浣嗘槸錛屽洜涓哄湪璋冪敤f(t1)涔嬪悗錛?鎴戜滑娌℃湁鍐嶄嬌鐢╰1(闄や簡涓涓殣鍚殑destructor璋冪敤)錛屾槸鍚﹀彲鑳芥妸澶嶅埗浼樺寲鎺夛紝 鐩存帴浣跨敤t1鍛紵榪欐牱鍙互鑺傜渷鎺変竴涓嫹璐濇瀯閫犲嚱鏁板拰涓涓瀽鏋勫嚱鏁般?

鍙槸錛?涓嶈鏄榪斿洖鍊肩殑浼樺寲錛?榪樻槸瀵逛笂闈㈣繖縐嶅眬閮ㄥ璞$殑浼樺寲錛屽湪1995騫寸殑C++鏂版爣鍑嗚崏妗堝嚭鍙板墠閮芥槸涓烘爣鍑嗘墍涓ユ牸闄愬埗鐨?(铏界劧鏈変簺緙栬瘧鍣ㄥ茍娌℃湁閬佃榪欎釜鏍囧噯錛?榪樻槸鏀寔浜嗚繖縐嶁滀紭鍖栤?

閭d箞錛?榪欏張鏄負浠涔堝憿錛?

榪欓噷闈㈡秹鍙婂埌涓涓櫘閬嶇殑瀵箂ide-effect鐨勬媴蹇с?

浠涔堝張鏄痵ide-effect鍛紵

鎵璋搒ide-effect灝辨槸涓涓嚱鏁扮殑璋冪敤涓庡惁鑳藉瀵圭郴緇熺殑鐘舵侀犳垚鍖哄埆銆?

int add(int i, int j){ return i+j; }灝辨槸娌℃湁side-effect鐨勶紝鑰?

void set(int* p, int I, int v){ p[I]=v; }灝辨槸鏈塻ide-effect鐨勩傚洜涓哄畠鏀瑰彉浜嗕竴涓暟緇勫厓绱犵殑鍊鹼紝 鑰岃繖涓暟緇勫厓绱犲湪鍑芥暟澶栨槸鍙鐨勩?

閫氬父鎰忎箟涓婃潵璇達紝 鎵鏈夌殑浼樺寲搴旇鍦ㄤ笉褰卞搷紼嬪簭鐨勫彲瑙傚療琛屼負鐨勫熀紜涓婅繘琛岀殑銆傚惁鍒欙紝蹇垯蹇簡錛?緇撴灉鍗村拰鎵鎯寵鐨勫畬鍏ㄤ笉鍚岋紒

鑰孋++鐨勬嫹璐濇瀯閫犲嚱鏁板拰鏋愭瀯鍑芥暟鍙堝緢澶氶兘鏄湁side-effect鐨勩傚鏋滄垜浠殑鈥滀紭鍖栤濆幓鎺変簡涓涓湁side-effect鐨勬嫹璐濇瀯閫犲嚱鏁板拰涓涓瀽鏋勫嚱鏁幫紝 榪欎釜鈥滀紭鍖栤濆氨鏈夊彲鑳芥敼鍙樼▼搴忕殑鍙瀵熻涓恒傦紙娉ㄦ剰錛?鎴戣繖閲岃鐨勬槸鈥滃彲鑳解濓紝鍥犱負鈥滆礋璐熷緱姝b濓紝 涓や釜鏈塻ide-effect鐨勫嚱鏁扮殑璋冪敤錛?鍦ㄤ笉鑰冭檻騫惰榪愯鐨勬儏鍐典笅錛?涔熻鍙嶈屼笉浼氬獎鍝嶇▼搴忕殑鍙瀵熻涓恒備笉榪囷紝 榪欑濉炵縼澶遍┈鐨勪簨鍎匡紝 緙栬瘧鍣ㄥ氨寰堥毦鍒ゆ柇浜嗭級

鍩轟簬榪欑蹇ц檻, 1995騫翠互鍓嶇殑鏍囧噯錛?鏄庣‘紱佹瀵瑰惈鏈塻ide-effect鐨勬嫹璐濇瀯閫犲嚱鏁板拰鏋愭瀯鍑芥暟鐨勪紭鍖栥傚悓鏃訛紝 榪樻湁涓浜涘C++鎵╁厖鐨勬彁璁紝 鑰冭檻璁╃▼搴忓憳鑷繁瀵圭被榪涜鍏佽浼樺寲鐨勫0鏄庛?紼嬪簭鍛樺彲浠ユ槑紜湴鍛婅瘔緙栬瘧鍣細涓嶉敊錛?鎴戣繖涓嫹璐濇瀯閫犲嚱鏁幫紝 鏋愭瀯鍑芥暟鏄湁side-effect, 浣嗕綘鍒錛?灝界浼樺寲錛?鍑轟簡浜嬫湁鎴戝憿錛?

鍝庯紝 side-effect鐪熸槸涓涓浜哄張鎭ㄥ張鐖辯殑涓滆タ錛佸畠浣跨紪璇戝櫒鐨勪紭鍖栧彉寰楀洶闅撅紱鍔犲ぇ浜嗙▼搴忕淮鎶ゅ拰璋冭瘯鐨勯毦搴︺傚洜姝?functional language 鎶妔ide-effect褰撲綔媧按鐚涘吔涓鏍鳳紝騫茶剢紱佹銆備絾鍚屾椂錛屾垜浠張寰堥毦紱誨紑side-effect. 涓嶈紼嬪簭鍛樹滑鏇翠範鎯簬imperative 鐨勭紪紼嬫柟娉? 璞℃暟鎹簱鎿嶄綔錛孖O鎿嶄綔閮藉ぉ鐒跺氨鏄痵ide-effect.

涓嶈繃錛屼釜浜鴻繕鏄涓篊++鏍囧噯瀵光滀紭鍖栤濈殑淇濆畧鎬佸害鏄湁閬撶悊鐨勩傛棤璁哄浣曪紝璁┾滀紭鍖栤濆彲浠ユ綔鍦ㄥ湴鍋峰伔鍦版敼鍙樼▼搴忕殑琛屼負鎬繪槸璁╀漢鎯寵搗鏉ュ氨涓嶈垝鏈嶇殑銆?

浣嗘槸錛?鐭涚浘鏄绔嬬粺涓鐨勩傦紙鎯沖綋騫翠亢椹垪鍙緱浜嗗叓鍗佸鍒嗗憿錛夈?瀵硅繖縐峚ggressive鐨勨滀紭鍖栤濈殑鍛煎0鏄竴嫻珮榪囦竴嫻?浠tan Lippeman涓洪鐨勪竴灝忔挳欏藉浐鍒嗗瓙瀵規爣鍑嗙殑棰犺鍜屽拰騫蟲紨鍙樼殑闃磋皨浠庢潵灝辨病鏈夊仠姝㈣繃銆?榪欎笉錛熷湪1996騫寸殑涓涓闆ㄤ氦鍔犵殑澶滄櫄錛?涓涓槾闄╃殑C++鏂版爣鍑嗚崏妗堝嚭鐐変簡銆傚湪榪欎釜鑽夋閲岋紝 鍔犲叆浜嗕竴涓悕涓篟VO (Return Value Optimization) 鐨勬斁瀹藉浼樺寲鐨勯檺鍒訛紝 濡勫浘璧拌祫鏈富涔夐亾璺紝 緇欒祫鏈寮犵洰鐨勬彁妗堛傚叾鍏蜂綋鍐呭灝辨槸璇達細鍏佽緙栬瘧鍣ㄥ鍛藉悕榪囩殑灞閮ㄥ璞$殑榪斿洖榪涜浼樺寲錛?鍗充嬌鎷瘋礉鏋勯犲嚱鏁?鏋愭瀯鍑芥暟鏈塻ide-effect涔熷湪鎵涓嶆儨銆傝繖涓彁璁儗鍚庢墍闅愯棌鐨勬濇兂灝辨槸錛氫負浜嗘彁楂樻晥鐜囷紝 瀹佸彲鍐掓敼鍙樼▼搴忚涓虹殑椋庨櫓銆傚畞瑕佽祫鏈富涔夌殑鑻楋紝 涓嶈紺句細涓諱箟鐨勮崏浜嗭紒

鎴戞兂錛?榪欐牱鐨勪竴涓姜澶ф伓鏋佺殑鎻愭绔熶細琚彁浜わ紝搴旇鏄洜涓篊++鐨勫兼嫹璐濈殑璇箟鐨勬晥鐜囧疄鍦ㄥお鈥滃濡堢殑鈥濅簡銆?褰撲綘鍐欎竴涓?Complex operator+(const Complex& c1, const Complex& c2);鐨勬椂鍊欙紝 绔熼渶瑕佽皟鐢ㄥソ鍑犳鎷瘋礉鏋勯犲嚱鏁板拰鏋愭瀯鍑芥暟錛佸悓蹇椾滑錛侊紙娌夌棝鍦幫紝 璇噸蹇冮暱鍦幫級紺句細涓諱箟鐨勭敓浜у叧緋葷殑浼樿秺鎬ф庝箞浣撶幇鍟婏紵

鎺ヤ笅鏉ワ紝 褰撴垜鎯矴oogle C++鏈鏂扮殑鏍囧噯錛?鐪婻VO鏄惁琚渶緇堥噰綰蟲椂錛?鍗翠粈涔堜篃鎵句笉鍒頒簡銆?鍒癆NSI鐨勭綉绔欎笂鍘伙紝 灞呯劧瑕佷粯閽辨墠鑳紻OWNLOAD鏂囨。銆?鈥滆佸瓙鍦ㄥ煄閲屼笅棣嗗瓙閮戒笉浠橀挶錛?down浣犲嚑涓儌鏂囨。榪樿緇欓挶錛燂紒鈥?

鏁呬簨娌℃湁緇撳眬錛?瀹炲湪鏄笉鐖姐?涔熶笉鐭ユ槸涓嶆槸鍥犱負鏍囧噯榪樻病鏈夋暡瀹氾紝 鎵浠C++6 灝辨病鏈変紭鍖栵紝 榪樻槸VC鏍規湰灝辨病瀹屽叏閬靛畧鏍囧噯銆?

涓嶈繃錛屾湁涓鐐規槸鑲畾鐨勩?褰撳啓紼嬪簭鐨勬椂鍊欙紝 鏈濂戒笉瑕佷緷璧栦簬RVO (鏈変漢錛?璞tan Lippeman, 鍙堝彨瀹僋RV浼樺寲)銆?鍥犱負錛?涓嶈瀵規爣鍑嗙殑浜夎鏄惁宸茬粡鏈変簡緇撴灉錛?瀹為檯涓婂悇涓紪璇戝櫒鐨勫疄鐜頒粛榪樻槸鍚勮嚜涓烘斂錛?娌℃湁緇熶竴銆?涓涓彨SCOtt Meyers鐨勫浼?蹇樹簡鏄崠浠涔堢殑浜?灝辮錛?濡傛灉浣犵殑紼嬪簭渚濊禆浜嶳VO, 鏈濂藉幓鎺夎繖縐嶄緷璧栥備篃灝辨槸璇達紝 涓嶇RVO鍒板簳鏍囧噯涓嶆爣鍑嗭紝 浣犺繕鏄笉鑳界敤銆?涓嶄粎涓嶈兘鐢紝 榪樺緱鏃跺埢璀︽儠鐫RVO鍙兘甯︽潵鐨勭▼搴忚涓轟笂鐨勫彉鍖栥?錛堜篃涓嶇煡榪欏府瀹朵紮鐬庡繖浜嗗崐澶╁埌搴曚負鍟ワ紒錛?

璇村埌榪欓噷錛?鍊掓兂璧蜂簡C#閲屼竴涓洶鎯戜簡鎴戝緢涔呯殑闂銆傝寰楄C#鐨剆pecification鐨勬椂鍊欙紝 闈炲父涓嶈В涓轟粈涔圕#涓嶅厑璁哥粰value type 瀹氫箟鏋愭瀯鍑芥暟銆?

榪欓噷錛?鍏堢畝鐣ヤ粙緇嶄竴涓婥#閲岀殑value type (鍘熷鏁版嵁綾誨瀷錛?struct 綾誨瀷)銆?

鍦–#閲岀殑value_type灝辮薄鏄鹼紝 姘歌繙鍙兘copy, 鍙栧箋傚洜姝わ紝 瀹冩案榪滄槸in-place鐨勩傚鏋滀綘鎶婁竴涓獀alue type鐨勬暟鎹斁鍦ㄤ竴涓璞¢噷錛屽畠鐨勭敓鍛芥湡灝卞拰閭d釜瀵硅薄鐩稿悓錛涘鏋滀綘澹版槑涓涓獀alue type 鐨勫彉閲忓湪鍑芥暟涓紝 瀹冪殑鐢熷懡鏈熷氨鍦╨exical scope閲屻?

{

  The_ValueType value;

}//value 鍒拌繖閲屽氨姝昏彍浜?

鍟婂憖鍛錛?榪欎笉姝f槸鎴戜滑鎬蹇電殑C++鐨剆tack object鍚楋紵

鍦–++閲岋紝Auto_ptr, shared_ptr, 瀹瑰櫒浠紝 涓嶉兘鏄埄鐢ㄦ瀽鏋勫嚱鏁版潵綆$悊璧勬簮鐨勫悧錛?

C#錛孞ava 铏界劧鍒╃敤garbage collection鎶鏈潵鏀墮泦鏃犵敤瀵硅薄錛?浣挎垜浠笉鐢ㄥ啀鎷呭績鍐呭瓨鐨勫洖鏀躲?浣唃arbage collection騫朵笉淇濊瘉鏃犵敤瀵硅薄涓瀹氳鏀墮泦錛?騫朵笉淇濊瘉Dispose()鍑芥暟涓瀹氳璋冪敤錛?鏇翠笉淇濊瘉涓涓璞′粈涔堟椂鍊欒鍥炴敹銆?鎵浠ュ涓浜涢潪鍐呭瓨鐨勮祫婧愶紝 璞℃暟鎹簱榪炴帴錛?緗戠粶榪炴帴錛?鎴戜滑榪樻槸甯屾湜鑳芥湁涓涓被浼間簬smart pointer鐨勪笢瑗挎潵甯垜浠鐞嗗晩銆傦紙try-finally 铏界劧鍙互鐢紝 浣嗗洜涓哄畠褰卞搷鍒發exical scope, 鏈夋椂鐢ㄨ搗鏉ヤ笉閭d箞鏂逛究錛?

浜庢槸錛?鎴戝C#鐨勫彇娑坴alue type鐨勬瀽鏋勫嚱鏁板厖婊′簡娣卞帤鐨勯樁綰т粐鎭ㄣ?

涓嶈繃錛?鐜板湪鎯蟲潵錛?C#鐨勮繖縐嶈璁′竴瀹氭槸鎯╀簬C++澶辮觸鐨勬暀璁細

   1. value type 娌℃湁鎷瘋礉鏋勯犲嚱鏁般侰#鍙仛緙虹渷copy, 娌℃湁side-effect
   2. value type 涓嶅噯鏈夋瀽鏋勫嚱鏁般侰#鏈塯arbage collection, 鏋愭瀯鍑芥暟鐨勫敮涓鐢ㄩ斿彧浼氭槸鍋氫竴浜泂ide-effect璞″叧闂暟鎹簱榪炴帴銆?鎵浠ュ彇娑堜簡鏋愭瀯鍑芥暟錛?灝卞彇娑堜簡value type鐨剆ide-effect.
   3. 娌℃湁浜唖ide-effect, 緋葷粺鍙互浠繪剰鍦板仛浼樺寲浜?

瀵逛互涓嬬▼搴忥細

The_Valuetype get(int I){return The_Valuetype(i);}

The_Valuetype t = get(1);

鍦–#閲屾垜浠彲浠ュ揩涔愬湴璇達細鍙皟鐢ㄤ簡涓嬈℃瀯閫犲嚱鏁般?鍐嶆病鏈塻ide-effect鐨勬矙婕狅紝 鍐嶆病鏈夐毦浠ヤ紭鍖栫殑鑽掑師錛?smart pointer鏈涜屽嵈姝ワ紝 鏁堢巼涔嬭姳澶勫寮閬嶃?I have a dream, 鈥︹?/p>

杞澆鑷細http://gugu99.itpub.net/post/34143/466008



volnet 2010-04-06 19:42 鍙戣〃璇勮
]]>
妯℃澘綾誨瀷鎺ㄦ柇鐨勪竴涓皬闂http://www.shnenglu.com/mymsdn/archive/2010/04/05/111629.htmlvolnetvolnetSun, 04 Apr 2010 16:28:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/04/05/111629.htmlhttp://www.shnenglu.com/mymsdn/comments/111629.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/04/05/111629.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/111629.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/111629.html/* * type_infer_in_const_char_array.cpp * * Created on: 2010-4-4 * Author: volnet * Ref: http://topic.csdn.net/u/20100403/16/aebc3e87-ae49-4a18-ba0b-263348b512e3.html * ShortRef: http://is.gd/be914 */ #include <stdlib.h> #include <iostream> #ifdef _MSC_VER #include <typeinfo.h> #else #include <typeinfo> #endif template <typename T> int compare(const T &v1, const T &v2) { std::cout << "invoking compare ..." << std::endl; if(v1<v2) return -1; if(v2<v1) return 1; return 0; } int main() { //error: // no matching function for call to `compare(const char[3], const char[6])' // compare("hi","world"); compare<const char*>("hi","world"); //what's the real type about "abcd"? // const char * or const char [n] ? std::cout << typeid("hi").name() << std::endl; // char const [3] std::cout << typeid("world").name() << std::endl; // char const [6] std::cout << typeid("dlrow").name() << std::endl; // char const [6] // the compiler infer the typename T is char const [6] compare("world", "dlrow"); }

volnet 2010-04-05 00:28 鍙戣〃璇勮
]]>
鏅鴻兘鎸囬拡鐨勫疄鐜幫紙1錛?/title><link>http://www.shnenglu.com/mymsdn/archive/2010/04/04/design_of_smart_ptr_1.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Sun, 04 Apr 2010 15:08:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2010/04/04/design_of_smart_ptr_1.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/111621.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2010/04/04/design_of_smart_ptr_1.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/111621.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/111621.html</trackback:ping><description><![CDATA[<p>棣栧厛鎴戜滑闈復鐨勬寫鎴樻槸錛屽0鏄庡爢涓殑鏁版嵁闇瑕佹垜浠敤new鍏抽敭瀛楋紝鐩村埌鎴戜滑鏄劇ず璋冪敤浜哾elete涔嬪悗錛屽畠浠墠浼氳縐婚櫎錛屼絾闂鏄紝鎴戜滑浠涔堟椂鍊欏簲璇ョЩ闄わ紵濡傛灉璋冪敤璇彞鍦ㄤ笂涓嬭涓紝鎴戜滑鑷劧鐭ラ亾濡備綍縐婚櫎銆傛垜浠煡閬撳鏋滃璞″0鏄庡湪鍫嗕腑錛岄偅涔堝湪瀹冪寮瀹冪殑浣滅敤鍩熶腑鐨勬椂鍊欙紝鍙互鑷姩閲婃斁錛屽鏋滄垜浠兘澶熺敤鏍堝璞℃潵綆$悊鍫嗗璞★紝鍦ㄦ爤瀵硅薄鑷姩閲婃斁鐨勬椂鍊欙紝閲婃斁鍫嗗璞★紝灝變笉闇瑕佹樉寮忚皟鐢╠elete璇彞浜嗭細 <p>浜庢槸灝辨湁浜嗕笅闈㈢殑鍋氭硶錛屽簲璇ヤ笉闅劇悊瑙o細<pre class="gc-code"><span style="color: blue">template</span><<span style="color: blue">class </span>T> <span style="color: blue">class </span>smart_ptr { <span style="color: blue">public</span>: smart_ptr(T* t = 0) { real_ptr = t; } ~smart_ptr() { <span style="color: blue">delete </span>real_ptr; } T* <span style="color: blue">operator </span>->() <span style="color: blue">const </span>{ <span style="color: blue">return </span>real_ptr; } T& <span style="color: blue">operator </span>*() <span style="color: blue">const </span>{ <span style="color: blue">return </span>*real_ptr; } <span style="color: blue">private</span>: T* real_ptr; };</pre> <p>鎴戜滑甯屾湜鎴戜滑鐨勬櫤鑳芥寚閽堣兘澶熷儚鎸囬拡涓鏍峰湴宸ヤ綔錛屼絾涓嬮潰鐨勫伐浣滄柟寮忎技涔庡瓨鍦ㄩ棶棰橈細 <p>鍦ㄦ櫘閫氱殑dumb鎸囬拡涓紝浠ヤ笅琛屼負鏄紜殑錛?pre class="gc-code"><span style="color: blue">void </span>letUsGo(BaseClass* objPtr); DerivedClass* derivedObjPtr2 = <span style="color: blue">new </span>DerivedClass(); letUsGo(derivedObjPtr2); <span style="color: blue">delete </span>derivedObjPtr2;</pre> <p>浣嗘槸錛屼互涓嬩唬鐮佸憿錛?pre class="gc-code"><span style="color: blue">void </span>letUsSmartGo(<span style="color: blue">const </span>smart_ptr<BaseClass>& objPtr); smart_ptr<DerivedClass> smartDerivedObjPtr2(<span style="color: blue">new </span>DerivedClass()); <span style="color: green">// the smart_ptr<DerivedClass> is not inherited from the smart_ptr<BaseClass> // the compiler can't find the class to cast it, so it must cause the error. </span>letUsSmartGo(smartDerivedObjPtr2);</pre><a ></a> <p>涓嬮潰鐨勮繃紼嬫弿榪頒簡榪欎釜鍙樺寲鎵闇瑕佺殑涓浜涙敮鎸侊細 <p>1銆乪rror C2664: 鈥渓etUsSmartGo鈥? 涓嶈兘灝嗗弬鏁?1 浠庘渟mart_ptr<T>鈥濊漿鎹負鈥渃onst smart_ptr<T> &鈥?br>2銆乻martDerivedObjPtr2鐨勭被鍨嬶細<br>    smart_ptr<DerivedClass> smartDerivedObjPtr2(new DerivedClass());<br>3銆乴etUsSmartGo鐨勫0鏄庯細<br>    void letUsSmartGo(const smart_ptr<BaseClass>&);<br>4銆侀棶棰樿漿鍖栦負錛屽浣曚粠smart_ptr<DerivedClass>鍒癱onst smart_ptr<BaseClass>&鐨勮漿鍙樸?br>5銆侀拡瀵筶etUsSmartGo鐨勫0鏄庯紝鍙互鏈夌殑瀹炲弬綾誨瀷鍖呮嫭錛?br>    const smart_ptr<BaseClass><br>    smart_ptr<BaseClass><br>    鍋囪瀛樺湪浠ヤ笅綾誨瀷 smart_derived_ptr : smart_ptr<BaseClass>錛岄偅涔坰mart_derived_ptr涔熸槸鍙互琚紶閫掔殑銆?br>6銆佽繖閲屽瓨鍦ㄨ繖鏍蜂竴涓棶棰橈細<br>    new DerivedClass() 琚紶閫掔粰smartDerivedObjPtr2涔嬪悗錛宻martDerivedObjPtr2灝辨嫢鏈変簡瀹冪殑鎸囬拡銆傚鏋滀粠smartDerivedObjPtr2闅愬紡杞崲鎴愬彟涓涓猻mart_ptr<X>鍚庯紝鎴戜滑闇瑕佽В鍐崇殑灝辨槸灝唖martDerivedObjPtr2鎵鎷ユ湁鐨勬寚閽堜紶閫掔粰smart_ptr<X>騫跺皢smartDerivedObjPtr2鐨勫唴閮ㄦ寚閽堟竻闆訛紙榪欐牱灝變笉浼氬湪smartDerivedObjPtr2琚攢姣佺殑鏃跺欙紝鍥犱負璋冪敤delete real_ptr錛岃屽畠鐨勬柊鎷瘋礉鍦ㄧ寮浣滅敤鍩熺殑鏃跺欙紝涓鏍蜂細鍐嶆璋冪敤delete real_ptr錛岃屾鏃秗eal_ptr鎸囧悜鐨勫璞″凡緇忚閲婃斁錛屽洜姝よ繖鏍風殑琛屼負鏄湭瀹氫箟鐨勩傦級<br>7銆佸洜姝わ紝瀹氫箟濡備笅鏂規硶鍗沖彲錛?pre class="gc-code"><span style="color: blue">template</span><<span style="color: blue">class </span>T> <span style="color: blue">class </span>smart_ptr { <span style="color: blue">public</span>: smart_ptr(T* t = 0) { std::cout << <span style="color: #a31515">"creating smart_ptr ...smart_ptr(T* t = 0)" </span><< std::endl; real_ptr = t; } <strong> <span style="color: blue">template</span><<span style="color: blue">class </span>U> smart_ptr(smart_ptr<U>& rhs) : real_ptr(rhs.real_ptr){ std::cout << <span style="color: #a31515">"creating smart_ptr ...smart_ptr(smart_ptr<U>& rhs)" </span><< std::endl; rhs.real_ptr = 0; }</strong> ~smart_ptr() { std::cout << <span style="color: #a31515">"destoring smart_ptr ..." </span><< std::endl; <span style="color: blue">delete </span>real_ptr; } T* <span style="color: blue">operator </span>->() <span style="color: blue">const </span>{ <span style="color: blue">return </span>real_ptr; } T& <span style="color: blue">operator </span>*() <span style="color: blue">const </span>{ <span style="color: blue">return </span>*real_ptr; } <span style="color: green"> </span>T* real_ptr; };</pre> <p>8銆佷絾鏄繖閲宺eal_ptr鎸夌収涔犳儻搴旇鏄竴涓鏈夋垚鍛橈紝鑰屼笖鎴戜滑鍦ㄥ畬鎴愯鏂規硶鏃跺欙紝甯屾湜鑳藉瀹炵幇涓縐嶆墍璋撶殑鎵鏈夋潈杞Щ錛屼篃灝辨槸灝嗗唴閮ㄧ殑鎸囬拡浼犻掔粰鍙︿竴涓櫤鑳芥寚閽堬紝鑰岃繖搴旇鏄竴涓師瀛愯繃紼嬨傚洜姝わ紝鎴戜滑瀹炵幇浠ヤ笅鏂規硶錛?/p><pre class="gc-code"><span style="color: blue">template</span><<span style="color: blue">class </span>T> <span style="color: blue">class </span>smart_ptr { <span style="color: blue">public</span>: smart_ptr(T* t = 0) { std::cout << <span style="color: #a31515">"creating smart_ptr ...smart_ptr(T* t = 0)" </span><< std::endl; real_ptr = t; } <span style="color: blue">template</span><<span style="color: blue">class </span>U> smart_ptr(smart_ptr<U>& rhs) : real_ptr(rhs.release()){ std::cout << <span style="color: #a31515">"creating smart_ptr ...smart_ptr(smart_ptr<U>& rhs)" </span><< std::endl; } ~smart_ptr() { std::cout << <span style="color: #a31515">"destoring smart_ptr ..." </span><< std::endl; <span style="color: blue">delete </span>real_ptr; } T* <span style="color: blue">operator </span>->() <span style="color: blue">const </span>{ <span style="color: blue">return </span>real_ptr; } T& <span style="color: blue">operator </span>*() <span style="color: blue">const </span>{ <span style="color: blue">return </span>*real_ptr; } <span style="color: green">// helper </span>T* release() { T* tmp = real_ptr; real_ptr = 0; <span style="color: blue">return </span>tmp; } <span style="color: blue">private</span>: T* real_ptr; };</pre><img src ="http://www.shnenglu.com/mymsdn/aggbug/111621.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2010-04-04 23:08 <a href="http://www.shnenglu.com/mymsdn/archive/2010/04/04/design_of_smart_ptr_1.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鐢ㄤ簨瀹炶璇濓紝C++ Profiler錛圴isual Studio 2008錛?/title><link>http://www.shnenglu.com/mymsdn/archive/2010/04/01/111307.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Thu, 01 Apr 2010 10:02:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2010/04/01/111307.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/111307.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2010/04/01/111307.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/111307.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/111307.html</trackback:ping><description><![CDATA[<p><a href="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_2.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_thumb.png" width="1186" height="786"></a> </p> <p>1銆佽緗滅敓鎴愭椂鍚敤C/C++浠g爜鍒嗘瀽鈥濅負鈥滄槸鈥濓紝濡傛灉涓嶈緗欏癸紝紼嬪簭閫熷害灝嗗嚭涔庝綘鐨勬剰鏂欌︹?/p> <p><a href="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_thumb_1.png" width="852" height="640"></a> </p> <p>2銆佺偣鍑燴滃垎鏋愨?>鈥滃惎鍔ㄦц兘鍚戝鈥?/p> <p><a href="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_6.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_thumb_2.png" width="890" height="370"></a> </p> <p>3銆佸湪鈥滄ц兘璧勬簮綆$悊鍣ㄢ濅腑鍙抽敭鏂板緩鐨勬ц兘鎶ュ憡鑺傜偣錛屽彸閿滃惎鍔ㄥ茍鍚敤鍒嗘瀽鍔熻兘鈥濄?/p> <p><a href="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_10.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_thumb_4.png" width="658" height="262"></a> </p> <p>铏界劧姝ゅ鏈夆滃惎鐢ㄥ垎鏋愬姛鑳解濓紝浣嗗鏋滃湪閰嶇疆閲岄潰娌℃湁榪涜璁劇疆錛岀涓嬈$殑嫻嬭瘯鎶ュ憡緇撴灉灝嗘槸涓嶅噯紜殑銆?/p> <p>4銆侀夋嫨涓や釜鎬ц兘鎶ュ憡錛坈trl+榧犳爣錛夛紝鍙抽敭鈥滄瘮杈冩ц兘鎶ュ憡鈥濄?/p> <p><a href="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_8.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.shnenglu.com/images/cppblog_com/mymsdn/WindowsLiveWriter/CProfilerVisualStudio2008_FD62/image_thumb_3.png" width="785" height="370"></a> </p> <p></p> <p></p> <p>鐢ㄦц兘鎶ュ憡灝嗘湁鍔╀簬鎻愰珮紼嬪簭鐨勬ц兘錛屽茍涓斿揩閫熷畾浣嶉棶棰樻墍鍦紝鍓╀笅鐨勭粨鏋滃氨鏄綘鑷繁闇瑕佸瑙傚療錛屽垎鏋愭ц兘鎶ュ憡鎵鍙嶆槧鐨勯棶棰樹簡銆?br></p> <p>鏇村</p> <p>VC++ 6.0璇﹁榪欓噷>> <a title="http://neural.cs.nthu.edu.tw/jang/mir/technicalDocument/vc6_profile/index.htm" >http://neural.cs.nthu.edu.tw/jang/mir/technicalDocument/vc6_profile/index.htm</a></p> <p>絎笁鏂瑰伐鍏?gt;> <a title="http://www.semdesigns.com/Products/Profilers/CppProfiler.html" >http://www.semdesigns.com/Products/Profilers/CppProfiler.html</a></p><img src ="http://www.shnenglu.com/mymsdn/aggbug/111307.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2010-04-01 18:02 <a href="http://www.shnenglu.com/mymsdn/archive/2010/04/01/111307.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Ubuntu涓嬬紪璇慴oost錛堥檮Eclipse闈欐侀摼鎺ュ簱璁劇疆錛?/title><link>http://www.shnenglu.com/mymsdn/archive/2010/03/30/install-boost-under-boost.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Tue, 30 Mar 2010 09:17:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2010/03/30/install-boost-under-boost.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/111019.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2010/03/30/install-boost-under-boost.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/111019.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/111019.html</trackback:ping><description><![CDATA[榪欎笉鏄竴綃囧悜瀵鹼紝鍏ㄩ潰鐨勫悜瀵艱鐪?a href="http://www.boost.org/doc/libs/1_42_0/more/getting_started/unix- variants.html">榪欓噷</a>錛坔ttp://goo.gl/XcAf錛夈?br>榪欎粎鏄竴綃囩瑪璁般傝繖閲岀敤<a href="http://www.boost.org/doc/libs/1_42_0/more/getting_started/unix- variants.html#easy-build-and-install">bjam鏂瑰紡</a>榪涜緙栬瘧銆?br><ol><li>涓嬭澆boost銆傦紙http://cdnetworks-kr-1.dl.sourceforge.net/project/boost/boost/1.42.0/boost_1_42_0.tar.bz2錛?/li><li>瑙e帇鍒版寚瀹氭枃浠跺す錛屾垜鏄皢瀹冭В鍘嬪埌鏍圭洰褰曚笅鐨勪竴涓猻ourcecode鏂囦歡澶逛笅銆?div class="console">    /home/volnet/sourcecode/boost_1_42_0</div></li><li>鍚姩緇堢錛坈trl+f2,杈撳叆“gnome-terminal”錛夈傜敓鎴恇jam鑴氭湰銆?div class="console">    ./bootstrap.sh --prefix=/home/volnet/sourcecode/boost_install</div>濡傛灉涓嶅甫--prefix鍙傛暟鐨勮瘽錛?b>鎺ㄨ崘</b>錛夛紝榛樿鐨勮礬寰勬槸/usr/local/include鍜?usr/local/lib<div id="hvzpftn" class="console">    ./bootstrap.sh</div>浣跨敤--help鍙傛暟鍙互鏌ョ湅甯姪錛岀敤-h鍙互鏌ョ湅綆瑕佸府鍔┿?div class="console">    ./bootstrap.sh --help</div>鐢熸垚鑴氭湰bjam錛屽凡緇忓瓨鍦ㄧ殑鑴氭湰灝嗚鑷姩澶囦喚銆?pre class="console">volnet@Ubuntu:~/sourcecode/boost_1_42_0$ ./bootstrap.sh --prefix=/home/volnet/sourcecode/boost_install/<br>Building Boost.Jam with toolset gcc... tools/jam/src/bin.linuxx86/bjam<br>Detecting Python version... 2.6<br>Detecting Python root... /usr<br>Unicode/ICU support for Boost.Regex?... /usr<br>Backing up existing Boost.Build configuration in project-config.jam.4<br>Generating Boost.Build configuration in project-config.jam...<br><br>Bootstrapping is done. To build, run:<br><br>    ./bjam<br>    <br>To adjust configuration, edit 'project-config.jam'.<br>Further information:<br><br>   - Command line help:<br>     ./bjam --help<br>     <br>   - Getting started guide: <br>     http://www.boost.org/more/getting_started/unix-variants.html<br>     <br>   - Boost.Build documentation:<br>     http://www.boost.org/boost-build2/doc/html/index.html<br><br></pre></li><li>鐒跺悗灝辨槸鍒╃敤鐢熸垚鐨刡jam鑴氭湰緙栬瘧婧愮爜浜嗐?br><div id="hvzpftn" class="console">volnet@Ubuntu:~/sourcecode/boost_1_42_0$ sudo ./bjam -a -sHAVE_ICU=1 installNote: Building Boost.Regex with Unicode/ICU support enabled    Using ICU in  /usr/include</div><ul><li>./鏄痷nix-like緋葷粺鎵ц鏂囦歡鐨勫墠緙錛岃繖閲屽氨鏄寚瑕佹墽琛宐jam鏂囦歡銆?/li><li>-a鏄弬鏁幫紝浠h〃閲嶆柊緙栬瘧錛圧ebuild錛夈傝緭鍏?/bjam -h鑾峰緱鏇村甯姪銆?/li><li>-sHAVE_ICU=1錛屼唬琛ㄦ敮鎸乁nicode/ICU銆?a >鐐瑰嚮榪欓噷錛坔ttp://goo.gl/ySEe錛?/a><br><div id="hvzpftn" class="note">鍓嶆彁錛氱郴緇熷唴闇瑕佸畨瑁呮湁libicu-dev錛屽彲浠ュ湪緇堢杈撳叆錛?br><div id="hvzpftn" class="console">sudo apt-get install libicu-dev</div></div></li><li>install錛岃〃紺哄畨瑁?/li><li>--clean錛岃〃紺烘竻鐞嗐傚綋鍓嶈鍙ヤ腑鍖呭惈-a錛屽垯涓嶉渶瑕佸厛鎵嬪姩娓呯悊錛屽惁鍒欓渶瑕佸厛榪愯<div id="hvzpftn" class="console">./bjam --clean</div></li></ul></li><li>濡傛灉浣犳墽琛屼笂涓姝ワ紝浼氬嚭鐜拌濡傦細mpi鐨勯棶棰橈紝澶氬崐鏄湰鏈烘病鏈夊畨瑁卪pi銆傛牴鎹郴緇熸彁紺猴紝浣犲彲浠ユ壘鍒版湁/home/volnet/sourcecode/boost_1_42_0/tools/build/v2/user-config.jam銆傚湪鏂囦歡鏈鍚庤窡涓?div class="console">using mpi ;</div>鍗沖彲銆傜劧鍚庡鏋滆繕鏄湁mpi闂錛岃鏄庢湰鏈烘病鏈夊畨瑁卪pi銆?div class="console">sudo apt-get install mpi-default-dev</div></li></ol><br>鐣屾搴旇欏哄埄閫氳繃緙栬瘧浜嗐傚茍鍦?usr/local/lib涓嬫湁浜哹oost鐨勫簱浜嗐?br>涓嬮潰璁ㄨ涓涓嬮摼鎺ラ潤鎬侀摼鎺ュ簱鍦‥clipse閲岀殑闂銆?br>鍦?a >Unix variants</a>鍚戝閲岋紝瀹樻柟鎻愪緵浜嗕竴涓嬌鐢ㄦ鍒欒〃杈懼紡鐨勭▼搴忋?br>鍦‥clipse閲岋紝鏂板c++ project錛屽茍浣跨敤Linux C++緙栬瘧鍣ㄣ傚皢浠g爜鎷瘋礉鍒版枃浠朵腑錛?br>鍥犱負瑕侀潤鎬侀摼鎺ュ埌姝e垯琛ㄨ揪寮忕殑搴擄紝鎵浠ュ涓嬪浘鎵紺猴紝璁劇疆瀵瑰簲鐨?.a鏂囦歡璺緞鍒癳clipse錛屼互浣塊摼鎺ュ櫒鑳藉鎵懼埌瀹冦?br><img src="http://www.shnenglu.com/images/cppblog_com/mymsdn/static-library-in-eclipse.png" border="0"><br><br>緙栬瘧閫氳繃錛?br>涓轟簡鎴戜滑鐨勭▼搴忚兘澶熸紜澆鍏ワ紝鎴戜滑闇瑕佽鎴戜滑鐨勭▼搴忓湪鎴戜滑鐨勭郴緇熻寖鍥村唴涔熻兘澶熸壘鍒版垜浠殑搴撱傝繖鏃跺欐垜浠渶瑕佸湪璁劇疆涓涓嬨傝瑙?a >榪欓儴鍒?/a>鐨勭浉鍏充粙緇嶃?br>嫻嬭瘯紼嬪簭錛?br><div id="hvzpftn" class="console">volnet@Ubuntu:~/workspace/boost_get_started/Debug$ ./boost_get_started < test<br>Will Success Spoil Rock Hunter?<br></div>榪欓噷test鏄竴涓枃浠躲?br><br><br> <img src ="http://www.shnenglu.com/mymsdn/aggbug/111019.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2010-03-30 17:17 <a href="http://www.shnenglu.com/mymsdn/archive/2010/03/30/install-boost-under-boost.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鍗曞弬鏁版瀯閫犲嚱鏁扮殑闂錛宻ingle-argument constructhttp://www.shnenglu.com/mymsdn/archive/2010/03/29/single-argument-construct.htmlvolnetvolnetMon, 29 Mar 2010 07:37:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/03/29/single-argument-construct.htmlhttp://www.shnenglu.com/mymsdn/comments/110879.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/03/29/single-argument-construct.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/110879.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/110879.html鍏充簬鍗曚竴鍙傛暟鏋勯犲嚱鏁扮殑闂錛屼富瑕佹槸鍥犱負鍗曚竴鍙傛暟琚紪璇戝櫒鐢ㄤ綔闅愬紡綾誨瀷杞崲錛屼粠鑰屽鑷翠竴浜涗笉鍙鏈熺殑浜嬩歡鐨勫彂鐢燂紝璇峰弬瑙佷唬鐮佽緇嗘敞閲婏細

/*
 * single_argument_ctor.cpp
 *
 *  Created on: 2010-3-29
 *      Author: Volnet
 *      Compiler: GNU C++(version 3.4.5)
 *                MSVC CL(version 15.00.30729.01)
 */

#include <stdlib.h>
#include <iostream>
#include <sstream>

// single-argument class
class SingleArgumentClass {
private:
    int _inner;
public:
    SingleArgumentClass()
        :_inner(-1)
    {

    }
    SingleArgumentClass(int actual)
        :_inner(actual)
    {

    }
    bool operator==(const SingleArgumentClass& rhs);
    std::string str(){
        // we'd better to use boost::lexical_cast to cast.
        // #region cast
        std::stringstream strStream;
        strStream << _inner;
        std::string str;
        strStream >> str;
        // #endregion
        return str;
    }
};
bool
SingleArgumentClass::operator ==(const SingleArgumentClass& rhs){
    if(_inner == rhs._inner)
        return true;
    return false;
}

// single-argument class fixed bug by explicit keyword.
class SingleArgumentClassFixedBugByExplicitKeyword {
private:
    int _inner;
public:
    SingleArgumentClassFixedBugByExplicitKeyword()
        :_inner(-1)
    {

    }
    explicit SingleArgumentClassFixedBugByExplicitKeyword(int actual)
        :_inner(actual)
    {

    }
    bool operator==(const SingleArgumentClassFixedBugByExplicitKeyword& rhs);
    std::string str(){
        // we'd better to use boost::lexical_cast to cast.
        // #region cast
        std::stringstream strStream;
        strStream << _inner;
        std::string str;
        strStream >> str;
        // #endregion
        return str;
    }
};
bool
SingleArgumentClassFixedBugByExplicitKeyword::operator ==(const SingleArgumentClassFixedBugByExplicitKeyword& rhs){
    if(_inner == rhs._inner)
        return true;
    return false;
}

// single-argument class fixed bug by helper class.
class ActualType {
public:
    ActualType(int value):_value(value){};
    int get_value(){ return _value; }
private:
    int _value;
};
class SingleArgumentClassFixedBugByHelperClass {
private:
    int _inner;
public:
    SingleArgumentClassFixedBugByHelperClass()
        :_inner(-1)
    {

    }
    SingleArgumentClassFixedBugByHelperClass(ActualType actual)
        :_inner(actual.get_value())
    {

    }
    bool operator==(const SingleArgumentClassFixedBugByHelperClass& rhs);
    std::string str(){
        // we'd better to use boost::lexical_cast to cast.
        // #region cast
        std::stringstream strStream;
        strStream << _inner;
        std::string str;
        strStream >> str;
        // #endregion
        return str;
    }
};
bool
SingleArgumentClassFixedBugByHelperClass::operator ==(const SingleArgumentClassFixedBugByHelperClass& rhs){
    if(_inner == rhs._inner)
        return true;
    return false;
}


void Assert(bool status,
        std::string strTrue = std::string("assert result is true;"),
        std::string strFalse = std::string("assert result is false;"));
int main(void){
    SingleArgumentClass obj(3);
    std::cout << obj.str() << std::endl;

    // our purpose.
    SingleArgumentClass obj1(1);
    SingleArgumentClass obj2(1);
    Assert(obj1 == obj2, "obj1 == obj2", "obj1 != obj2");

    int i = 3;
    // warning!!!
    // obj is a SingleArgumentClass object.
    // i is a integer.
    // operator== only define the equal between two SingleArgumentClass object.
    // In fact:
    //    obj == i:
    //        1.compiler found the operator== require two SingleArgumentClass object.
    //        2.compiler try to find a cast method for casting int to SingleArgumentClass.
    //        3.compiler found it can use the SingleArguementClass.Ctor(int)
    //            to create a new SingleArgumentClass.
    //         4.compiler try to create a new SingleArgumentClass object from i.
    //        5.so it without any warning and error, but it's logical not we need.
    Assert(obj == i, "obj == i //right?", "obj != i");
    // Assert(i == obj); // Compile ERROR: no match for 'operator==' in 'i == obj'

    // it's may encounter a compile-time error.
    // GNU G++: no match for 'operator==' in 'objFixed == i'    single_argument_ctor.cpp    single_argument_ctor/src    106    C/C++ Problem
    // MSVC: 閿欒    1    error C2679: 浜岃繘鍒垛?=鈥? 娌℃湁鎵懼埌鎺ュ彈鈥渋nt鈥濈被鍨嬬殑鍙蟲搷浣滄暟鐨勮繍綆楃(鎴栨病鏈夊彲鎺ュ彈鐨勮漿鎹?    {projectpath}\single_argument_ctor\src\single_argument_ctor.cpp    107    single_argument_ctor
    SingleArgumentClassFixedBugByExplicitKeyword objFixed(3);
    // Assert(objFixed == i, "objFixed == i", "objFixed != i");

    SingleArgumentClassFixedBugByHelperClass objFixedByHelper1(3);
    SingleArgumentClassFixedBugByHelperClass objFixedByHelper2(3);
    // it's may encounter a compile-time error.
    // GNU G++: no match for 'operator==' in 'objFixedAuto1 == i'    single_argument_ctor.cpp    single_argument_ctor/src    158    C/C++ Problem
    // MSVC: 閿欒    1    error C2679: 浜岃繘鍒垛?=鈥? 娌℃湁鎵懼埌鎺ュ彈鈥渋nt鈥濈被鍨嬬殑鍙蟲搷浣滄暟鐨勮繍綆楃(鎴栨病鏈夊彲鎺ュ彈鐨勮漿鎹?    {projectpath}\single_argument_ctor\src\single_argument_ctor.cpp    163    single_argument_ctor
    // Assert(objFixedByHelper1 == i);
}
void Assert(bool status,
        std::string strTrue, std::string strFalse)
{
    std::cout << (status  strTrue : strFalse) << std::endl;
}

瑙e喅鏂規硶錛?/p>

1銆乪xplicit鍏抽敭瀛楋紝鍦ㄥ崟鍙傛暟鏋勯犲嚱鏁板墠浣跨敤explicit鍙傛暟錛屽彲浠ラ伩鍏嶅崟鍙傛暟鏋勯犲嚱鏁拌鐢ㄤ簬闅愬紡杞崲銆?/p>

2銆佸埄鐢ㄤ腑闂寸被鐨勬柟寮忥紝璇﹁浠g爜鈥淪ingleArgumentClassFixedBugByHelperClass鐩稿叧閮ㄥ垎鈥濄傚鏋滅紪璇戝櫒涓嶆敮鎸佽В鍐蟲柟娉?錛屽垯寤鴻浣跨敤姝ゆ柟娉曘備笂闈唬鐮佹墍鎻愬強鐨勪袱嬈句富嫻佺紪璇戝櫒鍧囨敮鎸乪xplicit鍏抽敭瀛椼?/p>

volnet 2010-03-29 15:37 鍙戣〃璇勮
]]>
[C++]namespace&amp;using keywordhttp://www.shnenglu.com/mymsdn/archive/2010/03/29/namespace-using-keywords.htmlvolnetvolnetMon, 29 Mar 2010 05:28:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/03/29/namespace-using-keywords.htmlhttp://www.shnenglu.com/mymsdn/comments/110846.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/03/29/namespace-using-keywords.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/110846.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/110846.html

鏈夋椂鍊欐垜浠彲浠ョ敤namespace鏉ョ粍緇囧懡鍚嶇┖闂淬?/p>

鏈夋椂鍊欐垜浠張甯屾湜灝嗕竴浜涜緝娣卞眰嬈$殑綾誨彉鎴愭垜浠瘮杈冨鏄撹闂殑瀵硅薄銆?/p>

涓嬮潰鐨勪唬鐮佹彁渚涗簡涓縐嶇畝鍗曠殑紺轟緥鏉ユ弧瓚寵繖鏍風殑闇姹傘?/p>

1銆佺敤namespace鏉ョ粍緇囧悇涓被鐨勫眰綰у叧緋匯?/p>

2銆佺敤using鍏抽敭瀛楋紝灝嗘繁灞傛緇撴瀯鏆撮湶鍒拌緝澶栧眰銆?/p>

//============================================================================
// Name        : namespace.cpp
// Author      : Volnet
// Version     :
// Copyright   : reserve by volnet@tom.com
// Description : namespace in C++, Ansi-style
//============================================================================

#include <iostream>

namespace volnet {
    namespace extensions {
        class _Console {
        public:
            void WriteLine(std::string);
        };
    }
    using extensions::_Console;
}
using namespace volnet;
_Console Console;

void
_Console::WriteLine(std::string s) {
    std::cout << s << std::endl;
}

using namespace std;

int main() {
    Console.WriteLine(std::string("I'm volnet!"));
    return 0;
}


volnet 2010-03-29 13:28 鍙戣〃璇勮
]]>
boost::tuplehttp://www.shnenglu.com/mymsdn/archive/2010/03/25/110537.htmlvolnetvolnetThu, 25 Mar 2010 09:46:00 GMThttp://www.shnenglu.com/mymsdn/archive/2010/03/25/110537.htmlhttp://www.shnenglu.com/mymsdn/comments/110537.htmlhttp://www.shnenglu.com/mymsdn/archive/2010/03/25/110537.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/110537.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/110537.html
boost::tuple<derived> tup4;
boost::tuple<base> tup5;
tup5 = tup4;
tup4.get<0>().test();
tup5.get<0>().test(); // 涓㈠け澶氭佹?br>
derived d;
boost::tuple<derived*> tup6(&d);
boost::tuple<base*> tup7;
tup7 = tup6;
tup6.get<0>()->test();
tup7.get<0>()->test(); // 鎭㈠澶氭佹э紙鏂規硶1錛?

boost::tuple<derived&> tup8(d);
boost::tuple<base&> tup9(tup8);

// tup9 = tup8; 涓嶈兘浣跨敤璇ユ柟娉曪紝鍥犱負鏃犳硶瀵瑰紩鐢ㄨ祴鍊箋?
tup8.get<0>().test(); tup9.get<0>().test(); // 鎭㈠澶氭佹э紙鏂規硶2錛?/span>
/*
 * tuple.cpp
 *
 *  Created on: 2010-3-25
 *      Author: GoCool
 */
#include <stdlib.h>
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_io.hpp>
#include "../header/baseClass.h"

using namespace std;
class X {
  X();
public:
  X(std::string){}
};
class Y {
  Y(const Y&);
public:
  Y(){}
};
class A {
};
bool operator==(A, A) { std::cout << "All the same to me..."; return true; }
void f(int i);

void cut_off_rule(void);
int main(void){
    // add a new tuple
    boost::tuple<int,double,std::string>   triple(42, 3.14, "My first tuple!");
    int a = triple.get<0>();
    ++a;
    cout << a << endl;
    cout << triple << endl;

    cut_off_rule();

    boost::tuple<int, double> pair = boost::make_tuple(21, 22.5);
    cout << pair << endl;

    cut_off_rule();

    int pair_element_1 = -1;
    double pair_element_2 = -1;
    boost::tie(pair_element_1, pair_element_2) = pair;

    cout << pair_element_1 << "," << pair_element_2 << endl;

    cut_off_rule();

    boost::tuple<int,std::string,derived> tup1(-5,"Tuples");
    boost::tuple<unsigned int,std::string,base> tup2;
    tup2=tup1;
    tup2.get<2>().test();
    std::cout << "Interesting value: " << tup2.get<0>() << '\n';
    const boost::tuple<double,std::string,base> tup3(tup2);
    // Description    Resource    Path    Location    Type
    // assignment of read-only location    tuple.cpp    boost_tuple/src    45    C/C++ Problem
    // tup3.get<0>()=3.14;

    cut_off_rule();

    boost::tuple<X,X,X> obj = boost::tuple<X,X,X>(string("Jaba"), string("Daba"), string("Duu")); // ok

    cut_off_rule();

    double dNum = 5;
    boost::tuple<double&> numTuple(dNum);               // ok

    // boost::tuple<double&>(dNum+3.14);          // error: cannot initialize
                                    // non-const reference with a temporary

    boost::tuple<const double&>(dNum+3.14);    // ok, but dangerous:
                                    // the element becomes a dangling reference
    cut_off_rule();

    // char arr[2] = {'a', 'b'};
    // boost::tuple<char[2]>(arr); // error, arrays can not be copied
    // boost::tuple<char[2], Y>(arr, Y()); // error, neither arrays nor Y can be copied

    boost::tuple<char[2], Y>();       // ok

    cut_off_rule();

    boost::tuple<void (*)(int)> pFTuple1 = boost::make_tuple(&f);
    pFTuple1.get<0>()(10);

    boost::tuple<void (*)(int)> pFTuple2 = boost::make_tuple(boost::ref(f));
    pFTuple2.get<0>()(20);

    boost::tuple<void (&)(int)> pFTuple3(f);
    pFTuple3.get<0>()(30);

    boost::tuple<boost::tuple<void (&)(int)> > pFTuple4(f);
    pFTuple4.get<0>().get<0>()(40);

    cut_off_rule();

    // boost::tuple<int, char> stdPairToTuple = std::make_pair(1, 'a');

    cut_off_rule();

    boost::tuple<std::string, int, A> t1(std::string("same?"), 2, A());
    boost::tuple<std::string, long> t2(std::string("same?"), 2);
    boost::tuple<std::string, long> t3(std::string("different"), 3);
    // t1 == t2;        // true

    cut_off_rule();

    int i; char c;
    boost::tie(i, c) = std::make_pair(1, 'a');
    cout << i << " " << c << endl;

    cut_off_rule();

    boost::tie(boost::tuples::ignore, c) = std::make_pair(1, 'a');
    cout << c << endl;

    cut_off_rule();

    int myX = -1;
    double myY = -2;
    boost::tuple<int, double> f2(2);
    boost::tie(myX, myY) = f2; // #2
    cout << "myX = " << myX << ", myY = " <<myY << endl;
}
void cut_off_rule(void) {
    cout << "-----------------------------------" << endl;
}

void f(int i) {
    cout << "f(" << i << ")" << endl;
}


tuple鏄痓oost搴撲腑涓涓被浼兼爣鍑唖td::pair搴撳簱錛屼絾pair鍙兘鏀寔涓ょ鍏冪礌錛岃宼uple鍒欏彲浠ユ敮鎸佸ぇ浜庝袱縐嶇殑銆?/p>

鏇村璇﹁В錛?a title="http://www.boost.org/doc/libs/1_42_0/libs/tuple/doc/tuple_users_guide.html" >http://www.boost.org/doc/libs/1_42_0/libs/tuple/doc/tuple_users_guide.html

浠ヤ笅鍐呭鐩存帴寮曡嚜鍘熸枃錛?/p>


 

Boost C++ LibrariesBoost C++ Libraries

鈥?..one of the most highly regarded and expertly designed C++ library projects in the world.鈥?鈥?Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

C++ 
Boost

The Boost Tuple Library

A tuple (or n-tuple) is a fixed size collection of elements. Pairs, triples, quadruples etc. are tuples. In a programming language, a tuple is a data object containing other objects as elements. These element objects may be of different types.

Tuples are convenient in many circumstances. For instance, tuples make it easy to define functions that return more than one value.

Some programming languages, such as ML, Python and Haskell, have built-in tuple constructs. Unfortunately C++ does not. To compensate for this "deficiency", the Boost Tuple Library implements a tuple construct using templates.

Table of Contents

  1. Using the library
  2. Tuple types
  3. Constructing tuples
  4. Accessing tuple elements
  5. Copy construction and tuple assignment
  6. Relational operators
  7. Tiers
  8. Streaming
  9. Performance
  10. Portability
  11. Acknowledgements
  12. References
More details

Advanced features (describes some metafunctions etc.).

Rationale behind some design/implementation decisions.

Using the library

To use the library, just include:

#include "boost/tuple/tuple.hpp"

Comparison operators can be included with:

#include "boost/tuple/tuple_comparison.hpp"

To use tuple input and output operators,

#include "boost/tuple/tuple_io.hpp"

Both tuple_io.hpp and tuple_comparison.hpp include tuple.hpp.

All definitions are in namespace ::boost::tuples, but the most common names are lifted to namespace ::boost with using declarations. These names are: tuple, make_tuple, tie and get. Further, ref and cref are defined directly under the ::boost namespace.

Tuple types

A tuple type is an instantiation of the tuple template. The template parameters specify the types of the tuple elements. The current version supports tuples with 0-10 elements. If necessary, the upper limit can be increased up to, say, a few dozen elements. The data element can be any C++ type. Note that void and plain function types are valid C++ types, but objects of such types cannot exist. Hence, if a tuple type contains such types as elements, the tuple type can exist, but not an object of that type. There are natural limitations for element types that cannot be copied, or that are not default constructible (see 'Constructing tuples' below).

For example, the following definitions are valid tuple instantiations (A, B and C are some user defined classes):

tuple<int>
tuple<double&, const double&, const double, double*, const double*>
tuple<A, int(*)(char, int), B(A::*)(C&), C>
tuple<std::string, std::pair<A, B> >
tuple<A*, tuple<const A*, const B&, C>, bool, void*>

Constructing tuples

The tuple constructor takes the tuple elements as arguments. For an n-element tuple, the constructor can be invoked with k arguments, where 0 <= k <= n. For example:

tuple<int, double>() 
tuple<int, double>(1)
tuple<int, double>(1, 3.14)

If no initial value for an element is provided, it is default initialized (and hence must be default initializable). For example.

class X {
X();
public:
X(std::string);
};

tuple<X,X,X>() // error: no default constructor for X
tuple<X,X,X>(string("Jaba"), string("Daba"), string("Duu")) // ok

In particular, reference types do not have a default initialization:

tuple<double&>()                // error: reference must be 
// initialized explicitly

double d = 5;
tuple<double&>(d) // ok

tuple<double&>(d+3.14) // error: cannot initialize
// non-const reference with a temporary

tuple<const double&>(d+3.14) // ok, but dangerous:
// the element becomes a dangling reference

Using an initial value for an element that cannot be copied, is a compile time error:

class Y { 
Y(const Y&);
public:
Y();
};

char a[10];

tuple<char[10], Y>(a, Y()); // error, neither arrays nor Y can be copied
tuple<char[10], Y>(); // ok

Note particularly that the following is perfectly ok:

Y y;
tuple<char(&)[10], Y&>(a, y);

It is possible to come up with a tuple type that cannot be constructed. This occurs if an element that cannot be initialized has a lower index than an element that requires initialization. For example: tuple<char[10], int&>.

In sum, the tuple construction is semantically just a group of individual elementary constructions.

The make_tuple function

Tuples can also be constructed using the make_tuple (cf. std::make_pair) helper functions. This makes the construction more convenient, saving the programmer from explicitly specifying the element types:

tuple<int, int, double> add_multiply_divide(int a, int b) {
return make_tuple(a+b, a*b, double(a)/double(b));
}

By default, the element types are deduced to the plain non-reference types. E.g.:

void foo(const A& a, B& b) { 
...
make_tuple(a, b);

The make_tuple invocation results in a tuple of type tuple<A, B>.

Sometimes the plain non-reference type is not desired, e.g. if the element type cannot be copied. Therefore, the programmer can control the type deduction and state that a reference to const or reference to non-const type should be used as the element type instead. This is accomplished with two helper template functions: ref and cref. Any argument can be wrapped with these functions to get the desired type. The mechanism does not compromise const correctness since a const object wrapped with ref results in a tuple element with const reference type (see the fifth example below). For example:

A a; B b; const A ca = a;
make_tuple(cref(a), b); // creates tuple<const A&, B>
make_tuple(ref(a), b); // creates tuple<A&, B>
make_tuple(ref(a), cref(b)); // creates tuple<A&, const B&>
make_tuple(cref(ca)); // creates tuple<const A&>
make_tuple(ref(ca)); // creates tuple<const A&>

Array arguments to make_tuple functions are deduced to reference to const types by default; there is no need to wrap them with cref. For example:

make_tuple("Donald", "Daisy");

This creates an object of type tuple<const char (&)[7], const char (&)[6]> (note that the type of a string literal is an array of const characters, not const char*). However, to get make_tuple to create a tuple with an element of a non-const array type one must use the ref wrapper.

Function pointers are deduced to the plain non-reference type, that is, to plain function pointer. A tuple can also hold a reference to a function, but such a tuple cannot be constructed with make_tuple (a const qualified function type would result, which is illegal):

void f(int i);
...
make_tuple(&f); // tuple<void (*)(int)>
...

volnet:
boost::tuple<void (&)(int)> pFTuple3(f);

pFTuple3.get<0>()(30);

tuple<tuple<void (&)(int)> > a(f) // ok
make_tuple(f); // not ok

Accessing tuple elements

Tuple elements are accessed with the expression:

t.get<N>()

or

get<N>(t)

where t is a tuple object and N is a constant integral expression specifying the index of the element to be accessed. Depending on whether t is const or not, get returns the Nth element as a reference to const or non-const type. The index of the first element is 0 and thus N must be between 0 and k-1, where k is the number of elements in the tuple. Violations of these constraints are detected at compile time. Examples:

double d = 2.7; A a;
tuple<int, double&, const A&> t(1, d, a);
const tuple<int, double&, const A&> ct = t;
...
int i = get<0>(t); i = t.get<0>(); // ok
int j = get<0>(ct); // ok
get<0>(t) = 5; // ok
get<0>(ct) = 5; // error, can't assign to const
...
double e = get<1>(t); // ok
get<1>(t) = 3.14; // ok
get<2>(t) = A(); // error, can't assign to const
A aa = get<3>(t); // error: index out of bounds
...
++get<0>(t); // ok, can be used as any variable

Note! The member get functions are not supported with MS Visual C++ compiler. Further, the compiler has trouble with finding the non-member get functions without an explicit namespace qualifier. Hence, all get calls should be qualified as: tuples::get<N>(a_tuple) when writing code that should compile with MSVC++ 6.0.

Copy construction and tuple assignment

A tuple can be copy constructed from another tuple, provided that the element types are element-wise copy constructible. Analogously, a tuple can be assigned to another tuple, provided that the element types are element-wise assignable. For example:

class A {};
class B : public A {};
struct C { C(); C(const B&); };
struct D { operator C() const; };
tuple<char, B*, B, D> t;
...
tuple<int, A*, C, C> a(t); // ok
a = t; // ok

In both cases, the conversions performed are: char -> int, B* -> A* (derived class pointer to base class pointer), B -> C (a user defined conversion) and D -> C (a user defined conversion).

Note that assignment is also defined from std::pair types:

tuple<float, int> a = std::make_pair(1, 'a');

volnet:(Eclipse with MinGW g++
conversion from `std::pair<int, char>' to non-scalar type `boost::tuples::tuple<float, int, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type>' requested

Relational operators

Tuples reduce the operators ==, !=, <, >, <= and >= to the corresponding elementary operators. This means, that if any of these operators is defined between all elements of two tuples, then the same operator is defined between the tuples as well. The equality operators for two tuples a and b are defined as:

  • a == b iff for each i: ai == bi
  • a != b iff exists i: ai != bi

The operators <, >, <= and >= implement a lexicographical ordering.

Note that an attempt to compare two tuples of different lengths results in a compile time error. Also, the comparison operators are "short-circuited": elementary comparisons start from the first elements and are performed only until the result is clear.

Examples:

tuple<std::string, int, A> t1(std::string("same?"), 2, A());
tuple<std::string, long, A> t2(std::string("same?"), 2, A());
tuple<std::string, long, A> t3(std::string("different"), 3, A());

bool operator==(A, A) { std::cout << "All the same to me..."; return true; }

t1 == t2; // true
t1 == t3; // false, does not print "All the..."

Tiers

Tiers are tuples, where all elements are of non-const reference types. They are constructed with a call to the tie function template (cf. make_tuple):

int i; char c; double d; 
...
tie(i, c, a);

The above tie function creates a tuple of type tuple<int&, char&, double&>. The same result could be achieved with the call make_tuple(ref(i), ref(c), ref(a)).

A tuple that contains non-const references as elements can be used to 'unpack' another tuple into variables. E.g.:

int i; char c; double d; 
tie(i, c, d) = make_tuple(1,'a', 5.5);
std::cout << i << " " << c << " " << d;

This code prints 1 a 5.5 to the standard output stream. A tuple unpacking operation like this is found for example in ML and Python. It is convenient when calling functions which return tuples.

The tying mechanism works with std::pair templates as well:

int i; char c;
tie(i, c) = std::make_pair(1, 'a');
Ignore

There is also an object called ignore which allows you to ignore an element assigned by a tuple. The idea is that a function may return a tuple, only part of which you are interested in. For example (note, that ignore is under the tuples subnamespace):

char c;
tie(tuples::ignore, c) = std::make_pair(1, 'a');

Streaming

The global operator<< has been overloaded for std::ostream such that tuples are output by recursively calling operator<< for each element.

Analogously, the global operator>> has been overloaded to extract tuples from std::istream by recursively calling operator>> for each element.

The default delimiter between the elements is space, and the tuple is enclosed in parenthesis. For Example:

tuple<float, int, std::string> a(1.0f,  2, std::string("Howdy folks!");

cout << a;

outputs the tuple as: (1.0 2 Howdy folks!)

The library defines three manipulators for changing the default behavior:

  • set_open(char) defines the character that is output before the first element.
  • set_close(char) defines the character that is output after the last element.
  • set_delimiter(char) defines the delimiter character between elements.

Note, that these manipulators are defined in the tuples subnamespace. For example:

cout << tuples::set_open('[') << tuples::set_close(']') << tuples::set_delimiter(',') << a; 

outputs the same tuple a as: [1.0,2,Howdy folks!]

The same manipulators work with operator>> and istream as well. Suppose the cin stream contains the following data:

(1 2 3) [4:5]

The code:

tuple<int, int, int> i;
tuple<int, int> j;

cin >> i;
cin >> tuples::set_open('[') >> tuples::set_close(']') >> tuples::set_delimiter(':');
cin >> j;

reads the data into the tuples i and j.

Note that extracting tuples with std::string or C-style string elements does not generally work, since the streamed tuple representation may not be unambiguously parseable.

Performance

All tuple access and construction functions are small inlined one-liners. Therefore, a decent compiler can eliminate any extra cost of using tuples compared to using hand-written tuple like classes. Particularly, with a decent compiler there is no performance difference between this code:

class hand_made_tuple { 
A a; B b; C c;
public:
hand_made_tuple(const A& aa, const B& bb, const C& cc)
: a(aa), b(bb), c(cc) {};
A& getA() { return a; };
B& getB() { return b; };
C& getC() { return c; };
};

hand_made_tuple hmt(A(), B(), C());
hmt.getA(); hmt.getB(); hmt.getC();

and this code:

tuple<A, B, C> t(A(), B(), C());
t.get<0>(); t.get<1>(); t.get<2>();

Note, that there are widely used compilers (e.g. bcc 5.5.1) which fail to optimize this kind of tuple usage.

Depending on the optimizing ability of the compiler, the tier mechanism may have a small performance penalty compared to using non-const reference parameters as a mechanism for returning multiple values from a function. For example, suppose that the following functions f1 and f2 have equivalent functionalities:

void f1(int&, double&);
tuple<int, double> f2();

Then, the call #1 may be slightly faster than #2 in the code below:

int i; double d;
...
f1(i,d); // #1
tie(i,d) = f2(); // #2

volnet:
int myX = -1;
double myY = -2;
boost::tuple<int, double> f2(2);
boost::tie(myX, myY) = f2; // #2
cout << "myX = " << myX << ", myY = " <<myY << endl;

See [1, 2] for more in-depth discussions about efficiency.

Effect on Compile Time

Compiling tuples can be slow due to the excessive amount of template instantiations. Depending on the compiler and the tuple length, it may be more than 10 times slower to compile a tuple construct, compared to compiling an equivalent explicitly written class, such as the hand_made_tuple class above. However, as a realistic program is likely to contain a lot of code in addition to tuple definitions, the difference is probably unnoticeable. Compile time increases between 5 and 10 percent were measured for programs which used tuples very frequently. With the same test programs, memory consumption of compiling increased between 22% to 27%. See [1, 2] for details.

Portability

The library code is(?) standard C++ and thus the library works with a standard conforming compiler. Below is a list of compilers and known problems with each compiler:

Compiler
Problems

gcc 2.95
-

edg 2.44
-

Borland 5.5
Can't use function pointers or member pointers as tuple elements

Metrowerks 6.2
Can't use ref and cref wrappers

MS Visual C++
No reference elements (tie still works). Can't use ref and cref wrappers

Acknowledgements

Gary Powell has been an indispensable helping hand. In particular, stream manipulators for tuples were his idea. Doug Gregor came up with a working version for MSVC, David Abrahams found a way to get rid of most of the restrictions for compilers not supporting partial specialization. Thanks to Jeremy Siek, William Kempf and Jens Maurer for their help and suggestions. The comments by Vesa Karvonen, John Max Skaller, Ed Brey, Beman Dawes, David Abrahams and Hartmut Kaiser helped to improve the library. The idea for the tie mechanism came from an old usenet article by Ian McCulloch, where he proposed something similar for std::pairs.

References

[1] J盲rvi J.: Tuples and multiple return values in C++, TUCS Technical Report No 249, 1999.

[2] J盲rvi J.: ML-Style Tuple Assignment in Standard C++ - Extending the Multiple Return Value Formalism, TUCS Technical Report No 267, 1999.

[3] J盲rvi J.:Tuple Types and Multiple Return Values, C/C++ Users Journal, August 2001.


Last modified 2003-09-07

漏 Copyright Jaakko J盲rvi 2001. Permission to copy, use, modify, sell and distribute this software and its documentation is granted provided this copyright notice appears in all copies. This software and its documentation is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.



 



volnet 2010-03-25 17:46 鍙戣〃璇勮
]]>
C璇█浣嶅煙http://www.shnenglu.com/mymsdn/archive/2009/12/30/c-bit-field.htmlvolnetvolnetTue, 29 Dec 2009 17:02:00 GMThttp://www.shnenglu.com/mymsdn/archive/2009/12/30/c-bit-field.htmlhttp://www.shnenglu.com/mymsdn/comments/104425.htmlhttp://www.shnenglu.com/mymsdn/archive/2009/12/30/c-bit-field.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/104425.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/104425.html
 1 #include <stdio.h>
 2 typedef struct _SimpleType1 {
 3     int Variable1;    //4bytes(32bits)
 4     int Variable2;    //4bytes(32bits)
 5     int Variable3;    //4bytes(32bits)
 6     int Variable4;    //4bytes(32bits)
 7 } SimpleType1;
 8 
 9 typedef struct _ComplexType1 {
10     int Variable1 : 8;    //1bytes(8bits)
11     int Variable2 : 8;    //1bytes(8bits)
12     int Variable3 : 8;    //1bytes(8bits)
13     int Variable4 : 8;    //1bytes(8bits)
14 } ComplexType1;
15 
16 typedef struct _ComplexType2 {
17     int Variable1 : 8;    //1bytes(8bits)
18     int Variable2 : 8;    //1bytes(8bits)
19     int Variable3 : 8;    //1bytes(8bits)
20     int Variable4 : 8;    //1bytes(8bits)
21     int Variable5 : 1;    //0.125bytes(1bits) but the it also hold 32bits 
22 } ComplexType2;
23 
24 int main(void){
25     printf("sizeof SimpleType1 = %d\n"sizeof(SimpleType1));
26     printf("sizeof ComplexType1 = %d\n"sizeof(ComplexType1));
27     printf("sizeof ComplexType2 = %d\n"sizeof(ComplexType2));
28 }
緇撴灉錛?br>sizeof SimpleType1 = 16
sizeof ComplexType1 = 4
sizeof ComplexType2 = 8



volnet 2009-12-30 01:02 鍙戣〃璇勮
]]>
鍏充簬綰跨▼鍚屾鐨勪竴浜涙葷粨錛堢敤鎴鋒ā寮?鍐呮牳妯″紡錛?/title><link>http://www.shnenglu.com/mymsdn/archive/2009/12/26/about-multithread-sync.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Fri, 25 Dec 2009 21:53:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2009/12/26/about-multithread-sync.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/104111.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2009/12/26/about-multithread-sync.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/104111.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/104111.html</trackback:ping><description><![CDATA[<p><h3>鑷棆閿佸悓姝?/h3><ol><li>涓鑸槸涓轟簡鍐呮牳鎬佷笅鍚勪釜媧鵑仯鍑芥暟涔嬮棿鍋氬悓姝ヤ綔鐢ㄧ殑銆?/li><li>鍘熺悊鏄紙鍗旵PU錛夊皢IRQL浠庤蔣浠朵腑鏂彁鍗囧埌紜歡涓柇銆侾ASSIVE_LEVEL->DISPATCH_LEVEL銆傚洜涓哄湪DISPATCH_LEVEL涓槸涓嶄細鍑虹幇綰跨▼鍒囨崲鐨勶紙鍙湁楂樼駭鍒兘鎵撴柇浣庣駭鍒紝鑰屼綆綰у埆涓嶈兘鎵撴柇楂樼駭鍒級銆?/li><li>鍥犱負鍒嗛〉鍐呭瓨灝嗗鑷村鏋滅嚎紼嬪垏鎹㈢殑鏃跺欎細寮曡搗鍒嗛〉鏁版嵁浜ゆ崲錛屾暟鎹氦鎹㈡槸閫氳繃寮曞彂欏墊晠闅滄潵瀹炵幇鐨勶紝鑰岄〉鏁呴殰鏄笉鍏佽鍑虹幇鍦―ISPATCH_LEVEL涓殑錛屽惁鍒欏皢寮曡搗緋葷粺宕╂簝錛圥ASSIVE_LEVEL鍒欏厑璁革級銆傞┍鍔ㄧ▼搴忕殑StartIO渚嬬▼銆丏PC渚嬬▼銆佷腑鏂湇鍔′緥紼嬮兘榪愯鍦―ISPATCH_LEVEL鎴栬呮洿楂樼殑IRQL銆傚洜姝よ繖浜涗緥紼嬩笉鑳戒嬌鐢ㄥ垎欏靛唴瀛橈紝鍚﹀垯灝嗗鑷寸郴緇熷穿婧冦?/li><li>鑷棆閿佸湪涓嶅悓IRP涔嬮棿鍚屾鐨勬椂鍊欙紝鍒欓渶瑕佹斁鍦―eviceExtension涓紶閫掋?/li></ol></p><p><h3>浜掗攣</h3><ol><li>綾諱技浜巒umber++; //姹囩紪鍚庡皢涓嶆涓鏉¤鍙ワ紝闈炲師瀛愭搷浣渘umber--; //鍚屼笂鍥犱負璇彞浼氬彉鎴愬鍙ワ紝鍦ㄧ嚎紼嬪垏鎹㈢殑鏃跺欙紝涓や釜綰跨▼涓嬬殑璇ヤ緥紼嬪皢浼氫氦緇囧湪涓璧鋒墽琛岋紝瀵艱嚧閿欒銆傚彲浠ワ細<div id="hvzpftn" class="gc-code">鍏堝姞閿?br />number++;<br />瑙i攣<br />鍐嶅姞閿?br />number--;<br />瑙i攣<br /></div>鏉ュ疄鐜頒袱鍙ヨ瘽鐨勫悓姝ワ紙鎸夋寚瀹氶『搴忔墽琛岋紝鑰屼笉鍙楀埌綰跨▼鍒囨崲鐨勫獎鍝嶏級鍔犻攣瑙i攣鍙互浣跨敤鑷棆閿?/li><li>鍦ㄧ郴緇熶腑鎻愪緵浜咺nterlocked***/ExInterlocked***瀹炵幇</li></ol></p><p><h3>淇″彿鐏悓姝?/h3><ol><li>綰跨▼1鍏抽棴淇″彿鐏紝浠ヨ嚦浜庝嬌鐢╓ait****鐨勬椂鍊欙紝褰撳墠綰跨▼澶勪簬鏆傚仠鐘舵併?/li><li>綰跨▼2鐨勪綔鐢ㄥ氨鏄湪鎵ц緇撴潫鍚庯紝鐐逛寒淇″彿鐏紙澧炲姞璁℃暟鍣級銆傚綋綰跨▼鍒囨崲鍥炴潵鐨勬椂鍊欙紝綰跨▼1灝卞洜涓鴻鏁板櫒涓嶆槸0鑰屼嬌淇″彿鐏浜庢縺媧葷姸鎬侊紝浠庤岀戶緇墽琛岀嚎紼?銆?/li></ol></p><p><h3>浜嬩歡鐨勫悓姝?/h3>錛堜笉鑳介掑綊鑾峰彇浜掓枼浣擄級<ol><li>涓葷嚎紼嬪湪杈呭姪綰跨▼涓婅緗簡浜嬩歡錛屽鏋滀笉浣跨敤Wait**絳夊緟浜嬩歡榪斿洖錛屽垯涓葷嚎紼嬪彲鑳界洿鎺ユ墽琛屽畬姣曚簡錛岃屽鑷磋緟鍔╃嚎紼嬭繕鍦ㄦ墽琛屻?/li><li>浣跨敤Wait****鍙互浣夸富綰跨▼絳夊緟浜嬩歡鎵ц瀹屾垚銆?/li></ol></p><p><h3>浜掓枼浣撳悓姝?/h3>錛堝厑璁擱掑綊鑾峰彇浜掓枼浣擄紙寰楀埌浜掓枼浣撶殑綰跨▼榪樺彲浠ュ啀嬈¤幏寰楄繖涓簰鏂ヤ綋錛屾垨鑰呰浜掓枼浣撳浜庡凡緇忚幏寰椾簰鏂ヤ綋鐨勭嚎紼嬩笉浜х敓“浜掓枼”鍏崇郴錛夛級<ol><li>鍒涘緩涓涓簰鏂ヤ綋瀵硅薄錛屽皢浜掓枼浣撳璞′紶閫掔粰澶氫釜綰跨▼銆?/li><li>鍦ㄦ瘡涓嚎紼嬫搷浣滅殑姝ラ涓紝璋冪敤Wait*****錛屽鏋滀簰鏂ヤ綋澶勪簬嬋媧伙紙鍐呴儴緇存姢涓涓鏁板櫒錛夛紝鍒欑戶緇墽琛屽悗緇唬鐮侊紝騫跺湪璋冪敤緇撴潫鍚庢仮澶嶄簰鏂ヤ綋Release****錛岃繖鏍峰綋鍒殑綰跨▼璇曞浘浣跨敤浜掓枼浣撳悗闈㈢殑浠g爜鐨勬椂鍊欙紝鍥犱負浜掓枼浣撶姸鎬佹湭嬋媧伙紝鍒欐棤娉曠戶緇墽琛屼唬鐮併?/li></ol></p><p><h3>蹇熶簰鏂ヤ綋鍚屾</h3><ol><li>涓庝簰鏂ヤ綋鍚屾綾諱技錛屽敮涓鍖哄埆鏄笉鍏佽閫掑綊鑾峰彇浜掓枼浣?/li></ol></p><img src ="http://www.shnenglu.com/mymsdn/aggbug/104111.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2009-12-26 05:53 <a href="http://www.shnenglu.com/mymsdn/archive/2009/12/26/about-multithread-sync.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>濡備綍緙栬瘧TrueCrypt婧愮爜http://www.shnenglu.com/mymsdn/archive/2009/12/23/how-to-complie-TrueCrypt-63a.htmlvolnetvolnetWed, 23 Dec 2009 15:47:00 GMThttp://www.shnenglu.com/mymsdn/archive/2009/12/23/how-to-complie-TrueCrypt-63a.htmlhttp://www.shnenglu.com/mymsdn/comments/103863.htmlhttp://www.shnenglu.com/mymsdn/archive/2009/12/23/how-to-complie-TrueCrypt-63a.html#Feedback9http://www.shnenglu.com/mymsdn/comments/commentRss/103863.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/103863.html鐩稿叧閰嶇疆
       
  • Intel x86 Core 2 Duo
  •    
  • Windows 7 Ultimate x86 version
  •    
  • Windows Driver Develop Kit 7600.16385.0
  •    
  • TrueCrypt 6.3a Source.zip
  •    
  • Microsoft Visual Studio 2008 SP1 (VC++ 2008)
  •    
  • Microsoft Visual Studio VC++ 1.52
  •    
  • NASM version 2.07 compiled on Jul 19 2009
  •    
  • gzip 1.2.4 Win32 (02 Dec 97)
  •    
  • ......

閰嶇疆TrueCrypt

  1. 涓嬭澆MSVC++ 1.52錛屽畨瑁呭湪C鐩樹笅錛欳:\MSVC
  2. 涓嬭澆NASM錛屼篃瀹夎鍦–鐩樹笅錛欳:\NASM
    http://www.nasm.us/pub/nasm/releasebuilds/2.07/win32/
  3. 涓嬭澆GZIP錛屼篃瀹夎鍦–鐩樹笅錛欳:\gzip
  4. 涓嬭澆騫跺畨瑁匴INDDK錛宧ttp://www.microsoft.com/whdc/DevTools/WDK/WDKpkg.mspx
    鎴戝皢瀹冧滑瀹夎鍦―鐩橈紝璺緞錛欴:\WinDDK
  5. 璁劇疆緋葷粺鍙橀噺錛堬紙WIN7錛夋帶鍒墮潰鏉縗鎵鏈夋帶鍒墮潰鏉塊」\緋葷粺\楂樼駭緋葷粺璁劇疆\鐜鍙橀噺錛夛細緋葷粺鍙橀噺涓紝鏂板錛?br />鍙橀噺1錛歁SVC16_ROOT    鍊鹼細C:\MSVC
    鍙橀噺2錛歐INDDK_ROOT    鍊鹼細D:\WinDDK\7600.16385.0
    鍏朵腑7600.16385.0涓篧inDDK鐨勭浜岀駭鐩綍鍚嶏紝鍚屾椂涔熸槸鐗堟湰鍙鳳紝7600鏄疻indows7鐨勫彂琛岀増鏈彿銆?br />鍙屽嚮鍙橀噺錛歅ATH錛屽湪鍏跺肩殑鏈熬琛ヤ笂錛欳:\NASM;C:\gzip
    鐩殑鏄負浜嗚鎴戜滑鍙互鐩存帴鍦ㄥ懡浠よ瀹炵敤nasm浠ュ強gzip浣滀負鍛戒護琛屻?br />
  6. 涓嬭澆PKCS11錛屼笁涓枃浠訛紝鍙抽敭鍙﹀瓨涓哄嵆鍙?br />http://svn.openvpn.net/projects/openvpn/test/time/openvpn/pkcs11-headers/
    灝嗕笁涓枃浠訛紙pkcs11.h銆乸kcs11f.h銆乸kcs11t.h錛夋嫹璐濆埌婧愮爜涓嬬殑Common鏂囦歡澶逛笅錛屾垜鐨勬簮鐮佹斁鍦―鐩樻牴鐩綍錛屼笁涓簮鐮佹垜灝辨斁鍦?#8220;D:\TrueCrypt\Common”鏂囦歡澶逛腑銆?br />
  7. 緙栬瘧錛屼細鍙戠幇鏈変袱涓敊璇?br />CKR_NEW_PIN_MODE鍜孋KR_NEXT_OTP鏈畾涔夛紝琛ュ厖瀹氫箟涓涓嬪嵆鍙?br />鍦╠:\TrueCrypt\Common\pkcs11t.h鏂囦歡閲岋紙璇鋒牴鎹偍鑷繁鐨勮礬寰勮繘琛屽鍒訛級
    榪欓噷灝嗗畠浠緗負錛?br />
    #define CKR_NEW_PIN_MODE      0x000001B0
    #define CKR_NEXT_OTP          0x000001B1
    鎴戠殑鏂規硶鏄壘鍒板疄鐢ㄥ畠鐨勮鍙ラ檮榪戠殑鍚岀被璇彞錛屾壘鍒扮浉鍚岀殑瀹氫箟錛屽湪鍏朵笅鏂規坊鍔犮?br />姣斿錛?br />
                TC_TOKEN_ERR (CKR_MUTEX_NOT_LOCKED)
                TC_TOKEN_ERR (CKR_NEW_PIN_MODE)
                TC_TOKEN_ERR (CKR_NEXT_OTP)
    榪欎笁鍙ヨ瘽鏀懼湪涓璧鳳紝鍚庝袱鍙ユ湁闂錛屼絾絎竴鍙ユ甯革紝鍒欐煡鎵綜KR_MUTEX_NOT_LOCKED鐨勫瓨鏀句綅緗紝鍦ㄥ叾涓嬫柟娣誨姞濡備笂涓ゅ彞錛屽叾涓畾涔夌殑鍊煎弬鑰?br />http://www.cryptsoft.com/pkcs11doc/STANDARD/include/v220/otp-pkcs11.h錛岃繖閲岀殑鍊煎彧涓嶈繃鏄竴縐嶉敊璇爜錛屽彧瑕佸畠涓嶉噸澶嶏紝灝卞彲浠ヤ簡銆?br />
  8. 鍐嶇紪璇戯紝鍙兘浼氶亣鍒頒竴浜涜鍛婏細
    1. nasm.exe姝e湪鍋滄錛岃屽洜涓烘病鏈夋紜墽琛岋紝鍙堝鑷?#8220;fatal error LNK1136: invalid or corrupt file”閿欒銆?br />閬囧埌榪欎釜鍙兘鏄洜涓轟綘鐨刵asm姝e湪璇曞浘緙栬瘧ase_amdX64.asm鏂囦歡錛岃宯asm瀵?4浣嶇殑asm緙栬瘧瀵逛綘鍙兘鎰忎箟涓嶅ぇ錛岃搗鐮佸鎴戣岃█鏄繖鏍風殑錛屼簬鏄紝鎴戝氨灝嗗畠杞垚緙栬瘧x86浣撶郴鏋舵瀯鐨勶紝涔熻鏄洜涓哄弬鏁伴厤緗殑闂錛屼綘鍙互灝濊瘯鍒殑鏂規錛屽鏋滄湁鏇村ソ鐨勮瘽錛岃鍛婅瘔鎴戙?br />榪欓噷鎴戞悳绱細x64銆乤sm絳夊叧閿瓧錛屼慨鏀筪:\TrueCrypt\Crypto\Makefile.inc鏂囦歡涓轟笅闈㈣繖鏍峰嵆鍙細
      琛?    !if "$(TC_ARCH)" == "x86"
      琛?    TC_OBJ_FORMAT = win32
      琛?    !else
      琛?    #TC_OBJ_FORMAT = win64
      琛?    #edit by gocool, if the x64 system need the nasm.exe use the x64 format parameters for executing.
      琛?    #abort the x64 system here for building.
      琛?    #2009/12/23
      琛?    TC_OBJ_FORMAT = win32
      琛?    TC_ARCH = x86
      琛?0    !endif
      琛?1   
      琛?2    "$(OBJ_PATH)\$(O)\Aes_$(TC_ARCH).obj": Aes_$(TC_ARCH).asm
      琛?3        nasm.exe -Xvc -f $(TC_OBJ_FORMAT) -Ox -D DLL_EXPORT -o "$@" -l "$(OBJ_PATH)\$(O)\Aes_$(TC_ARCH).lst" Aes_$(TC_ARCH).asm
      鍏朵腑錛屼負浜嗗噺灝戝彉鍖栭噺錛屼篃鍒╀簬浠ュ悗鎭㈠錛岀4-7琛屼負娉ㄩ噴錛岀8銆?琛屾垜灝嗛潪x86鐨勬儏鍐典篃瀹氫箟鎴恱86鐨勬儏鍐碉紝榪欐牱鏃犺濡備綍涓嬮潰絎?3琛岀殑璇彞閮藉皢鎵ц浠86浣撶郴涓虹粨鏋勭殑璁劇疆錛岃岃繖鏍風殑璁劇疆閫氬父鏄紜殑銆?br />
    2. fatal error LNK1000: Internal error during IncrBuildImage
      鎹鏄疢icrosoft Visual Studio 2008鐨勪竴涓狟UG銆俬ttp://blog.csdn.net/just_one_two/archive/2009/10/05/4634391.aspx
      鍚鏈変袱縐嶆柟娉曪紝涓縐嶆槸鏂規硶1錛岄渶瑕佷笅杞借ˉ涓侊紝鎴戞病鏈夊皾璇曘傜浜岀閫氳繃淇敼閰嶇疆鐨勬柟娉曟垜鎴愬姛浜嗭紝姝ラ濡備笅錛?br />鏂規硶錛氶」鐩?>灞炴?>閾炬帴鍣?>甯歌   涓嬮潰鐨?#8220;鍚敤澧為噺閾炬帴”錛屽皢“鏄?/INCREMENTAL)”鏀逛負“鍚?/INCREMENTAL:NO)”銆?br />涓嶈繃榪欏張寮曞叆浜嗗彟澶栦竴涓鍛婏細3>FormatCom.obj : warning LNK4075: 蹇界暐“/EDITANDCONTINUE”(鐢變簬“/INCREMENTAL:NO”瑙勮寖)
      閫夋嫨欏圭洰,灞炴?>閰嶇疆灞炴?>C/C++錛屼慨鏀?#8220;璋冭瘯淇℃伅鏍煎紡”涓?#8220;紼嬪簭鏁版嵁搴?/Zi)”鍗沖彲銆?br />
    3. 璀﹀憡錛氭湭鎵懼埌涓嬪垪鐜鍙橀噺
      4>欏圭洰 : warning PRJ0018 : 鏈壘鍒頒笅鍒楃幆澧冨彉閲?
      4>$(PKCS11_INC)
      鐢變簬欏圭洰灞炴ч噷璁劇疆鏈夐檮鍔犲寘鍚洰褰?#8220;$(PKCS11_INC)”錛屽洜姝ょ紪璇戠殑鏃跺欎細鍦ㄧ郴緇熷彉閲忛噷瀵繪壘PKCS11_INC欏圭洰錛屽鏋滄壘涓嶅埌錛屽垯緇欏嚭璀﹀憡錛屽洜姝わ紝鎴戜滑闇瑕佹墜鍔ㄨˉ鍏呰繖涓」銆傛柟娉曞悓姝ラ5錛屽鍔犱竴涓彉閲忎負PKCS11_INC錛屽鹼細D:\TrueCrypt\Common錛屽叾涓紝鍊煎氨鏄箣鍓嶆垜浠嫹璐濅笁涓枃浠訛紙pkcs11.h銆乸kcs11f.h銆乸kcs11t.h錛夌殑鐩綍銆?br />
    4. 濡傛灉涓嶅嚭鎰忓鐨勮瘽錛屼綘鍙兘榪樹細寰楀埌涓涓嬌鐢ㄤ簡PKEY_AppUserModel_ID鏈畾涔夌殑澹版槑絎︾殑閿欒銆傝繖涓槸鐢ㄤ簬鏍囪瘑鐢ㄦ埛鎬佸簲鐢ㄧ▼搴忕殑鍞竴鏍囪瘑銆備綘鍙互鍦⊿etup.h鏂囦歡涓畾涔夛細
      /*---region add by gc---*/
      #include "wtypes.h"
          const PROPERTYKEY PKEY_AppUserModel_ID = {
              {
                  (unsigned long)2009,/*unsigned long  Data1;*/
                  (unsigned short)12,/*unsigned short Data2;*/
                  (unsigned short)23,/*unsigned short Data3;*/
                  0x44,0x55,0x55,0x55,0x55,0x55,0x55,0x55
              },/*GUID fmtid;*/
              (DWORD)PID_FIRST_USABLE /*DWORD pid;*/
          };
      /*---endregion---*/
      鍏朵腑錛岃繖涓粨鏋勪綋鏄敱GUID鍜孭ID鍏卞悓緇勬垚鐨勩?br />

涓嬭澆閾炬帴

  • TrueCrypt涓嬭澆錛歨ttp://www.sfr-fresh.com/windows/misc/TrueCrypt-6.2a-Source.zip:a/Boot/Windows/Makefile
  • WinDDK涓嬭澆錛歨ttp://www.microsoft.com/whdc/DevTools/WDK/WDKpkg.mspx
  • PKCS11涓嬭澆錛歨ttp://svn.openvpn.net/projects/openvpn/test/time/openvpn/pkcs11-headers/
  • GZip涓嬭澆錛歨ttp://www.gzip.org/ 鎴栬?http://www.gzip.org/gz124src.zip
  • Nasm涓嬭澆錛歨ttp://www.nasm.us/pub/nasm/releasebuilds/2.07/win32/
  • MSVC1.52涓嬭澆錛歨ttp://download.csdn.net/source/620960 錛?5.02MB錛夛紙浼間箮緗戜笂寰堝浜洪兘鍦ㄦ壘1.52錛堟渶鍚庝竴涓彲浠ョ紪璇?6bit紼嬪簭鐨刅C緙栬瘧鍣級錛屼絾瀹樻柟緗戠珯涓婃病鏈夊叕寮涓嬭澆鐨勯摼鎺ワ紝瀹炲湪闈炲父閮侀椃錛屾垜浠嶮SDN璁㈤槄涓嬭澆錛堟敹璐圭殑鍣紝鏉叿錛夊垯鏈?7.6MB錛夛紝濡傛灉澶у瀹炲湪鎵句笉鍒頒笅杞芥垨鑰?5.02MB鐨勪笉鍙敤錛屽彲浠ヨ仈緋繪垜銆?/li>

    鍙傝冮摼鎺?/h3>
    • http://blog.csdn.net/skyremember/archive/2009/09/17/4562090.aspx
    • http://blog.sina.com.cn/s/blog_4758691d0100d8mc.html
    • http://lll332.blog.163.com/blog/static/1553692220093404635752/
    • http://msdn.microsoft.com/en-us/library/aa373931%28VS.85%29.aspx
    • http://hi.baidu.com/hhacker/blog/item/2fc5b3fb0b24132a4f4aea1d.html
    • http://blog.csdn.net/just_one_two/archive/2009/10/05/4634391.aspx
    • http://blog.csdn.net/liufei_learning/archive/2009/12/21/5047632.aspx
    • http://msdn.microsoft.com/zh-cn/library/958x11bc%28VS.80%29.aspx
    • http://bbs.xiakexing.com/cgi-bin/topic.cgi?forum=22&topic=498


    volnet 2009-12-23 23:47 鍙戣〃璇勮
    ]]>QuickSort蹇熸帓搴忔硶(2009-10-28)http://www.shnenglu.com/mymsdn/archive/2009/10/28/quicksort20091028.htmlvolnetvolnetTue, 27 Oct 2009 16:38:00 GMThttp://www.shnenglu.com/mymsdn/archive/2009/10/28/quicksort20091028.htmlhttp://www.shnenglu.com/mymsdn/comments/99624.htmlhttp://www.shnenglu.com/mymsdn/archive/2009/10/28/quicksort20091028.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/99624.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/99624.html鍦ㄦ湰鍗氬錛屼箣鍓嶆湁涓涓増鏈殑QuickSort錛屽叾瀹炵綉鍙嬪緢鏃╁氨鎻愬嚭浜嗙浉鍏崇殑BUG錛屼絾鏄垜涓鐩存病鏈夋椂闂磋繘琛屼慨姝o紝浠婂ぉ鍙堟湁鏈嬪弸鎻愬嚭浜嗚繖涓狟UG錛屾垜鍒氬ソ鎶界┖鐪嬩簡涓涓嬶紝鐪嬫潵鑷繁鏄浉褰撲笉涓ヨ皚浠ヨ嚦浜庣姱涓嬩簡濡傛澶ч敊銆?/p>

    鍘熸枃閾炬帴錛?a href="http://www.shnenglu.com/mymsdn/archive/2009/03/06/quicksort.aspx">http://www.shnenglu.com/mymsdn/archive/2009/03/06/quicksort.aspx 錛堟湭淇錛?/p>

    鍏充簬淇BUG鍚庣殑浠g爜錛屽皢鍦ㄦ湰鏂囦腑鎻愪緵錛屽師鏂囦腑涓嶈繘琛屼慨鏀癸紝浣嗕細鏈夋彁紺烘寚鏄庢槸閿欒鐨勶紝鏈夊叴瓚g殑鏈嬪弸涔熷彲浠ョ洿鎺ュ湪鍘熸枃鐨勪唬鐮佷腑瀵繪壘閿欒鏉ラ敾鐐艱嚜宸辨帓閿欑殑鑳藉姏銆?/p>

    涓嬮潰灝辨槸鍑犳浠g爜錛?/p>

    Algorithms.cpp

    #include "StdAfx.h"
    #include "Algorithms.h"
    Algorithms::Algorithms(void)
    {
    }
    
    Algorithms::~Algorithms(void)
    {
    }
    

    Algorithms.h

    #pragma once
    
    #include <iostream>
    
    class Algorithms
    {
    public:
        Algorithms(void);
        ~Algorithms(void);
    
    public:
        template <typename T>
        static void QuickSort(T* arr, size_t min, size_t max);
    private:
        template <typename T>
        static size_t qsort_helper_partition(T* arr, size_t min, size_t max);
        template <typename T>
        static inline void swap(T* arr, size_t x, size_t y);
        // helper
        template <typename T>
        static inline void helper_show_all(T* arr, size_t min, size_t max, char *msg);
    };
    
    template <typename T>
    void Algorithms::QuickSort(T* arr, size_t min, size_t max)
    {
        if(min >= max || max == 0 - 1) return;
        helper_show_all(arr, min, max, "before qsort_helper_partition");
        size_t p = qsort_helper_partition(arr, min, max);
        helper_show_all(arr, min, max, "after qsort_helper_partition");
    
        QuickSort(arr, min, p - 1);
        QuickSort(arr, p + 1, max);
    }
    /*
    *    @BUG:    bug200910280001
    *    @DESC:    鐢變簬鍦ㄥ驚鐜痺hile(true)涓紝鍋囪鍘熶唬鐮?
                01    while(true)
                02    {
                03        while(cmp < arr[i])
                04            ++i;
                05        while(arr[j] < cmp)
                06            --j;
                07        if(i >= j) break;
                08
                09        swap(arr, i, j);
                10    }
                涓紝鍓嶄袱孌碉紙琛屽彿錛岃鍙鳳級涓殑浠g爜鍧囪繑鍥瀎alse錛?
                鍒欐棤娉曡繘鍏?+i鎴栬?-j錛岄偅涔堝湪榪欎釜while(true)涓紝
                i鍜宩鐨勫煎皢鏃犳硶鍙戠敓鍙樺寲錛屼粠鑰屽鑷存寰幆銆?
        @LINK:http://www.shnenglu.com/mymsdn/archive/2009/03/06/quicksort.aspx#99606
    */
    template <typename T>
    size_t Algorithms::qsort_helper_partition(T* arr, size_t min, size_t max)
    {
        T cmp = arr[min];
        int i = min, j = max; // bug200910280001:淇i = min+1錛屽皢+1鐨勫姩浣滄斁鍦ㄥ驚鐜唴銆?
        while(true)
        {
            while(cmp < arr[++i]) // bug200910280001:灝嗗師鏈湪寰幆澶栫殑min+1錛岀Щ榪涘驚鐜唴錛屽茍棣栧厛+1
                ; // bug200910280001:灝?+1縐昏嚦while鏉′歡涓?
            while(arr[j] < cmp)
                --j;
            if(i >= j) break;
    
            helper_show_all(arr, min, max, "before swap(arr, i, j)");
            swap(arr, i, j);
            helper_show_all(arr, min, max, "after swap(arr, i, j)");
        }
        swap(arr, min, j);
        return j;
    }
    
    template <typename T>
    void Algorithms::swap(T* arr, size_t x, size_t y)
    {
        T tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }
    
    template <typename T>
    void Algorithms::helper_show_all(T* arr, size_t min, size_t max, char *msg)
    {
        std::cout << "current array :\t";
        for(int i = min; i < max; ++i)
        {
            std::cout << arr[i] << " ";
        }
        std::cout<<"\t//"<<msg;
        std::cout<<std::endl;
    }

    cpp_quickSort.cpp

    // cpp_quickSort.cpp : 瀹氫箟鎺у埗鍙板簲鐢ㄧ▼搴忕殑鍏ュ彛鐐廣?
    //
    
    #include "stdafx.h"
    #include "Algorithms.h"
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int arr_begin = 0;
        int arr_length = 12; //The length will instead of the magic numbers 
        int arr[] = {8, 3, 7, 1, 5, 6, 2, 1, 9, 9, 1, 1};
        
        // Test for : 20091028bug0001
        // int arr[] = {1, 1, 1};
    
        std::cout << "input array :\t";
        for(size_t i = arr_begin; i != arr_length; ++i)
        {
            std::cout<<arr[i]<<" ";
        }
        std::cout<<std::endl;
    
        Algorithms::QuickSort(arr, arr_begin, arr_length);
    
        std::cout << "result array :\t";
        for(size_t i = arr_begin; i != arr_length; ++i)
        {
            std::cout<<arr[i]<<" ";
        }
        std::cout<<std::endl;
    
        std::cout << "--------------------" << std::endl;
        std::cout << "input array :\t";
    
        std::vector<int> vec;
        vec.push_back(3);
        vec.push_back(1);
        vec.push_back(4);
        vec.push_back(1);
        vec.push_back(7);
        vec.push_back(6);
    
        for(std::vector<int>::iterator iter = vec.begin();
            iter != vec.end(); ++ iter)
        {
            std::cout<<*iter<<" ";
        }
        std::cout<<std::endl;
    
        std::sort(vec.begin(), vec.end());
    
        for(std::vector<int>::iterator iter = vec.begin();
            iter != vec.end(); ++ iter)
        {
            std::cout<<*iter<<" ";
        }
        std::cout<<std::endl;
    
        return 0;
    }
    
    


    volnet 2009-10-28 00:38 鍙戣〃璇勮
    ]]>
    鍏充簬NULL鍦–鍜孋++涓殑鍖哄埆http://www.shnenglu.com/mymsdn/archive/2009/05/31/about-the-NULL-in-C-or-CPlusPlus.htmlvolnetvolnetSun, 31 May 2009 12:50:00 GMThttp://www.shnenglu.com/mymsdn/archive/2009/05/31/about-the-NULL-in-C-or-CPlusPlus.htmlhttp://www.shnenglu.com/mymsdn/comments/86308.htmlhttp://www.shnenglu.com/mymsdn/archive/2009/05/31/about-the-NULL-in-C-or-CPlusPlus.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/86308.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/86308.html鍏堢湅鐪嬫爣棰樻墍鎻愬埌鐨勫唴瀹癸細
    鏍規嵁https://research.microsoft.com/en-us/um/redmond/projects/invisible/include/__defs.h.htm鏂囨。涓殑瀹氫箟錛?br>
    #if !defined(NULL) && defined(__NEEDS_NULL)
    #ifdef __cplusplus
    #define NULL    0
    #else
    #define NULL    ((void *)0)
    #endif
    #endif
    鍦–鍜孋++涓紝鍒嗗埆鏄敱(void*)0鍜?鏉ヨ〃紺虹殑銆?br>鑰?\0'鏄疉SCII涓殑鍊鹼紝ASCII涓畠鐨勫煎氨鏄?銆?br>鎵浠ュ畠浠笁鑰呮槸鐩稿悓鐨勩?br>
    鍙傝冭祫鏂欙細
    https://research.microsoft.com/en-us/um/redmond/projects/invisible/include/__defs.h.htm

    鍦ㄦ帰絀剁殑榪囩▼涓壘鍒頒笅闈㈢殑涓涓笘瀛愩傚緢鏄笉閿欙紝COPY濡備笅銆?br>杞澆鑷細http://blog.chinaunix.net/u/18517/showart_309917.html


        鏈枃杞嚜錛?a >http://bbs.chinaunix.net/viewthread.php?tid=544415&extra=&page=7
        甯栧瓙閲岃璁轟簡C璇█涓殑絀烘寚閽堛佺┖鎸囬拡甯擱噺銆丯ULL銆?絳夋蹇靛強鐩鎬簰鍏崇郴鍙婂尯鍒傝繖閲屾憳褰晈hyglinux鍏勭殑鎬葷粨銆傚仛涓爣絳撅紝鍛靛懙^_^

    1. 浠涔堟槸絀烘寚閽堝父閲忥紙null pointer constant錛?

      [6.3.2.3-3] An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant.

      榪欓噷鍛婅瘔鎴戜滑錛?銆?L銆?\0'銆? - 3銆? * 17 錛堝畠浠兘鏄?#8220;integer constant expression”錛変互鍙?(void*)0 錛?font color="#990033">tyc錛? 鎴戣寰?void*)0搴旇綆楁槸涓涓┖鎸囬拡鍚э紝鏇存伆褰撲竴鐐?/font>錛夌瓑閮芥槸絀烘寚閽堝父閲忥紙娉ㄦ剰 (char*) 0 涓嶅彨絀烘寚閽堝父閲忥紝鍙槸涓涓┖鎸囬拡鍊鹼級銆?font color="#0000ff">鑷充簬緋葷粺閫夊彇鍝褰㈠紡浣滀負絀烘寚閽堝父閲忎嬌鐢紝鍒欐槸瀹炵幇鐩稿叧鐨?/font>銆備竴鑸殑 C 緋葷粺閫夋嫨 (void*)0 鎴栬?0 鐨勫眳澶氾紙涔熸湁涓埆鐨勯夋嫨 0L錛夛紱鑷充簬 C++ 緋葷粺錛岀敱浜庡瓨鍦ㄤ弗鏍肩殑綾誨瀷杞寲鐨勮姹傦紝void* 涓嶈兘璞?C 涓偅鏍瘋嚜鐢辮漿鎹負鍏跺畠鎸囬拡綾誨瀷錛屾墍浠ラ氬父閫?0 浣滀負絀烘寚閽堝父閲忥紙tyc: C++鏍囧噯鎺ㄨ崘錛夛紝鑰屼笉閫夋嫨 (void*)0銆?br>
    2. 浠涔堟槸絀烘寚閽堬紙null pointer錛?

      [6.3.2.3-3] If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

      鍥犳錛屽鏋?p 鏄竴涓寚閽堝彉閲忥紝鍒?p = 0;銆乸 = 0L;銆乸 = '\0';銆乸 = 3 - 3;銆乸 = 0 * 17; 涓殑浠諱綍涓縐嶈祴鍊兼搷浣滀箣鍚庯紙瀵逛簬 C 鏉ヨ榪樺彲浠ユ槸 p = (void*)0;錛夛紝 p 閮芥垚涓轟竴涓┖鎸囬拡錛?font color="#0000ff">鐢辯郴緇熶繚璇佺┖鎸囬拡涓嶆寚鍚戜換浣曞疄闄呯殑瀵硅薄鎴栬呭嚱鏁?/font>銆傚弽榪囨潵璇達紝浠諱綍瀵硅薄鎴栬呭嚱鏁扮殑鍦板潃閮戒笉鍙兘鏄┖鎸囬拡銆傦紙tyc: 姣斿榪欓噷鐨?void*)0灝辨槸涓涓┖鎸囬拡銆傛妸瀹冪悊瑙d負null pointer榪樻槸null pointer constant浼氭湁寰鐨勪笉鍚岋紝褰撶劧涔熶笉鏄揣瑕佷簡錛?br>
    3. 浠涔堟槸 NULL錛?br>
      [6.3.2.3-Footnote] The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant

      鍗?NULL 鏄竴涓爣鍑嗚瀹氱殑瀹忓畾涔夛紝鐢ㄦ潵琛ㄧず絀烘寚閽堝父閲?/font>銆傚洜姝わ紝闄や簡涓婇潰鐨勫悇縐嶈祴鍊兼柟寮忎箣澶栵紝榪樺彲浠ョ敤 p = NULL; 鏉ヤ嬌 p 鎴愪負涓涓┖鎸囬拡銆傦紙tyc錛氬緢澶氱郴緇熶腑鐨勫疄鐜幫細#define NULL (void*)0錛屼笌榪欓噷鐨?#8220;a null pointer constant”騫朵笉鏄畬鍏ㄤ竴鑷寸殑錛?br>
    4. 絀烘寚閽堬紙null pointer錛夋寚鍚戜簡鍐呭瓨鐨勪粈涔堝湴鏂癸紙絀烘寚閽堢殑鍐呴儴瀹炵幇錛夛紵

      鏍囧噯騫舵病鏈夊絀烘寚閽堟寚鍚戝唴瀛樹腑鐨勪粈涔堝湴鏂硅繖涓涓棶棰樹綔鍑鴻瀹氾紝涔熷氨鏄鐢ㄥ摢涓叿浣撶殑鍦板潃鍊鹼紙0x0 鍦板潃榪樻槸鏌愪竴鐗瑰畾鍦板潃錛夎〃紺虹┖鎸囬拡鍙栧喅浜庣郴緇熺殑瀹炵幇銆傛垜浠父瑙佺殑絀烘寚閽堜竴鑸寚鍚?0 鍦板潃錛屽嵆絀烘寚閽堢殑鍐呴儴鐢ㄥ叏 0 鏉ヨ〃紺猴紙zero null pointer錛岄浂絀烘寚閽堬級錛涗篃鏈変竴浜涚郴緇熺敤涓浜涚壒孌婄殑鍦板潃鍊兼垨鑰呯壒孌婄殑鏂瑰紡琛ㄧず絀烘寚閽堬紙nonzero null pointer錛岄潪闆剁┖鎸囬拡錛夛紝鍏蜂綋璇峰弬瑙?a target="_blank">C FAQ銆?br>
      騫歌繍鐨勬槸錛屽湪瀹為檯緙栫▼涓笉闇瑕佷簡瑙e湪鎴戜滑鐨勭郴緇熶笂絀烘寚閽堝埌搴曟槸涓涓?zero null pointer 榪樻槸 nonzero null pointer錛?font color="#0000ff">鎴戜滑鍙渶瑕佷簡瑙d竴涓寚閽堟槸鍚︽槸絀烘寚閽堝氨鍙互浜嗏斺旂紪璇戝櫒浼氳嚜鍔ㄥ疄鐜板叾涓殑杞崲錛屼負鎴戜滑灞忚斀鍏朵腑鐨勫疄鐜扮粏鑺傘?/font>娉ㄦ剰錛氫笉瑕佹妸絀烘寚閽堢殑鍐呴儴琛ㄧず絳夊悓浜庢暣鏁? 0 鐨勫璞¤〃紺衡斺斿涓婃墍榪幫紝鏈夋椂瀹冧滑鏄笉鍚岀殑銆?br>
    5. 濡備綍鍒ゆ柇涓涓寚閽堟槸鍚︽槸涓涓┖鎸囬拡錛?/font>

      榪欏彲浠ラ氳繃涓庣┖鎸囬拡甯擱噺鎴栬呭叾瀹冪殑絀烘寚閽堢殑姣旇緝鏉ュ疄鐜幫紙娉ㄦ剰涓庣┖鎸囬拡鐨勫唴閮ㄨ〃紺烘棤鍏籌級銆備緥濡傦紝鍋囪 p 鏄竴涓寚閽堝彉閲忥紝q 鏄竴涓悓綾誨瀷鐨勭┖鎸囬拡錛岃媯鏌?p 鏄惁鏄竴涓┖鎸囬拡錛屽彲浠ラ噰鐢ㄤ笅鍒椾換鎰忓艦寮忎箣涓鈥斺斿畠浠湪瀹炵幇鐨勫姛鑳戒笂閮芥槸絳変環鐨勶紝鎵涓嶅悓鐨勫彧鏄鏍肩殑宸埆銆?br>
      鎸囬拡鍙橀噺 p 鏄┖鎸囬拡鐨勫垽鏂細
      if ( p == 0 )
      if ( p == '\0' )
      if ( p == 3 - 3 )
      if ( p == NULL )  /* 浣跨敤 NULL 蹇呴』鍖呭惈鐩稿簲鐨勬爣鍑嗗簱鐨勫ご鏂囦歡 */
      if ( NULL == p )
      if ( !p )
      if ( p == q )
      ...

      鎸囬拡鍙橀噺 p 涓嶆槸絀烘寚閽堢殑鍒ゆ柇錛?br>if ( p != 0 )
      if ( p != '\0' )
      if ( p != 3 - 3 )
      if ( p != NULL )  /* 浣跨敤 NULL 蹇呴』鍖呭惈鐩稿簲鐨勬爣鍑嗗簱鐨勫ご鏂囦歡 */
      if ( NULL != p )
      if ( p )
      if ( p != q )
      ...

    6. 鍙互鐢?memset 鍑芥暟鏉ュ緱鍒頒竴涓┖鎸囬拡鍚楋紵

      榪欎釜闂絳夊悓浜庯細濡傛灉 p 鏄竴涓寚閽堝彉閲忥紝閭d箞

      memset( &p, 0, sizeof(p) ); 鍜?p = 0;

      鏄瓑浠風殑鍚楋紵

      絳旀鏄惁瀹氱殑錛岃櫧鐒跺湪澶у鏁扮郴緇熶笂鏄瓑浠風殑錛屼絾鏄洜涓烘湁鐨勭郴緇熷瓨鍦ㄧ潃“闈為浂絀烘寚閽?#8221; 錛坣onzero null pointer錛夛紝鎵浠ヨ繖鏃朵袱鑰呬笉絳変環銆傜敱浜庤繖涓師鍥狅紝瑕佹敞鎰忓綋鎯沖皢鎸囬拡璁劇疆涓虹┖鎸囬拡鐨勬椂鍊欎笉搴旇浣跨敤 memset錛岃屽簲璇ョ敤絀烘寚閽堝父閲忔垨絀烘寚閽堝鎸囬拡鍙橀噺璧嬪兼垨鑰呭垵濮嬪寲鐨勬柟娉曘?br>
    7. 鍙互瀹氫箟鑷繁鐨?NULL 鐨勫疄鐜板悧錛熷吋絳?NULL 鐨勫煎彲浠ユ槸 1銆?銆? 絳夊煎悧錛?綾諱技闂

      [7.1.3-2] If the program declares or defines an identifier in a context in which it is reserved (other than as allowed by 7.1.4), or defines a reserved identifier as a macro name, the behavior is undefined.

      NULL 鏄爣鍑嗗簱涓殑涓涓鍚堜笂榪版潯浠剁殑 reserved identifier 錛堜繚鐣欐爣璇嗙錛夈傛墍浠ワ紝濡傛灉鍖呭惈浜嗙浉搴旂殑鏍囧噯澶存枃浠惰屽紩鍏ヤ簡 NULL 鐨勮瘽錛屽垯鍐嶅湪紼嬪簭涓噸鏂板畾涔?NULL 涓轟笉鍚岀殑鍐呭鏄潪娉曠殑錛屽叾琛屼負鏄湭瀹氫箟鐨勩備篃灝辨槸璇達紝濡傛灉鏄鍚堟爣鍑嗙殑紼嬪簭錛屽叾 NULL 鐨勫煎彧鑳芥槸 0錛屼笉鍙兘鏄櫎 0 涔嬪鐨勫叾瀹冨鹼紝姣斿 1銆?銆? 絳夈?br>
    8. malloc 鍑芥暟鍦ㄥ垎閰嶅唴瀛樺け璐ユ椂榪斿洖 0 榪樻槸 NULL錛?br>
      malloc 鍑芥暟鏄爣鍑?C 瑙勫畾鐨勫簱鍑芥暟銆傚湪鏍囧噯涓槑紜瀹氫簡鍦ㄥ叾鍐呭瓨鍒嗛厤澶辮觸鏃惰繑鍥炵殑鏄竴涓?“null pointer”錛堢┖鎸囬拡錛夛細

      [7.20.3-1] If the space cannot be allocated, a null pointer is returned.

      瀵逛簬絀烘寚閽堝鹼紝涓鑸殑鏂囨。錛堟瘮濡? man錛変腑鍊懼悜浜庣敤 NULL 琛ㄧず錛岃屾病鏈夌洿鎺ヨ鎴?0銆備絾鏄垜浠簲璇ユ竻妤氾細瀵逛簬鎸囬拡綾誨瀷鏉ヨ錛岃繑鍥?NULL 鍜?榪斿洖 0 鏄畬鍏ㄧ瓑浠風殑錛屽洜涓?NULL 鍜?0 閮借〃紺?“null pointer”錛堢┖鎸囬拡錛夈傦紙tyc錛氫竴鑸郴緇熶腑鎵嬪唽涓兘榪斿洖NULL錛岄偅鎴戜滑灝辯敤NULL鍚?/font>錛?/li>

    鍙﹀錛岄檮C FAQ涓婂叧浜巒ull pointer鐨勮В閲婏細C FAQ錛歯ull pointer




    volnet 2009-05-31 20:50 鍙戣〃璇勮
    ]]>
    濡備綍鏌ョ湅浣犲彲浠ュ垎閰嶅澶у唴瀛?/title><link>http://www.shnenglu.com/mymsdn/archive/2009/05/25/85667.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Sun, 24 May 2009 17:37:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2009/05/25/85667.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/85667.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2009/05/25/85667.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/85667.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/85667.html</trackback:ping><description><![CDATA[<pre class="gc-code"><span style="color: #7f0055">#include </span><span style="color: #2a00ff"><stdio.h> </span><span style="color: #7f0055">#include </span><span style="color: #2a00ff"><stdlib.h> </span><span style="color: #7f0055">int </span>main(<span style="color: #7f0055">void</span>){ <span style="color: #7f0055">int </span>MB = 0; <span style="color: #7f0055">while</span>(<span style="color: #642880">malloc</span>(1 << 20)) ++MB; <span style="color: #642880">printf</span>(<span style="color: #2a00ff">"Allocated %d MB total\n"</span>, MB); <span style="color: #7f0055">return </span>EXIT_SUCCESS; }</pre> <p>鍘熺悊錛屽洜涓?鐨?0嬈℃柟灝辨槸1MB錛?鐨?0嬈℃柟涓?KB錛?鐨?0嬈℃柟涓?GB錛屼互姝ょ被鎺級銆?/p> <p>濡傛灉浣犺姹傚垎閰嶇殑鍐呭瓨鍧楀皬浜?MB錛屼綘寰楀埌鐨勫唴瀛樻槸鍚︽瘮榪欒澶氫竴浜涘憿錛熶負浠涔堬紵</p> <p>絳旓細</p> <p>榪欎笉鏄粷瀵圭殑錛?/p> <p>鍦ㄦ湰渚嬩腑浣跨敤</p> <p>1<<22錛?MB錛夊緱鍒扮殑緇撴灉涔?鏄?000MB</p> <p>1<<21錛?MB錛夊緱鍒扮殑緇撴灉涔?鏄?972MB</p> <p>1<<20錛?MB錛夊緱鍒扮殑緇撴灉鏄?918MB</p> <p>1<<19錛?.5MB錛夊緱鍒扮殑緇撴灉闄?鏄?812MB</p> <p>1<<18錛?.25MB錛夊緱鍒扮殑緇撴灉闄?鏄?003MB</p> <p>1<<17錛?.125MB錛夊緱鍒扮殑緇撴灉闄?鏄?034MB</p> <p>鏄劇劧鍑虹幇浜嗕竴涓剰澶栫殑緇撴灉銆?/p><img src ="http://www.shnenglu.com/mymsdn/aggbug/85667.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2009-05-25 01:37 <a href="http://www.shnenglu.com/mymsdn/archive/2009/05/25/85667.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>static in Chttp://www.shnenglu.com/mymsdn/archive/2009/05/24/85626.htmlvolnetvolnetSun, 24 May 2009 10:17:00 GMThttp://www.shnenglu.com/mymsdn/archive/2009/05/24/85626.htmlhttp://www.shnenglu.com/mymsdn/comments/85626.htmlhttp://www.shnenglu.com/mymsdn/archive/2009/05/24/85626.html#Feedback3http://www.shnenglu.com/mymsdn/comments/commentRss/85626.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/85626.html

    #include <stdio.h>
    #include <stdlib.h>
    
    char * favorite_fruit1(void);
    char * favorite_fruit2(void);
    void favorite_fruit3(char **);
    int main(void) {
        char * fruit1 = favorite_fruit1();
        printf("%s\n", fruit1);
    
        char * fruit2 = favorite_fruit2();
        printf("%s\n", fruit2);
    
        char * fruit3 = NULL;
        favorite_fruit3(&fruit3);
        printf("%s\n", fruit3);
    
        printf("------END of CODE------");
        return EXIT_SUCCESS;
    }
    char * favorite_fruit1(void){
        char deciduous[] = "apple";
        return deciduous;
    }
    char * favorite_fruit2(void){
        static char deciduous[] = "apple";
        return deciduous;
    }
    void favorite_fruit3(char ** fruit){
        static char deciduous[] = "apple";
        *fruit = deciduous;
    }

    favorite_fruit1寰堟槑鏄句細鍑虹幇闂錛屽師鍥犳槸鍥犱負char deciduous[]鏄眬閮ㄥ彉閲忥紝鍦ㄥ嚱鏁拌皟鐢ㄨ繑鍥炲悗錛屽氨閲婃斁浜嗐?/p>

    favorite_fruit2鍥犱負浣跨敤浜唖tatic錛岃宻tatic闄愬畾浜嗗彉閲忚淇濆瓨鍦ㄦ暟鎹錛坉ata segment錛変腑錛屽畠鐨勫0鏄庡懆鏈熷悓紼嬪簭涓鏍烽暱銆傛墍浠ヤ笉浼氬嚭閿欍?/p>

    favorite_fruit3鏄彟涓縐嶆湁鏁堢殑鍐欐硶錛屽叾鍘熺悊鍚?銆?/p>

    volnet 2009-05-24 18:17 鍙戣〃璇勮
    ]]>
    [C涓撳緙栫▼]瀛︿範絎旇鈥斺攁.out錛屾(segment)錛屾煡鐪嬪彲鎵ц鏂囦歡涓殑孌?/title><link>http://www.shnenglu.com/mymsdn/archive/2009/05/19/expert-c-programming-a-out-segment.html</link><dc:creator>volnet</dc:creator><author>volnet</author><pubDate>Tue, 19 May 2009 15:50:00 GMT</pubDate><guid>http://www.shnenglu.com/mymsdn/archive/2009/05/19/expert-c-programming-a-out-segment.html</guid><wfw:comment>http://www.shnenglu.com/mymsdn/comments/83424.html</wfw:comment><comments>http://www.shnenglu.com/mymsdn/archive/2009/05/19/expert-c-programming-a-out-segment.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.shnenglu.com/mymsdn/comments/commentRss/83424.html</wfw:commentRss><trackback:ping>http://www.shnenglu.com/mymsdn/services/trackbacks/83424.html</trackback:ping><description><![CDATA[鏈儴鍒嗗唴瀹硅瑙併奀涓撳緙栫▼銆婸119<br>鏌ョ湅鍙墽琛屾枃浠朵腑鐨勬<br>1.緙栬瘧“hello world”紼嬪簭錛屽湪鍙墽琛屾枃浠朵腑鎵цls -l錛屽緱鍒版枃浠剁殑鎬諱綋澶у皬銆傝繍琛宻ize寰楀埌鏂囦歡閲屽悇涓鐨勫ぇ灝忋?br>2.澧炲姞涓涓叏灞鐨刬nt[1000]鏁扮粍澹版槑錛岄噸鏂拌繘琛岀紪璇戯紝鍐嶇敤涓婇潰鐨勫懡浠ゅ緱鍒版諱綋鍙婂悇涓鐨勫ぇ灝忥紝娉ㄦ剰鍓嶅悗鐨勫尯鍒?br>3.鐜板湪錛屽湪鏁扮粍鐨勫0鏄庝腑澧炲姞鍒濆鍊鹼紙璁頒綇錛孋璇█騫朵笉寮鴻揩瀵規暟緇勮繘琛屽垵濮嬪寲鏃朵負姣忎釜鍏冪礌鎻愪緵鍒濆鍊鹼級銆傝繖灝嗕嬌鏁扮粍浠嶣SS孌佃漿鎹㈠埌鏁版嵁孌點傞噸澶嶄笂闈㈢殑嫻嬮噺錛屾敞鎰忓悇涓鍓嶅悗澶у皬鐨勫尯鍒?br>4.鐜板湪錛屽湪鍑芥暟鍐呭0鏄庝竴涓法澶х殑鏁扮粍銆傜劧鍚庡啀澹版槑涓涓法澶х殑灞閮ㄦ暟緇勶紝浣嗚繖嬈″姞涓婂垵濮嬪箋傞噸澶嶄笂闈㈢殑嫻嬮噺銆傚畾涔変簬鍑芥暟鍐呴儴鐨勫眬閮ㄦ暟緇勫瓨鍌ㄥ湪鍙墽琛屾枃浠朵腑鍚楋紵鏈夋病鏈夊垵濮嬪寲鏈変粈涔堜笉鍚屽悧錛?br>5.濡傛灉鍦ㄨ皟璇曠姸鎬佷笅緙栬瘧錛屾枃浠跺拰孌電殑澶у皬鏈夋病鏈夊彉鍖栵紵鏄負浜嗘渶澶х▼搴︾殑浼樺寲鍚楋紵<br>鍒嗘瀽涓婇潰“緙栫▼鎸戞垬”鐨勭粨鏋滐紝浣胯嚜宸辯‘淇★細<br> <ul> <li>鏁版嵁孌典繚瀛樺湪鐩爣鏂囦歡涓?/li> <li>BSS孌典笉淇濆瓨鍦ㄧ洰鏍囨枃浠朵腑錛堥櫎浜嗚褰旴SS孌靛湪榪愯鏃舵墍闇瑕佺殑澶у皬錛夈?/li> <li>鏂囨湰孌墊槸鏈瀹規槗鍙椾紭鍖栨帾鏂藉獎鍝嶇殑孌點?/li> <li>a.out鏂囦歡鐨勫ぇ灝忓彈璋冭瘯鐘舵佷笅緙栬瘧鐨勫獎鍝嶏紝浣嗘涓嶅彈褰卞搷銆?/li> </ul> helloworld.c鐨勬枃浠舵渶緇堝涓嬶細<br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">#include </span><span style="color: #000000;"><</span><span style="color: #000000;">stdio.h</span><span style="color: #000000;">></span><span style="color: #000000;"><br></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> global_array[</span><span style="color: #000000;">1000</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> {</span><span style="color: #000000;">100</span><span style="color: #000000;">, </span><span style="color: #000000;">101</span><span style="color: #000000;">, </span><span style="color: #000000;">102</span><span style="color: #000000;">};<br></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> SayGoodBye(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">);<br></span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)<br>{<br>        printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">hello Linux world</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>        </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> main_array[</span><span style="color: #000000;">1000</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> {</span><span style="color: #000000;">10</span><span style="color: #000000;">, </span><span style="color: #000000;">20</span><span style="color: #000000;">, </span><span style="color: #000000;">30</span><span style="color: #000000;">, </span><span style="color: #000000;">40</span><span style="color: #000000;">};<br>        SayGoodBye();<br>        </span><span style="color: #0000ff;">return</span><span style="color: #000000;"> </span><span style="color: #000000;">0</span><span style="color: #000000;">;<br>}<br><br></span><span style="color: #0000ff;">void</span><span style="color: #000000;"> SayGoodBye(</span><span style="color: #0000ff;">void</span><span style="color: #000000;">)<br>{<br>        </span><span style="color: #0000ff;">int</span><span style="color: #000000;"> goodbye_array[</span><span style="color: #000000;">1000</span><span style="color: #000000;">] </span><span style="color: #000000;">=</span><span style="color: #000000;"> {</span><span style="color: #000000;">1024</span><span style="color: #000000;">, </span><span style="color: #000000;">2048</span><span style="color: #000000;">, </span><span style="color: #000000;">4096</span><span style="color: #000000;">};<br>        printf(</span><span style="color: #000000;">"</span><span style="color: #000000;">good bye user!</span><span style="color: #000000;">"</span><span style="color: #000000;">);<br>}<br></span></div> <br>浠ヤ笅鍛戒護鎸夌収鏂囦腑姝ラ渚濇淇敼涓婃枃浠g爜錛堣繃紼嬩笉璧樿堪錛夛紝榪愯size鍚庣殑鏁版嵁濡備笅鎵紺猴細<br> <div style="border: 1px solid #cccccc; padding: 4px 5px 4px 4px; background-color: #eeeeee; font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: #000000;">[volnet@GoCool c]$ echo </span><span style="color: #000000;">"</span><span style="color: #000000;">size helloworld1</span><span style="color: #000000;">"</span><span style="color: #000000;">; size helloworld1; echo </span><span style="color: #000000;">"</span><span style="color: #000000;">size helloworld2</span><span style="color: #000000;">"</span><span style="color: #000000;">; size helloworld2; echo </span><span style="color: #000000;">"</span><span style="color: #000000;">size helloworld3</span><span style="color: #000000;">"</span><span style="color: #000000;">; size helloworld3; echo </span><span style="color: #000000;">"</span><span style="color: #000000;">size helloworld4</span><span style="color: #000000;">"</span><span style="color: #000000;">; size helloworld4;<br>size helloworld1<br>   text       data        bss        dec        hex    filename<br>   </span><span style="color: #000000;">1015</span><span style="color: #000000;">        </span><span style="color: #000000;">252</span><span style="color: #000000;">          </span><span style="color: #000000;">8</span><span style="color: #000000;">       </span><span style="color: #000000;">1275</span><span style="color: #000000;">        4fb    helloworld1<br>size helloworld2<br>   text       data        bss        dec        hex    filename<br>   </span><span style="color: #000000;">1015</span><span style="color: #000000;">        </span><span style="color: #000000;">252</span><span style="color: #000000;">       </span><span style="color: #000000;">4032</span><span style="color: #000000;">       </span><span style="color: #000000;">5299</span><span style="color: #000000;">       14b3    helloworld2<br>size helloworld3<br>   text       data        bss        dec        hex    filename<br>   </span><span style="color: #000000;">1015</span><span style="color: #000000;">       </span><span style="color: #000000;">4280</span><span style="color: #000000;">          </span><span style="color: #000000;">8</span><span style="color: #000000;">       </span><span style="color: #000000;">5303</span><span style="color: #000000;">       14b7    helloworld3<br>size helloworld4<br>   text       data        bss        dec        hex    filename<br>   </span><span style="color: #000000;">1270</span><span style="color: #000000;">       </span><span style="color: #000000;">4280</span><span style="color: #000000;">          </span><span style="color: #000000;">8</span><span style="color: #000000;">       </span><span style="color: #000000;">5558</span><span style="color: #000000;">       15b6    helloworld4<br></span></div> <br>娉細<br>BSS鐨勫叏縐版槸錛欱lock Started by Symbol錛堢敱絎﹀彿寮濮嬬殑鍧楋級錛屽畠鏄棫寮廔BM704姹囩紪紼嬪簭鐨勪竴涓吉鎸囦護錛孶NIX鍊熺敤浜嗚繖涓悕瀛楋紝鑷充粖渚濈劧娌跨敤銆?br> <img src ="http://www.shnenglu.com/mymsdn/aggbug/83424.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.shnenglu.com/mymsdn/" target="_blank">volnet</a> 2009-05-19 23:50 <a href="http://www.shnenglu.com/mymsdn/archive/2009/05/19/expert-c-programming-a-out-segment.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>Expert C Programming noteshttp://www.shnenglu.com/mymsdn/archive/2009/04/01/78518.htmlvolnetvolnetTue, 31 Mar 2009 16:31:00 GMThttp://www.shnenglu.com/mymsdn/archive/2009/04/01/78518.htmlhttp://www.shnenglu.com/mymsdn/comments/78518.htmlhttp://www.shnenglu.com/mymsdn/archive/2009/04/01/78518.html#Feedback0http://www.shnenglu.com/mymsdn/comments/commentRss/78518.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/78518.html1銆乧onst鍏跺疄騫朵笉鏄湡姝g殑甯擱噺錛圥32錛?/p>

    const_is_not_constant

    2銆佹棭鏈熺殑gets()涓殑Bug瀵艱嚧浜咺nternet锠曡櫕錛圥42錛?/p>

    gets()鍑芥暟騫朵笉媯鏌ョ紦鍐插尯鐨勭┖闂達紝浜嬪疄涓婂畠涔熸棤娉曟鏌ョ紦鍐插尯鐨勭┖闂淬傚鏋滃嚱鏁扮殑璋冪敤鑰呮彁渚涗簡涓涓寚鍚戝爢鏍堢殑鎸囬拡錛屽茍涓攇ets()鍑芥暟璇誨叆鐨勫瓧絎︽暟閲忚秴榪囩紦鍐插尯鐨勭┖闂達紝gets()鍑芥暟灝嗕細鎰夊揩鍦板皢澶氬嚭鏉ョ殑瀛楃緇х畫鍐欏叆鍒板爢鏍堜腑錛岃繖灝辮鐩栦簡鍫嗘爤鍘熷厛鐨勫唴瀹廣傗斺旇繖灝辨槸鐥呮瘨鍒╃敤瀹冩潵鍐欏叆棰濆絀洪棿錛屽茍寮曞彂锠曡櫕鐥呮瘨鐨勫墠鎻愩?/p>

    鎺ㄨ崘鐨勬柟寮忔槸灝?/p>

    gets(line)

    鏇挎崲涓?/p>

    if(fgets(line, sizeof(line), stdin) == NULL)

    exit(1);

    3銆佺浉閭誨瓧絎︿覆甯擱噺鑷姩榪炴帴錛圥45錛?/p>

    榪欎釜鍏跺疄宸茬粡搴旂敤寰堟櫘閬嶄簡錛屼絾鏄垜涓漢鐢ㄧ殑姣旇緝灝戯紝鐗規璁板綍涓涓嬨?/p>

    ansi-c_connect_string

    4銆佽繑鍥炰竴涓寚閽堬紵錛圥48錛?/p>

    榪欎釜璇濋鍥寸粫涓涓▼搴忕殑BUG鏉ュ睍寮錛岃繖涓▼搴忚繑鍥炰簡灞閮ㄥ彉閲忕殑鍊肩殑鎸囬拡錛岃繖涔堣褰撶劧浣犱竴鐪煎氨鑳界湅寰楀嚭鏉ラ棶棰樻墍鍦紝浣嗘槸鍦ㄥ緢澶氭椂鍊欙紝榪欎釜閿欒鍗存繪槸鍦ㄤ綘鐨勭溂鐨瓙搴曚笅婧滆蛋銆?/p>

    浣滆呮彁渚涗簡浜旂鏂瑰紡錛屽彧鑳借鍙互鐢紝浣嗗敮涓鎺ㄨ崘鐨勫彧鏈変竴涓紝璇﹁浣滆呯殑鍒嗘瀽錛圥48錛夛紙涓嶆槸浠涔堥珮娣辯殑鐞嗚錛屼綘鑷繁涔熻兘鍒嗘瀽鍦板嚭鏉ワ級銆?/p>

    a.榪斿洖涓涓瓧絎︿覆甯擱噺鐨勬寚閽堛傚洜涓哄父閲忓瓨鍦ㄩ潤鎬佹暟鎹瓨鍌ㄥ尯錛屾墍浠ユ寚閽堟病闂銆?/p>

    b.浣跨敤鍏ㄥ眬澹版槑鐨勬暟緇勩傛彁鍒板叏灞涓や釜瀛楋紝灝辯煡閬撹繖涓柟娉曟湁寰堝ぇ鐨勫眬闄愭с?/p>

    c.浣跨敤闈欐佹暟緇勩備笅涓嬈¤皟鐢ㄥ皢瑕嗙洊榪欎釜鏁扮粍鍐呭銆?/p>

    char * func() {

    static char buffer[20];

    鈥?/p>

    return buffer;

    }

    d.鏄懼紡鍒嗛厤涓浜涘唴瀛橈紝淇濆瓨榪斿洖鐨勫箋?/p>

    char  * func() {

    char * s = malloc(120);

    鈥?/p>

    return s;

    }

    鏃㈢劧鐢ㄥ埌浜唌alloc錛屽氨蹇呯劧浼撮殢鐫free錛屽洜姝ゅ甫鏉ヤ簡鍐呭瓨綆$悊鐨勯棶棰橈紝澧炲姞浜嗗紑鍙戣呰礋鎷呫?/p>

    e.錛?u>鎺ㄨ崘錛夊湪璋冪敤鍓嶅悗錛岀敱鍑芥暟璋冪敤鑰呭垎閰嶅唴瀛橈紝騫剁敱鍏墮噴鏀撅紝鍦ㄥ悓涓鍦版柟閲婃斁瀵逛簬鍐呭瓨綆$悊鏉ヨ浠d環鐩稿鏈灝忋?/p>

    void func( char * result, int size) {

    鈥?/p>

    strncpy(result, 鈥淭hat鈥檇 be in the data segment, Bob鈥? size);

    }

    buffer = malloc(size);

    func(buffer, size);

    鈥?/p>

    free(buffer);



    volnet 2009-04-01 00:31 鍙戣〃璇勮
    ]]>
    QuickSort蹇熸帓搴忔硶(2009-03-06)http://www.shnenglu.com/mymsdn/archive/2009/03/06/quicksort.htmlvolnetvolnetThu, 05 Mar 2009 19:03:00 GMThttp://www.shnenglu.com/mymsdn/archive/2009/03/06/quicksort.htmlhttp://www.shnenglu.com/mymsdn/comments/75693.htmlhttp://www.shnenglu.com/mymsdn/archive/2009/03/06/quicksort.html#Feedback5http://www.shnenglu.com/mymsdn/comments/commentRss/75693.htmlhttp://www.shnenglu.com/mymsdn/services/trackbacks/75693.html var totalConfirmTime = 10; var currentConfirmTime = 0; var enableNoticeReader = true; var enableCloseNotice = true; function onRedirect20091028(){ if(enableNoticeReader && currentConfirmTime < totalConfirmTime) { if(confirm("鎮ㄥ凡緇忓湪鏈〉闈㈤楃暀"+ 10*(++currentConfirmTime) +"縐掞紝鏈〉闈唬鐮佸瓨鍦˙UG錛乗n鏄惁紜璺寵漿鍒版紜唬鐮侀〉闈€奞uickSort蹇熸帓搴忔硶(2009-10-28)銆嬶紵")) { window.location.href="http://www.shnenglu.com/mymsdn/archive/2009/10/28/quicksort20091028.html?source=20090306&method=setTimeOut" } else { if(enableCloseNotice) { if(confirm("鎴戜滑灝嗘渶澶氭彁紺烘偍"+ totalConfirmTime +"嬈°俓n濡傛灉鎮ㄥ喅瀹氱戶緇楃暀鍦ㄦ湰欏碉紝鏄惁闇瑕佸叧闂〉闈㈡彁閱掞紵\n錛堢偣鍑燴滄槸鈥濆嵆鍙叧闂紒錛?)) { enableNoticeReader = false; } else { setTimeout(onRedirect20091028, 10000); } // only 1 time. enableCloseNotice = false; } else { setTimeout(onRedirect20091028, 10000); } } } } function closeEnableNoticeReader(){ enableNoticeReader = false; } setTimeout(onRedirect20091028, 10000);

    鏈枃浠g爜宸茬粡鏇存柊錛屼慨姝d弗閲岯UG錛屾渶鏂扮増鏈瑙併?a href="http://www.shnenglu.com/mymsdn/archive/2009/10/28/quicksort20091028.html">QuickSort蹇熸帓搴忔硶(2009-10-28)銆嬶紒

    鏈枃涓墍娑夊強鐨勪唬鐮佹湭鍋氭洿鏂幫紝璇風Щ姝ユ渶鏂扮増鏌ラ槄姝g‘浠g爜錛?/p>

    閾炬帴錛?a title="http://www.shnenglu.com/mymsdn/archive/2009/10/28/quicksort20091028.html" href="http://www.shnenglu.com/mymsdn/archive/2009/10/28/quicksort20091028.html">http://www.shnenglu.com/mymsdn/archive/2009/10/28/quicksort20091028.html


    蹇熸帓搴忔硶錛氾紙濂藉湡錛屾劅瑙夋弧涓栫晫閮戒細錛屼笉榪囪繕鏄啓涓涓嬶紝褰撶劧浜嗭紝鏍囧噯搴撻噷澶氱殑鏄帓搴忕畻娉曪級錛岃繖閲岃繕鏄疄鐜扮粡鍏哥増鐨勫揩閫熸帓搴忎簡錛屾椂闂村鏉傚害O(nlogn)

    Algorithms.h

    #pragma once
    
    #include <iostream>
    
    class Algorithms
    {
    public:
        Algorithms(void);
        ~Algorithms(void);
    
    public:
        template <typename T>
        static void QuickSort(T* arr, size_t min, size_t max);
    private:
        template <typename T>
        static size_t qsort_helper_partition(T* arr, size_t min, size_t max);
        template <typename T>
        static inline void swap(T* arr, size_t x, size_t y);
    };
    
    template <typename T>
    void Algorithms::QuickSort(T* arr, size_t min, size_t max)
    {
        if(min >= max || max == 0 - 1) return;
        size_t p = qsort_helper_partition(arr, min, max);
    
        QuickSort(arr, min, p - 1);
        QuickSort(arr, p + 1, max);
    }
    
    template <typename T>
    size_t Algorithms::qsort_helper_partition(T* arr, size_t min, size_t max)
    {
        T cmp = arr[min];
        int i = min + 1, j = max;
        while(true)
        {
            while(cmp < arr[i])
                ++i;
            while(arr[j] < cmp)
                --j;
            if(i >= j) break;
    
            swap(arr, i, j);
        }
        swap(arr, min, j);
        return j;
    }
    
    template <typename T>
    void Algorithms::swap(T* arr, size_t x, size_t y)
    {
        T tmp = arr[x];
        arr[x] = arr[y];
        arr[y] = tmp;
    }

    鐢ㄦ硶錛氾紙欏轟究鏈夋爣鍑嗗簱鐨勬帓搴忔硶錛屽綋鐒跺彧鏄皟涓涓嬶紝娌℃湁浠涔堝彲璇寸殑浜嗭級

    #include "Algorithms.h"
    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        int arr[] = {4, 8, 3, 7, 1, 5, 6, 2};
    
        for(size_t i = 0; i != 8; ++i)
        {
            std::cout<<arr[i]<<" ";
        }
        std::cout<<std::endl;
    
        Algorithms::QuickSort(arr,0, 7);
    
        for(size_t i = 0; i != 8; ++i)
        {
            std::cout<<arr[i]<<" ";
        }
        std::cout<<std::endl;
    
        std::vector<int> vec;
        vec.push_back(3);
        vec.push_back(1);
        vec.push_back(4);
        vec.push_back(1);
        vec.push_back(7);
        vec.push_back(6);
    
        for(std::vector<int>::iterator iter = vec.begin();
            iter != vec.end(); ++ iter)
        {
            std::cout<<*iter<<" ";
        }
        std::cout<<std::endl;
    
        std::sort(vec.begin(), vec.end());
    
        for(std::vector<int>::iterator iter = vec.begin();
            iter != vec.end(); ++ iter)
        {
            std::cout<<*iter<<" ";
        }
        std::cout<<std::endl;
    
        return 0;
    }
    
    


    volnet 2009-03-06 03:03 鍙戣〃璇勮
    ]]>
    青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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热软件| 欧美—级高清免费播放| 欧美激情一区二区三区在线视频观看 | 久久综合狠狠综合久久综青草| 国产精品乱人伦中文| 最新国产の精品合集bt伙计| 狠狠久久亚洲欧美| 欧美中文字幕第一页| 欧美伊人久久大香线蕉综合69| 欧美午夜影院| 宅男精品视频| 亚洲一区日韩| 国产精品theporn| 夜夜嗨av一区二区三区四区| 亚洲一区二区三区四区视频| 国产精品jizz在线观看美国| 亚洲欧洲午夜| 亚洲免费视频中文字幕| 国产精品少妇自拍| 性高湖久久久久久久久| 久久欧美中文字幕| 亚洲电影免费观看高清| 欧美电影在线观看| 亚洲伦理精品| 欧美一区2区三区4区公司二百| 欧美一区在线看| 国内精品一区二区三区| 久久精品一级爱片| 91久久精品日日躁夜夜躁欧美| 一区二区高清视频在线观看| 国产精品久久久久久亚洲毛片| 亚洲欧美美女| 欧美国产精品日韩| 一本大道久久a久久精品综合| 欧美午夜视频在线| 欧美在线一二三四区| 欧美www视频| 亚洲婷婷在线| 国内精品久久久久久久影视蜜臀| 美女在线一区二区| 9色porny自拍视频一区二区| 久久久国产亚洲精品| 亚洲欧洲日本一区二区三区| 欧美性事在线| 久久影视精品| 在线午夜精品| 欧美成人一区二区| 99精品国产高清一区二区 | 国产精品一香蕉国产线看观看| 欧美中文字幕视频| 91久久精品国产91久久性色| 亚洲欧美日韩爽爽影院| 亚洲高清成人| 国产精品无人区| 欧美激情第10页| 欧美一区激情视频在线观看| 亚洲精品国产精品乱码不99 | 亚洲天堂av高清| 国内久久视频| 欧美日韩精品免费观看视一区二区 | 久久人人97超碰国产公开结果| 亚洲国产精品福利| 国产精品伊人日日| 欧美日韩国产高清| 久久久久久网| 亚洲综合欧美日韩| 亚洲精品小视频| 狂野欧美激情性xxxx欧美| 亚洲一区欧美| 日韩一级免费| 亚洲高清视频在线观看| 国产日韩欧美在线看| 国产精品h在线观看| 欧美电影免费| 欧美91精品| 久久色中文字幕| 久久国产精品电影| 亚洲男女自偷自拍| 亚洲一区3d动漫同人无遮挡| 亚洲精品裸体| 亚洲精品久久视频| 欧美激情中文字幕一区二区| 久久久久久噜噜噜久久久精品| 午夜精品视频| 亚洲一区尤物| 亚洲私人影吧| 亚洲午夜一二三区视频| 一本久久青青| 亚洲视频综合| 亚洲一级二级| 亚洲一区免费在线观看| 亚洲一区二区三区午夜| 亚洲性夜色噜噜噜7777| 亚洲一区二区三区视频| 亚洲制服欧美中文字幕中文字幕| 一本到高清视频免费精品| 日韩视频―中文字幕| 亚洲另类一区二区| 亚洲视频你懂的| 亚洲欧美成人综合| 亚洲一区二区动漫| 先锋影音网一区二区| 久久成人资源| 老司机免费视频久久| 麻豆精品在线视频| 亚洲国产精品视频一区| 亚洲免费电影在线| 亚洲欧美99| 久久精品男女| 欧美成人日韩| 欧美啪啪成人vr| 国产精品网站在线播放| 国内精品久久久久伊人av| 亚洲国产另类久久久精品极度| 91久久国产综合久久| 一区二区欧美日韩| 久久爱www久久做| 米奇777在线欧美播放| 亚洲国产91色在线| 这里只有精品电影| 久久久久.com| 欧美高清视频一区二区| 欧美日韩一区二区三区四区五区 | 亚洲无吗在线| 欧美制服丝袜| 欧美激情一二三区| 国产精品亚洲综合一区在线观看 | 一本久久知道综合久久| 欧美在线视频一区| 亚洲第一精品久久忘忧草社区| 夜夜嗨av一区二区三区中文字幕 | 久久国内精品自在自线400部| 美国成人直播| 国产精品视频免费一区| 亚洲高清精品中出| 小辣椒精品导航| 亚洲大片精品永久免费| 亚洲欧美日韩天堂| 欧美激情亚洲另类| 韩日在线一区| 亚洲免费视频网站| 亚洲国产精品123| 午夜精品美女久久久久av福利| 欧美大片免费看| 国产一区二区看久久| 在线亚洲精品| 欧美/亚洲一区| 午夜激情亚洲| 欧美日韩综合一区| 亚洲青涩在线| 久久综合精品一区| 午夜精品福利视频| 欧美人交a欧美精品| 在线观看欧美日韩国产| 欧美在线free| 亚洲系列中文字幕| 欧美日韩国产一级片| 亚洲国产91色在线| 久久久91精品| 亚洲永久免费观看| 欧美深夜福利| 99精品视频免费| 亚洲电影自拍| 老司机精品视频一区二区三区| 国产日韩精品一区二区三区| 亚洲免费中文字幕| 日韩一级成人av| 欧美另类69精品久久久久9999| 在线日韩精品视频| 理论片一区二区在线| 欧美一区二区三区精品电影| 国产精品一区二区久久国产| 亚洲欧美激情诱惑| 亚洲天堂av综合网| 欧美性大战xxxxx久久久| 在线视频精品| 日韩一级二级三级| 欧美性理论片在线观看片免费| 在线视频精品一| 在线综合亚洲| 国产精品日本欧美一区二区三区| 亚洲欧美日韩精品久久亚洲区| 一本色道久久综合亚洲精品按摩 | 亚洲——在线| 亚洲综合电影| 国产亚洲aⅴaaaaaa毛片| 久久精品国产久精国产爱| 欧美影院成人| 亚洲高清视频在线| 亚洲欧洲日本专区| 国产精品xxxxx| 久久精品国产亚洲精品|