锘??xml version="1.0" encoding="utf-8" standalone="yes"?> "STREQUAL" "x86_64" Unknown arguments specified 鍦ㄥご鏂囦歡涓紝瀹氫箟瀹?br>#ifdef MYDLL_EXPORTS 鐜板湪錛屽湪鍙橀噺m_structs鍓嶏紝娣誨姞錛?br>template class MYDLL_API std::allocator<myStruct>; 浜?瀛楄妭瀵歸綈瀵圭▼搴忕殑褰卞搷: 緇撴瀯浣揂涓寘鍚簡4瀛楄妭闀垮害鐨刬nt涓涓紝1瀛楄妭闀垮害鐨刢har涓涓拰2瀛楄妭闀垮害鐨剆hort鍨嬫暟鎹竴涓?B涔熶竴鏍?鎸夌悊璇碅,B澶у皬搴旇閮芥槸7瀛楄妭銆?br>涔嬫墍浠ュ嚭鐜頒笂闈㈢殑緇撴灉鏄洜涓虹紪璇戝櫒瑕佸鏁版嵁鎴愬憳鍦ㄧ┖闂翠笂榪涜瀵歸綈銆備笂闈㈡槸鎸夌収緙栬瘧鍣ㄧ殑榛樿璁劇疆榪涜瀵歸綈鐨勭粨鏋?閭d箞鎴戜滑鏄笉鏄彲浠ユ敼鍙樼紪璇戝櫒鐨勮繖縐嶉粯璁ゅ榻愯緗憿,褰撶劧鍙互.渚嬪: 涓?緙栬瘧鍣ㄦ槸鎸夌収浠涔堟牱鐨勫師鍒欒繘琛屽榻愮殑? 鍥?濡備綍淇敼緙栬瘧鍣ㄧ殑榛樿瀵歸綈鍊? 浜?閽堝瀛楄妭瀵歸綈,鎴戜滑鍦ㄧ紪紼嬩腑濡備綍鑰冭檻? 濡傛灉鍦ㄧ紪紼嬬殑鏃跺欒鑰冭檻鑺傜害絀洪棿鐨勮瘽,閭d箞鎴戜滑鍙渶瑕佸亣瀹氱粨鏋勭殑棣栧湴鍧鏄?,鐒跺悗鍚勪釜鍙橀噺鎸夌収涓婇潰鐨勫師鍒欒繘琛屾帓鍒楀嵆鍙?鍩烘湰鐨勫師鍒欏氨鏄妸緇撴瀯涓殑鍙橀噺鎸夌収 綾誨瀷澶у皬浠庡皬鍒板ぇ澹版槑,灝介噺鍑忓皯涓棿鐨勫~琛ョ┖闂?榪樻湁涓縐嶅氨鏄負(fù)浜嗕互絀洪棿鎹㈠彇鏃墮棿鐨勬晥鐜?鎴戜滑鏄劇ず鐨勮繘琛屽~琛ョ┖闂磋繘琛屽榻?姣斿:鏈変竴縐嶄嬌鐢ㄧ┖闂存崲鏃墮棿鍋?娉曟槸鏄懼紡鐨勬彃鍏eserved鎴愬憳錛?br> struct A{ reserved鎴愬憳瀵規(guī)垜浠殑紼嬪簭娌℃湁浠涔堟剰涔?瀹冨彧鏄搗鍒板~琛ョ┖闂翠互杈懼埌瀛楄妭瀵歸綈鐨勭洰鐨?褰撶劧鍗充嬌涓嶅姞榪欎釜鎴愬憳閫氬父緙栬瘧鍣ㄤ篃浼?xì)缁欐垜浠嚜鍔ㄥ~琛ュ瘹w綈,鎴戜滑鑷繁鍔犱笂瀹冨彧鏄搗鍒版樉寮忕殑鎻愰啋浣滅敤. 鍏?瀛楄妭瀵歸綈鍙兘甯︽潵鐨勯殣鎮(zhèn)? p=&i; 涓?濡備綍鏌ユ壘涓庡瓧鑺傚榻愭柟闈㈢殑闂: 鍏?鐩稿叧鏂囩珷:杞嚜http://blog.csdn.net/goodluckyxl/archive/2005/10/17/506827.aspx ARM涓嬬殑瀵歸綈澶勭悊 3.13 type qulifiers 鏈夐儴鍒嗘憳鑷狝RM緙栬瘧鍣ㄦ枃妗e榻愰儴鍒?/p>
瀵歸綈鐨勪嬌鐢? p = (char*)&a; /* //榪欐牱鍙互寰堟竻妤氱殑鐪嬪埌闈炲榻愯闂槸濡備綍浜х敓閿欒鐨?br>//浠ュ強(qiáng)濡備綍娑堥櫎闈炲榻愯闂甫鏉ラ棶棰?br>//涔熷彲浠ョ湅鍒伴潪瀵歸綈璁塊棶鍜屽榻愯闂殑鎸囦護(hù)宸紓瀵艱嚧鏁堢巼闂 涓嬮潰瀵逛簬寰蔣瀵逛簬c++璇█鐨勪慨鏀瑰仛涓涓嬪垎鏋愶紒 Lambda琛ㄨ揪寮?/p>
寰堝緙栫▼緙栫▼璇█閮芥敮鎸佸尶鍚嶅嚱鏁?anonymous function)銆傛墍璋撳尶鍚嶅嚱鏁幫紝灝辨槸榪欎釜鍑芥暟鍙湁鍑芥暟浣擄紝鑰屾病鏈夊嚱鏁板悕銆侺ambda琛ㄨ揪寮忓氨鏄疄鐜板尶鍚嶅嚱鏁扮殑涓縐嶇紪紼嬫妧宸э紝瀹冧負(fù)緙栧啓鍖垮悕鍑芥暟鎻愪緵浜嗙畝鏄庣殑鍑芥暟寮忕殑鍙ユ硶銆傚悓鏍鋒槸Visual Studio涓殑寮鍙戣璦錛孷isual Basic鍜孷isual C#鏃╁氨瀹炵幇浜嗗Lambda琛ㄨ揪寮忕殑鏀寔錛岀粓浜嶸isual C++榪欐涔熶笉鐢樿惤鍚庯紝鍦╒isual Studio 2010涓坊鍔犱簡瀵筁ambda琛ㄨ揪寮忕殑鏀寔銆?br> Lambda琛ㄨ揪寮忎嬌寰楀嚱鏁板彲浠ュ湪浣跨敤鐨勫湴鏂瑰畾涔夛紝騫朵笖鍙互鍦↙ambda鍑芥暟涓嬌鐢↙ambda鍑芥暟涔嬪鐨勬暟鎹傝繖灝變負(fù)閽堝闆嗗悎鎿嶄綔甯︽潵浜嗗緢澶х殑渚垮埄銆傚湪浣滅敤涓婏紝Lambda琛ㄨ揪寮忕被浼間簬鍑芥暟鎸囬拡鍜屽嚱鏁板璞★紝Lambda琛ㄨ揪寮忓緢濂藉湴鍏奸【浜嗗嚱鏁版寚閽堝拰鍑芥暟瀵硅薄鐨勪紭鐐癸紝鍗存病鏈夊畠浠殑緙虹偣銆傜浉瀵逛簬鍑芥暟鎸囬拡鎴栨槸鍑芥暟瀵硅薄澶嶆潅鐨勮娉曞艦寮忥紝Lambda琛ㄨ揪寮忎嬌鐢ㄩ潪甯哥畝鍗曠殑璇硶灝卞彲浠ュ疄鐜板悓鏍風(fēng)殑鍔熻兘錛岄檷浣庝簡Lambda琛ㄨ揪寮忕殑瀛︿範(fàn)闅懼害錛岄伩鍏嶄簡浣跨敤澶嶆潅鐨勫嚱鏁板璞℃垨鏄嚱鏁版寚閽堟墍甯︽潵鐨勯敊璇傛垜浠彲浠ョ湅涓涓疄闄呯殑渚嬪瓙錛?/p>
view plaincopy to clipboardprint? using namespace std; int _tmain(int argc, _TCHAR* argv[]) return 0; 榪欐浠g爜寰幆閬嶅巻杈撳嚭vector涓殑姣忎竴涓暟錛屽茍鍒ゆ柇榪欎釜鏁版槸濂囨暟榪樻槸鍋舵暟銆傛垜浠彲浠ラ殢鏃朵慨鏀筁ambda琛ㄨ揪寮忚屾敼鍙樿繖涓尶鍚嶅嚱鏁扮殑瀹炵幇錛屼慨鏀瑰闆嗗悎鐨勬搷浣溿傚湪榪欐浠g爜涓紝C++浣跨敤涓瀵逛腑鎷彿“[]”鏉ヨ〃紺篖ambda琛ㄨ揪寮忕殑寮濮嬶紝鍏跺悗鐨?#8221;(int n)”琛ㄧずLambda琛ㄨ揪寮忕殑鍙傛暟銆傝繖浜涘弬鏁板皢鍦↙ambda琛ㄨ揪寮忎腑浣跨敤鍒般備負(fù)浜嗕綋浼?xì)Lambda琛ㄨ揪寮忕殑綆媧侊紝鎴戜滑鏉ョ湅鐪嬪悓鏍風(fēng)殑鍔熻兘錛屽浣曚嬌鐢ㄥ嚱鏁板璞″疄鐜幫細(xì) struct LambdaFunctor { } int _tmain(int argc, _TCHAR* argv[]) for (int i = 0; i < 10; ++i) { for_each(v.begin(), v.end(), LambdaFunctor()); return 0; 闈欐佹柇璦static_assert 鍦ㄤ箣鍓嶇殑C++鏍囧噯C++03涓紝鎴戜滑鍙互浣跨敤涓ょ鏂█錛?br> • 浣跨敤棰勫鐞嗕腑鐨勬潯浠剁紪璇戝拰#error鎸囦護(hù)錛屽彲浠ュ湪棰勫鐞嗛樁孌墊鏌ヤ竴浜涚紪璇戞潯浠?br> • 鍙互浣跨敤瀹廰ssert鏉ヨ繘琛岃繍琛屾椂媯鏌ワ紝浠ョ‘淇濈▼搴忛昏緫鐨勬紜?/p>
浣嗕嬌鐢?error鏂規(guī)硶鏄潪甯哥儲鐞愮殑錛屽茍涓斾笉鑳藉瀵規(guī)ā鏉垮弬鏁拌繘琛屾鏌ワ紝鍥犱負(fù)妯℃澘瀹炰緥鍖栨槸鍦ㄧ紪璇戞椂榪涜錛岃?error鏂規(guī)硶鏄湪棰勫鐞嗛樁孌佃繘琛岀殑銆傝宎ssert瀹忔槸鍦ㄨ繍琛屾椂榪涜媯鏌ャ備笉闅懼彂鐜幫紝鎴戜滑緙哄皯浜嗕竴鏍蜂笢瑗匡紝閭e氨鏄彲鐢ㄤ簬鍦ㄧ紪璇戞椂媯鏌ョ殑宸ュ叿銆備簬鏄紝闈欐佹柇璦搴旇繍鑰岀敓銆?/p>
鍦ㄦ柊鐨凜++鏍囧噯C++0x涓紝鍔犲叆浜嗗闈欐佹柇璦鐨勬敮鎸侊紝寮曞叆浜嗘柊鐨勫叧閿瓧static_assert鏉ヨ〃紺洪潤鎬佹柇璦銆備嬌鐢ㄩ潤鎬佹柇璦錛屾垜浠彲浠ュ湪紼嬪簭鐨勭紪璇戞椂鏈熸嫻嬩竴浜涙潯浠舵槸鍚︽垚绔嬶紝榪欎釜鐗規(guī)у湪璋冭瘯妯℃澘鍑芥暟鐨勬ā鏉垮弬鏁版椂鐗瑰埆鏈夌敤銆傚湪緙栬瘧鐨勬椂鍊欙紝妯℃澘鍑芥暟瀹炰緥鍖栵紝榪欐椂鎴戜滑灝卞彲浠ヤ嬌鐢ㄩ潤鎬佹柇璦鍘繪祴璇曟ā鏉垮嚱鏁扮殑鍙傛暟鏄惁鎸夌収鎴戜滑鐨勮璁℃嫢鏈夊悎閫傜殑鍊箋備緥濡備笅闈㈣繖孌典唬鐮侊細(xì) view plaincopy to clipboardprint? int main() { return 0; auto鍏抽敭瀛?/p>
鍦–++0x涓紝auto鍏抽敭瀛楃殑鎰忎箟鍙戠敓浜嗘敼鍙樸備粠Visual C++ 2010寮濮嬶紝auto鍏抽敭瀛楀皢鐢ㄤ簬鎸囧紩緙栬瘧鍣ㄦ牴鎹彉閲忕殑鍒濆鍊兼潵鍐沖畾鍙橀噺鐨勬暟鎹被鍨嬨傛崲鍙ヨ瘽璇達(dá)紝鎴戜滑鍙互鎶奱uto褰撴垚涓縐嶆柊鐨勬暟鎹被鍨嬶紝瀹冨彲浠?#8220;浠庡垵濮嬪寲鍣?initialize)涓帹瀵煎嚭鎵浠h〃鐨勫彉閲忕殑鐪熸綾誨瀷”銆傝繖縐嶅auto鍏抽敭瀛楃殑浣跨敤鏂瑰紡鍙互澶уぇ娑堥櫎褰撳墠鏇夸唬鏂瑰紡鎵瀵艱嚧鐨勫啑闀垮拰鏄撳嚭閿欑殑浠g爜銆傛垜浠湅涓涓疄闄呯殑渚嬪瓙錛?/p>
view plaincopy to clipboardprint? int main() { const regex r("(\\w+) (\\w+)"); for (string s; getline(cin, s); ) { return 0; 鍙﹀錛岃窡鍏朵粬鏁版嵁綾誨瀷涓鏍鳳紝鎴戜滑涔熷彲浠ュauto鍏抽敭瀛楄繘琛屼慨楗幫紝渚嬪娣誨姞const錛屾寚閽?*)錛屽乏鍊煎紩鐢?&)錛屽彸鍊煎紩鐢?&&)絳夌瓑錛岀紪璇戝櫒浼?xì)鏍规嵁a(chǎn)uto綾誨瀷鎵浠h〃鐨勭湡姝g殑鏁版嵁鏉ュ喅瀹氳繖浜涗慨楗扮殑鍏蜂綋鍚箟銆?/p>
涓轟簡鍏煎涓浜涙棫鏈夌殑C++浠g爜錛屾垜浠彲浠ヤ嬌鐢?Zc:auto榪欎釜緙栬瘧鍣ㄩ夐」錛屾潵鍛婅瘔緙栬瘧鍣ㄦ槸閲囩敤auto鍏抽敭瀛楃殑鍘熸湁瀹氫箟榪樻槸鍦ㄦ柊鏍囧噯C++0x涓殑瀹氫箟銆?/p>
鍙沖煎紩鐢?/p>
浣滀負(fù)鏈閲嶈鐨勪竴欏硅璦鐗規(guī)э紝鍙沖煎紩鐢?rvalue references)琚紩鍏ュ埌 C++0x涓傛垜浠彲浠ラ氳繃鎿嶄綔絎?#8220;&&”鏉ュ0鏄庝竴涓彸鍊煎紩鐢紝鍘熷厛鍦–++涓嬌鐢?#8220;&”鎿嶄綔絎﹀0鏄庣殑寮曠敤鐜板湪琚О涓哄乏鍊煎紩鐢ㄣ? int a; int b; int& a_lvref = int(); // error C2440: 'initializing' : cannot convert from 'int' to 'int &' 鍙沖兼槸鏃犲悕鐨勬暟鎹紝渚嬪鍑芥暟鐨勮繑鍥炲間竴鑸鏉ュ氨鏄彸鍊箋傚綋瀵瑰彸鍊艱繘琛屾搷浣滅殑鏃跺欙紝鍙沖兼湰韜線寰娌℃湁蹇呰淇濈暀錛屽洜姝ゅ湪鏌愪簺鎯呭喌涓嬪彲浠ョ洿鎺?#8220;縐誨姩”涔嬨傞氳繃鍙沖煎紩鐢紝紼嬪簭鍙互鏄庣‘鐨勫尯鍒嗗嚭浼犲叆鐨勫弬鏁版槸鍚︿負(fù)鍙沖鹼紝浠庤岄伩鍏嶄簡涓嶅繀瑕佺殑鎷瘋礉錛岀▼搴忕殑鏁堢巼涔熷氨寰楀埌浜嗘彁楂樸傛垜浠冭檻涓涓畝鍗曠殑鏁版嵁浜ゆ崲鐨勫皬紼嬪簭錛屼粠涓潵浣撲細(xì)鍙沖煎紩鐢ㄦ墍甯︽潵鐨勬晥鐜囨彁鍗囥傛垜浠彲浠ュ啓涓涓嚱鏁皊wap鏉ュ疄鐜頒袱涓彉閲忓肩殑浜ゆ崲錛?/p>
view plaincopy to clipboardprint? 閭d箞錛屽鏋滀嬌鐢ㄥ彸鍊煎紩鐢ㄥ浣曞疄鐜板憿錛?/p>
view plaincopy to clipboardprint? template <class T> template <class T> void swap(T& a, T& b) int _tmain(int argc, _TCHAR* argv[]) return 0; 姝f槸鎷瘋礉(Copy)鍜岀Щ鍔?Move)鐨勫樊鍒紝浣垮緱鍙沖煎紩鐢ㄦ垚涓篊++0x涓渶嬋鍔ㄤ漢蹇冪殑鏂扮壒鎬т箣涓銆備粠瀹炶返瑙掑害璁詫紝瀹冭兘澶熷畬緹庢槸瑙e喅C++涓暱涔呬互鏉ヤ負(fù)浜烘墍璇熺梾鐨勪復(fù)鏃跺璞$殑鏁堢巼闂銆備粠璇█鏈韓璁詫紝瀹冨仴鍏ㄤ簡C++涓殑寮曠敤綾誨瀷鍦ㄥ乏鍊煎彸鍊兼柟闈㈢殑緙洪櫡銆備粠搴撹璁¤呯殑瑙掑害璁詫紝瀹冪粰搴撹璁¤呭張甯︽潵浜嗕竴鎶婂埄鍣ㄣ傝屽浜庡箍澶х殑搴撲嬌鐢ㄨ呰岃█錛屼笉鍔ㄤ竴鍏典竴鍗掍究鑳藉鑾峰緱“鍏嶈垂鐨?#8221;鏁堢巼鎻愬崌銆?/p>
鍦╒isual Studio 2010涓紝鍥犱負(fù)鏈変簡瀵硅繖浜汣++0x鏂扮壒鎬х殑鏀寔錛岄噸鏂扮偣鐕冧簡紼嬪簭鍛樹滑瀵笴++鐨勭儹鎯呫侰++閲嶆尟闆勯錛屾寚鏃ュ彲寰咃紒
]]>
float x = 100.1234;
]]>
瑙e喅錛?br />
if($ENV(COMPILING_TYPE) STREQUAL "x86_64")
鏀逛負(fù)錛?/span>
if(($ENV(COMPILING_TYPE)) STREQUAL "x86_64")
O浜嗭紒
]]>
sudo yum install centos-release-scl
2銆佸畨瑁卍evtoolset
sudo yum install devtoolset-9-gcc*
錛堝鏋滄兂瀹夎7.*鐗堟湰鐨勶紝灝辨敼鎴恉evtoolset-7-gcc*錛?
3銆佹縺媧誨搴旂殑devtoolset錛屾墍浠ヤ綘鍙互涓嬈″畨瑁呭涓増鏈殑devtoolset錛?br />闇瑕佺殑鏃跺欑敤涓嬮潰浠ヤ笅鍛戒護(hù)鍒囨崲鍒板搴旂殑鐗堟湰
scl enable devtoolset-9 bash
4銆佹煡鐪媑cc鐗堟湰
gcc -v
鏄劇ず涓?.x
O浜嗭紒
]]>
]]>
]]>
{
...
private:
std::vector<MY_STRUCT> m_structs;
}
浣嗘槸緙栬瘧鏃訛紝vs2005緇欏嚭浜唚arning C4251: 'CLASS_TEST::m_structs' : class 'std::vector<_Ty>' needs to have dll-interface to be used by clients of class ‘CLASS_TEST’鐨勮鍛婁俊鎭傝垂浜嗗緢澶х殑鍔叉墠瑙e喅鎺夛紝璁板綍涓嬫潵銆?/p>
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif
template class MYDLL_API std::vector<myStruct, std::allocator<myStruct> >;
榪欐牱錛屽嵆鍙互浜嗐?/p>
]]>
涓.浠涔堟槸瀛楄妭瀵歸綈,涓轟粈涔堣瀵歸綈?
鐜頒唬璁$畻鏈轟腑鍐呭瓨絀洪棿閮芥槸鎸夌収byte鍒掑垎鐨勶紝浠庣悊璁轟笂璁蹭技涔庡浠諱綍綾誨瀷鐨勫彉閲忕殑璁塊棶鍙互浠庝換浣曞湴鍧寮濮嬶紝浣嗗疄闄呮儏鍐墊槸鍦ㄨ闂壒瀹氱被鍨嬪彉閲忕殑鏃跺欑粡甯稿湪鐗?瀹氱殑鍐呭瓨鍦板潃璁塊棶錛岃繖灝遍渶瑕佸悇縐嶇被鍨嬫暟鎹寜鐓т竴瀹氱殑瑙勫垯鍦ㄧ┖闂翠笂鎺掑垪錛岃屼笉鏄『搴忕殑涓涓帴涓涓殑鎺掓斁錛岃繖灝辨槸瀵歸綈銆?br> 瀵歸綈鐨勪綔鐢ㄥ拰鍘熷洜錛氬悇涓‖浠跺鉤鍙板瀛樺偍絀洪棿鐨勫鐞嗕笂鏈夊緢澶х殑涓嶅悓銆備竴浜涘鉤鍙板鏌愪簺鐗瑰畾綾誨瀷鐨勬暟鎹彧鑳戒粠鏌愪簺鐗瑰畾鍦板潃寮濮嬪瓨鍙栥傛瘮濡傛湁浜涙灦鏋勭殑CPU鍦ㄨ闂?涓涓病鏈夎繘琛屽榻愮殑鍙橀噺鐨勬椂鍊欎細(xì)鍙戠敓閿欒,閭d箞鍦ㄨ繖縐嶆灦鏋勪笅緙栫▼蹇呴』淇濊瘉瀛楄妭瀵歸綈.鍏朵粬騫沖彴鍙兘娌℃湁榪欑鎯呭喌錛屼絾鏄渶甯歌鐨勬槸濡傛灉涓嶆寜鐓ч傚悎鍏跺鉤鍙拌姹傚 鏁版嵁瀛樻斁榪涜瀵歸綈錛屼細(xì)鍦ㄥ瓨鍙栨晥鐜囦笂甯︽潵鎹熷け銆傛瘮濡傛湁浜涘鉤鍙版瘡嬈¤閮芥槸浠庡伓鍦板潃寮濮嬶紝濡傛灉涓涓猧nt鍨嬶紙鍋囪涓?2浣嶇郴緇燂級濡傛灉瀛樻斁鍦ㄥ伓鍦板潃寮濮嬬殑鍦版柟錛岄偅 涔堜竴涓鍛ㄦ湡灝卞彲浠ヨ鍑鴻繖32bit錛岃屽鏋滃瓨鏀懼湪濂囧湴鍧寮濮嬬殑鍦版柟錛屽氨闇瑕?涓鍛ㄦ湡錛屽茍瀵逛袱嬈¤鍑虹殑緇撴灉鐨勯珮浣庡瓧鑺傝繘琛屾嫾鍑戞墠鑳藉緱鍒拌32bit鏁?鎹傛樉鐒跺湪璇誨彇鏁堢巼涓婁笅闄嶅緢澶氥?/p>
鍏堣鎴戜滑鐪嬪嚑涓緥瀛愬惂(32bit,x86鐜,gcc緙栬瘧鍣?:
璁劇粨鏋勪綋濡備笅瀹氫箟錛?br>struct A
{
int a;
char b;
short c;
};
struct B
{
char b;
int a;
short c;
};
鐜板湪宸茬煡32浣嶆満鍣ㄤ笂鍚勭鏁版嵁綾誨瀷鐨勯暱搴﹀涓?
char:1(鏈夌鍙鋒棤絎﹀彿鍚?
short:2(鏈夌鍙鋒棤絎﹀彿鍚?
int:4(鏈夌鍙鋒棤絎﹀彿鍚?
long:4(鏈夌鍙鋒棤絎﹀彿鍚?
float:4 double:8
閭d箞涓婇潰涓や釜緇撴瀯澶у皬濡備綍鍛?
緇撴灉鏄?
sizeof(strcut A)鍊間負(fù)8
sizeof(struct B)鐨勫煎嵈鏄?2
#pragma pack (2) /*鎸囧畾鎸?瀛楄妭瀵歸綈*/
struct C
{
char b;
int a;
short c;
};
#pragma pack () /*鍙栨秷鎸囧畾瀵歸綈錛屾仮澶嶇己鐪佸榻?/
sizeof(struct C)鍊兼槸8銆?br>淇敼瀵歸綈鍊間負(fù)1錛?br>#pragma pack (1) /*鎸囧畾鎸?瀛楄妭瀵歸綈*/
struct D
{
char b;
int a;
short c;
};
#pragma pack () /*鍙栨秷鎸囧畾瀵歸綈錛屾仮澶嶇己鐪佸榻?/
sizeof(struct D)鍊間負(fù)7銆?br>鍚庨潰鎴戜滑鍐嶈瑙?pragma pack()鐨勪綔鐢?
鍏堣鎴戜滑鐪嬪洓涓噸瑕佺殑鍩烘湰姒傚康錛?br>1.鏁版嵁綾誨瀷鑷韓鐨勫榻愬鹼細(xì)
瀵逛簬char鍨嬫暟鎹紝鍏惰嚜韜榻愬間負(fù)1錛屽浜巗hort鍨嬩負(fù)2錛屽浜巌nt,float,double綾誨瀷錛屽叾鑷韓瀵歸綈鍊間負(fù)4錛屽崟浣嶅瓧鑺傘?br>2.緇撴瀯浣撴垨鑰呯被鐨勮嚜韜榻愬鹼細(xì)鍏舵垚鍛樹腑鑷韓瀵歸綈鍊兼渶澶х殑閭d釜鍊箋?br>3.鎸囧畾瀵歸綈鍊鹼細(xì)#pragma pack (value)鏃剁殑鎸囧畾瀵歸綈鍊紇alue銆?br>4.鏁版嵁鎴愬憳銆佺粨鏋勪綋鍜岀被鐨勬湁鏁堝榻愬鹼細(xì)鑷韓瀵歸綈鍊煎拰鎸囧畾瀵歸綈鍊間腑灝忕殑閭d釜鍊箋?br>鏈?浜嗚繖浜涘鹼紝鎴戜滑灝卞彲浠ュ緢鏂逛究鐨勬潵璁ㄨ鍏蜂綋鏁版嵁緇撴瀯鐨勬垚鍛樺拰鍏惰嚜韜殑瀵歸綈鏂瑰紡銆傛湁鏁堝榻愬糔鏄渶緇堢敤鏉ュ喅瀹氭暟鎹瓨鏀懼湴鍧鏂瑰紡鐨勫鹼紝鏈閲嶈銆傛湁鏁堝榻怤錛屽氨鏄?琛ㄧず“瀵歸綈鍦∟涓?#8221;錛屼篃灝辨槸璇磋鏁版嵁鐨?瀛樻斁璧峰鍦板潃%N=0".鑰屾暟鎹粨鏋勪腑鐨勬暟鎹彉閲忛兘鏄寜瀹氫箟鐨勫厛鍚庨『搴忔潵鎺掓斁鐨勩傜涓涓暟鎹彉閲忕殑璧峰鍦板潃灝辨槸鏁?鎹粨鏋勭殑璧峰鍦板潃銆傜粨鏋勪綋鐨勬垚鍛樺彉閲忚瀵歸綈鎺掓斁錛岀粨鏋勪綋鏈韓涔熻鏍規(guī)嵁鑷韓鐨勬湁鏁堝榻愬煎渾鏁?灝辨槸緇撴瀯浣撴垚鍛樺彉閲忓崰鐢ㄦ婚暱搴﹂渶瑕佹槸瀵圭粨鏋勪綋鏈夋晥瀵歸綈鍊肩殑鏁存暟 鍊嶏紝緇撳悎涓嬮潰渚嬪瓙鐞嗚В)銆傝繖鏍峰氨涓嶈兘鐞嗚В涓婇潰鐨勫嚑涓緥瀛愮殑鍊間簡銆?br>渚嬪瓙鍒嗘瀽錛?br>鍒嗘瀽渚嬪瓙B錛?br>struct B
{
char b;
int a;
short c;
};
鍋?璁綛浠庡湴鍧絀洪棿0x0000寮濮嬫帓鏀俱傝渚嬪瓙涓病鏈夊畾涔夋寚瀹氬榻愬鹼紝鍦ㄧ瑪鑰呯幆澧冧笅錛岃鍊奸粯璁や負(fù)4銆傜涓涓垚鍛樺彉閲廱鐨勮嚜韜榻愬兼槸1錛屾瘮鎸囧畾鎴栬呴粯璁ゆ寚瀹?瀵歸綈鍊?灝忥紝鎵浠ュ叾鏈夋晥瀵歸綈鍊間負(fù)1錛屾墍浠ュ叾瀛樻斁鍦板潃0x0000絎﹀悎0x0000%1=0.絎簩涓垚鍛樺彉閲廰錛屽叾鑷韓瀵歸綈鍊間負(fù)4錛屾墍浠ユ湁鏁堝榻愬間篃涓?錛?鎵浠ュ彧鑳藉瓨鏀懼湪璧峰鍦板潃涓?x0004鍒?x0007榪欏洓涓繛緇殑瀛楄妭絀洪棿涓紝澶嶆牳0x0004%4=0,涓旂揣闈犵涓涓彉閲忋傜涓変釜鍙橀噺c,鑷韓瀵歸綈鍊間負(fù) 2錛屾墍浠ユ湁鏁堝榻愬間篃鏄?錛屽彲浠ュ瓨鏀懼湪0x0008鍒?x0009榪欎袱涓瓧鑺傜┖闂翠腑錛岀鍚?x0008%2=0銆傛墍浠ヤ粠0x0000鍒?x0009瀛樻斁鐨?閮芥槸B鍐呭銆傚啀鐪嬫暟鎹粨鏋凚鐨勮嚜韜榻愬間負(fù)鍏跺彉閲忎腑鏈澶у榻愬?榪欓噷鏄痓錛夋墍浠ュ氨鏄?錛屾墍浠ョ粨鏋勪綋鐨勬湁鏁堝榻愬間篃鏄?銆傛牴鎹粨鏋勪綋鍦嗘暣鐨勮姹傦紝 0x0009鍒?x0000=10瀛楄妭錛岋紙10錛?錛夛紖4錛?銆傛墍浠?x0000A鍒?x000B涔熶負(fù)緇撴瀯浣揃鎵鍗犵敤銆傛晠B浠?x0000鍒?x000B 鍏辨湁12涓瓧鑺?sizeof(struct B)=12;鍏跺疄濡傛灉灝辮繖涓涓氨鏉ヨ瀹冨凡灝嗘弧瓚沖瓧鑺傚榻愪簡, 鍥犱負(fù)瀹冪殑璧峰鍦板潃鏄?,鍥犳鑲畾鏄榻愮殑,涔嬫墍浠ュ湪鍚庨潰琛ュ厖2涓瓧鑺?鏄洜涓虹紪璇戝櫒涓轟簡瀹炵幇緇撴瀯鏁扮粍鐨勫瓨鍙栨晥鐜?璇曟兂濡傛灉鎴戜滑瀹氫箟浜嗕竴涓粨鏋凚鐨勬暟緇?閭?涔堢涓涓粨鏋勮搗濮嬪湴鍧鏄?娌℃湁闂,浣嗘槸絎簩涓粨鏋勫憿?鎸夌収鏁扮粍鐨勫畾涔?鏁扮粍涓墍鏈夊厓绱犻兘鏄揣鎸ㄧ潃鐨?濡傛灉鎴戜滑涓嶆妸緇撴瀯鐨勫ぇ灝忚ˉ鍏呬負(fù)4鐨勬暣鏁板?閭d箞涓嬩竴 涓粨鏋勭殑璧峰鍦板潃灝嗘槸0x0000A,榪欐樉鐒朵笉鑳芥弧瓚崇粨鏋勭殑鍦板潃瀵歸綈浜?鍥犳鎴戜滑瑕佹妸緇撴瀯琛ュ厖鎴愭湁鏁堝榻愬ぇ灝忕殑鏁存暟鍊?鍏跺疄璇稿:瀵逛簬char鍨嬫暟鎹紝鍏?鑷韓瀵歸綈鍊間負(fù)1錛屽浜巗hort鍨嬩負(fù)2錛屽浜巌nt,float,double綾誨瀷錛屽叾鑷韓瀵歸綈鍊間負(fù)4錛岃繖浜涘凡鏈夌被鍨嬬殑鑷韓瀵歸綈鍊間篃鏄熀浜庢暟緇勮冭檻鐨?鍙?鏄洜涓鴻繖浜涚被鍨嬬殑闀垮害宸茬煡浜?鎵浠ヤ粬浠殑鑷韓瀵歸綈鍊間篃灝卞凡鐭ヤ簡.
鍚岀悊,鍒嗘瀽涓婇潰渚嬪瓙C錛?br>#pragma pack (2) /*鎸囧畾鎸?瀛楄妭瀵歸綈*/
struct C
{
char b;
int a;
short c;
};
#pragma pack () /*鍙栨秷鎸囧畾瀵歸綈錛屾仮澶嶇己鐪佸榻?/
絎?涓涓彉閲廱鐨勮嚜韜榻愬間負(fù)1錛屾寚瀹氬榻愬間負(fù)2錛屾墍浠ワ紝鍏舵湁鏁堝榻愬間負(fù)1錛屽亣璁綜浠?x0000寮濮嬶紝閭d箞b瀛樻斁鍦?x0000錛岀鍚?x0000%1= 0;絎簩涓彉閲忥紝鑷韓瀵歸綈鍊間負(fù)4錛屾寚瀹氬榻愬間負(fù)2錛屾墍浠ユ湁鏁堝榻愬間負(fù)2錛屾墍浠ラ『搴忓瓨鏀懼湪0x0002銆?x0003銆?x0004銆?x0005鍥涗釜榪炵畫 瀛楄妭涓紝絎﹀悎0x0002%2=0銆傜涓変釜鍙橀噺c鐨勮嚜韜榻愬間負(fù)2錛屾墍浠ユ湁鏁堝榻愬間負(fù)2錛岄『搴忓瓨鏀?br>鍦?x0006銆?x0007涓紝絎﹀悎 0x0006%2=0銆傛墍浠ヤ粠0x0000鍒?x00007鍏卞叓瀛楄妭瀛樻斁鐨勬槸C鐨勫彉閲忋傚張C鐨勮嚜韜榻愬間負(fù)4錛屾墍浠鐨勬湁鏁堝榻愬間負(fù)2銆傚張8%2=0,C 鍙崰鐢?x0000鍒?x0007鐨勫叓涓瓧鑺傘傛墍浠izeof(struct C)=8.
1.鍦╒C IDE涓紝鍙互榪欐牱淇敼錛歔Project]|[Settings],c/c++閫夐」鍗ategory鐨凜ode Generation閫夐」鐨凷truct Member Alignment涓慨鏀癸紝榛樿鏄?瀛楄妭銆?br>2.鍦ㄧ紪鐮佹椂錛屽彲浠ヨ繖鏍峰姩鎬佷慨鏀癸細(xì)#pragma pack .娉ㄦ剰:鏄痯ragma鑰屼笉鏄痯rogma.
char a;
char reserved[3];//浣跨敤絀洪棿鎹㈡椂闂?br> int b;
}
浠g爜涓叧浜庡榻愮殑闅愭?zhèn)eQ屽緢澶氭槸闅愬紡鐨勩傛瘮濡傚湪寮哄埗綾誨瀷杞崲鐨勬椂鍊欍備緥濡傦細(xì)
unsigned int i = 0x12345678;
unsigned char *p=NULL;
unsigned short *p1=NULL;
*p=0x00;
p1=(unsigned short *)(p+1);
*p1=0x0000;
鏈鍚庝袱鍙ヤ唬鐮侊紝浠庡鏁拌竟鐣屽幓璁塊棶unsignedshort鍨嬪彉閲忥紝鏄劇劧涓嶇鍚堝榻愮殑瑙勫畾銆?br>鍦▁86涓婏紝綾諱技鐨勬搷浣滃彧浼?xì)濯勫搷鏁堢巼锛屼絾鏄湪MIPS鎴栬卻parc涓婏紝鍙兘灝辨槸涓涓猠rror,鍥犱負(fù)瀹冧滑瑕佹眰蹇呴』瀛楄妭瀵歸綈.
濡傛灉鍑虹幇瀵歸綈鎴栬呰祴鍊奸棶棰橀鍏堟煡鐪?br>1. 緙栬瘧鍣ㄧ殑big little绔緗?br>2. 鐪嬭繖縐嶄綋緋繪湰韜槸鍚︽敮鎸侀潪瀵歸綈璁塊棶
3. 濡傛灉鏀寔鐪嬭緗簡瀵歸綈涓庡惁,濡傛灉娌℃湁鍒欑湅璁塊棶鏃墮渶瑕佸姞鏌愪簺鐗規(guī)畩鐨勪慨楗版潵鏍囧織鍏剁壒孌婅闂搷浣溿?/p>
from DUI0067D_ADS1_2_CompLib
1.__align(num)
榪欎釜鐢ㄤ簬淇敼鏈楂樼駭鍒璞$殑瀛楄妭杈圭晫銆傚湪姹囩紪涓嬌鐢↙DRD鎴栬匰TRD鏃?br> 灝辮鐢ㄥ埌姝ゅ懡浠_align(8)榪涜淇グ闄愬埗銆傛潵淇濊瘉鏁版嵁瀵硅薄鏄浉搴斿榻愩?br> 榪欎釜淇グ瀵硅薄鐨勫懡浠ゆ渶澶ф槸8涓瓧鑺傞檺鍒?鍙互璁?瀛楄妭鐨勫璞¤繘琛?瀛楄妭
瀵歸綈,浣嗘槸涓嶈兘璁?瀛楄妭鐨勫璞?瀛楄妭瀵歸綈銆?br> __align鏄瓨鍌ㄧ被淇敼,浠栧彧淇グ鏈楂樼駭綾誨瀷瀵硅薄涓嶈兘鐢ㄤ簬緇撴瀯鎴栬呭嚱鏁板璞°?br>
2.__packed
__packed鏄繘琛屼竴瀛楄妭瀵歸綈
1.涓嶈兘瀵筽acked鐨勫璞¤繘琛屽榻?br> 2.鎵鏈夊璞$殑璇誨啓璁塊棶閮借繘琛岄潪瀵歸綈璁塊棶
3.float鍙?qiáng)鍖呭惈float鐨勭粨鏋勮仈鍚堝強(qiáng)鏈敤__packed鐨勫璞″皢涓嶈兘瀛楄妭瀵歸綈
4.__packed瀵瑰眬閮ㄦ暣褰㈠彉閲忔棤褰卞搷
5.寮哄埗鐢眜npacked瀵硅薄鍚憄acked瀵硅薄杞寲鏄湭瀹氫箟,鏁村艦鎸囬拡鍙互鍚堟硶瀹?br> 涔変負(fù)packed銆?br> __packed int* p; //__packed int 鍒欐病鏈夋剰涔?br> 6.瀵歸綈鎴栭潪瀵歸綈璇誨啓璁塊棶甯︽潵闂
__packed struct STRUCT_TEST
{
char a;
int b;
char c;
} ; //瀹氫箟濡備笅緇撴瀯姝ゆ椂b鐨勮搗濮嬪湴鍧涓瀹氭槸涓嶅榻愮殑
//鍦ㄦ爤涓闂産鍙兘鏈夐棶棰?鍥犱負(fù)鏍堜笂鏁版嵁鑲畾鏄榻愯闂甗from CL]
//灝嗕笅闈㈠彉閲忓畾涔夋垚鍏ㄥ眬闈欐佷笉鍦ㄦ爤涓?
static char* p;
static struct STRUCT_TEST a;
void Main()
{
__packed int* q; //姝ゆ椂瀹氫箟鎴恄_packed鏉ヤ慨楗板綋鍓峲鎸囧悜涓洪潪瀵歸綈鐨勬暟鎹湴鍧涓嬮潰鐨勮闂垯鍙互
q = (int*)(p+1);
*q = 0x87654321;
/*
寰楀埌璧嬪肩殑姹囩紪鎸囦護(hù)寰堟竻妤?br>ldr r5,0x20001590 ; = #0x12345678
[0xe1a00005] mov r0,r5
[0xeb0000b0] bl __rt_uwrite4 //鍦ㄦ澶勮皟鐢ㄤ竴涓啓4byte鐨勬搷浣滃嚱鏁?
[0xe5c10000] strb r0,[r1,#0] //鍑芥暟榪涜4嬈trb鎿嶄綔鐒跺悗榪斿洖淇濊瘉浜嗘暟鎹紜殑璁塊棶
[0xe1a02420] mov r2,r0,lsr #8
[0xe5c12001] strb r2,[r1,#1]
[0xe1a02820] mov r2,r0,lsr #16
[0xe5c12002] strb r2,[r1,#2]
[0xe1a02c20] mov r2,r0,lsr #24
[0xe5c12003] strb r2,[r1,#3]
[0xe1a0f00e] mov pc,r14
*/
濡傛灉q娌℃湁鍔燺_packed淇グ鍒欐眹緙栧嚭鏉ユ寚浠ゆ槸榪欐牱鐩存帴浼?xì)瀵艰嚧濂囧湴鍧澶勮闂け璐?br>[0xe59f2018] ldr r2,0x20001594 ; = #0x87654321
[0xe5812000] str r2,[r1,#0]
*/
}
鏈枃鏉ヨ嚜CSDN鍗氬錛岃漿杞借鏍囨槑鍑哄錛?a >http://blog.csdn.net/jszj/archive/2009/02/20/3915328.aspx
]]>
寰蔣鍗沖皢鍦?010騫?鏈?2鏃ュ彂甯僔S2010鐨勬寮忕増錛屽浜巆++璇█鍋氫簡淇敼錛屼嬌涔嬫洿鍔犵鍚坈++鏍囧噯銆?/p>
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
for_each(v.begin(), v.end(), [] (int n) {
cout << n;
if (n % 2 == 0) {
cout << " even ";
} else {
cout << " odd ";
}
});
cout << endl;
return 0;
}
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
{
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
for_each(v.begin(), v.end(), [] (int n) {
cout << n;
if (n % 2 == 0) {
cout << " even ";
} else {
cout << " odd ";
}
});
cout << endl;
}
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;
struct LambdaFunctor {
void operator()(int n) const {
cout << n << " ";
if (n % 2 == 0) {
cout << " even ";
} else {
cout << " odd ";
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> v;
for (int i = 0; i < 10; ++i) {
v.push_back(i);
}
for_each(v.begin(), v.end(), LambdaFunctor());
cout << endl;
return 0;
}
#include "stdafx.h"
#include <algorithm>
#include <iostream>
#include <ostream>
#include <vector>
using namespace std;
void operator()(int n) const {
cout << n << " ";
if (n % 2 == 0) {
cout << " even ";
} else {
cout << " odd ";
}
};
{
vector<int> v;
v.push_back(i);
}
cout << endl;
}
閫氳繃姣旇緝鎴戜滑灝卞彲浠ュ彂鐜幫紝Lambda琛ㄨ揪寮忕殑璇硶鏇村姞綆媧侊紝浣跨敤璧鋒潵鏇村姞綆鍗曢珮鏁?/p>
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
template <int N> struct Kitten {
static_assert(N < 2, "Kitten<N> requires N < 2.");
};
int main() {
Kitten<1> peppermint;
Kitten<3> jazz;
return 0;
}
template <int N> struct Kitten {
static_assert(N < 2, "Kitten<N> requires N < 2.");
};
Kitten<1> peppermint;
Kitten<3> jazz;
}
褰撴垜浠湪涓誨嚱鏁頒腑浣跨敤“1”鍘誨疄渚嬪寲Kitten榪欎釜緇撴瀯浣撴椂錛屽湪緙栬瘧鐨勬椂鍊欙紝闈欐佹柇璦static_assert浼?xì)娴嬭瘯鍙傛暟N鐨勫鹼紝褰揘鐨勫煎皬浜?鏃跺氨浼?xì)漶旂敓涓涓柇璦閿欒錛屽茍灝嗙浉搴旂殑璋冭瘯甯姪淇℃伅杈撳嚭鍒?#8220;Error List”紿楀彛涓紝榪欐牱紼嬪簭鍛樺氨鍙互瀵歸棶棰樺揩閫熷畾浣嶏紝瑙e喅闂灝辨洿鍔犳柟渚夸簡銆?br>
鍥? static_assert鏂█鍙?qiáng)鍏惰緭鍑?/p>
鍙﹀錛岄潤鎬佹柇璦榪樺甫鏉ュ緢澶氬叾浠栫殑浼樺娍銆備緥濡傞潤鎬佹柇璦鍦ㄧ紪璇戞椂榪涜澶勭悊錛屼笉浼?xì)漶旂敓鋼Q浣曡繍琛屾椂鍒葷┖闂村拰鏃墮棿涓婄殑寮閿錛岃繖灝變嬌寰楀畠姣攁ssert瀹忓叿鏈夋洿濂界殑鏁堢巼銆傚彟澶栨瘮杈冮噸瑕佺殑涓涓壒鎬ф槸濡傛灉鏂█澶辮觸錛屽畠浼?xì)漶旂敓鏈夋剰涔変笖鍏呭垎鐨勮瘖鏂俊鎭Q屽府鍔╃▼搴忓憳蹇熻В鍐抽棶棰樸?/p>
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
#include <iostream>
#include <map>
#include <ostream>
#include <regex>
#include <string>
using namespace std;
using namespace std::tr1;
int main() {
map<string, string> m;
const regex r("(\\w+) (\\w+)");
for (string s; getline(cin, s); ) {
smatch results;
if (regex_match(s, results, r)) {
m[results[1]] = results[2];
}
}
for (auto i = m.begin(); i != m.end(); ++i) {
cout << i->second << " are " << i->first << endl;
}
return 0;
}
#include <iostream>
#include <map>
#include <ostream>
#include <regex>
#include <string>
using namespace std;
using namespace std::tr1;
map<string, string> m;
smatch results;
if (regex_match(s, results, r)) {
m[results[1]] = results[2];
}
}
for (auto i = m.begin(); i != m.end(); ++i) {
cout << i->second << " are " << i->first << endl;
}
}
鍦ㄨ繖孌典唬鐮佷腑錛屾垜浠嬌鐢╝uto鍏抽敭瀛楁潵浠f浛浜嗙湡姝g殑鏁版嵁綾誨瀷map<string, string>::iterator錛岃繖浣垮緱鏁翠釜浠g爜鑷劧鑰岀畝媧併?/p>
int& a_lvref = a; // 宸﹀煎紩鐢?/p>
int&& b_rvref = b; // 鍙沖煎簲鐢?br> 宸﹀煎紩鐢ㄥ拰鍙沖煎紩鐢ㄧ殑琛ㄧ幇琛屼負(fù)鍩烘湰涓鑷達(dá)紝瀹冧滑鍞竴鐨勫樊鍒氨鏄彸鍊煎紩鐢ㄥ彲浠ョ粦瀹氬埌涓涓復(fù)鏃跺璞?鍙沖?涓婏紝鑰屽乏鍊煎紩鐢ㄤ笉鍙互銆備緥濡傦細(xì)
int&& b_rvref = int(); // OK!
鍦ㄧ涓琛屼唬鐮佷腑錛屾垜浠皢涓涓復(fù)鏃跺璞nt()緇戝畾鍒頒竴涓乏鍊煎紩鐢紝灝嗕駭鐢熶竴涓紪璇戦敊璇傝屽湪絎簩琛屼腑錛屾垜浠皢涓存椂瀵硅薄緇戝畾鍒板彸鍊煎紩鐢紝灝卞彲浠ラ『鍒╅氳繃緙栬瘧銆?/p>
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
template <class T> swap(T& a, T& b)
{
T tmp(a); // tmp瀵硅薄鍒涘緩鍚庯紝鎴戜滑灝辨嫢鏈変簡a鐨勪袱浠芥嫹璐?nbsp;
a = b; // 鐜板湪鎴戜滑鎷ユ湁b鐨勪袱浠芥嫹璐?nbsp;
b = tmp; // 鐜板湪鎴戜滑鎷ユ湁a鐨勪袱浠芥嫹璐?nbsp;
}
template <class T> swap(T& a, T& b)
{
T tmp(a); // tmp瀵硅薄鍒涘緩鍚庯紝鎴戜滑灝辨嫢鏈変簡a鐨勪袱浠芥嫹璐?br> a = b; // 鐜板湪鎴戜滑鎷ユ湁b鐨勪袱浠芥嫹璐?br> b = tmp; // 鐜板湪鎴戜滑鎷ユ湁a鐨勪袱浠芥嫹璐?br>}
鍦ㄨ繖孌典唬鐮佷腑錛岃櫧鐒舵垜浠彧鏄負(fù)浜嗚繘琛岀畝鍗曠殑鏁版嵁浜ゆ崲錛屼絾鏄嵈鎵ц浜嗗嬈″璞℃嫹璐濄傝繖浜涘璞$殑鎷瘋礉鎿嶄綔錛岀壒鍒槸褰撹繖浜涘璞℃瘮杈冨ぇ鐨勬椂鍊欙紝鏃犵枒浼?xì)濯勫搷绋嬪簭鐨勬晥鐜囥?/p>
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
#include "stdafx.h"
template <class T>
T&& move(T&& a)
{
return a;
}
template <class T> void swap(T& a, T& b)
{
T tmp(move(a)); // 瀵硅薄a琚Щ鍔ㄥ埌瀵硅薄tmp錛宎琚竻絀?nbsp;
a = move(b); // 瀵硅薄b琚Щ鍔ㄥ埌瀵硅薄a錛宐琚竻絀?nbsp;
b = move(tmp); // 瀵硅薄tmp琚Щ鍔ㄥ埌瀵硅薄b
}
int _tmain(int argc, _TCHAR* argv[])
{
int a = 1;
int b = 2;
swap(a, b);
return 0;
}
#include "stdafx.h"
T&& move(T&& a)
{
return a;
}
{
T tmp(move(a)); // 瀵硅薄a琚Щ鍔ㄥ埌瀵硅薄tmp錛宎琚竻絀?br> a = move(b); // 瀵硅薄b琚Щ鍔ㄥ埌瀵硅薄a錛宐琚竻絀?br> b = move(tmp); // 瀵硅薄tmp琚Щ鍔ㄥ埌瀵硅薄b
}
{
int a = 1;
int b = 2;
swap(a, b);
}
鍦ㄨ繖孌甸噸鏂板疄鐜扮殑浠g爜涓紝鎴戜滑浣跨敤浜嗕竴涓猰ove()鍑芥暟鏉ヤ唬鏇垮璞$殑璧嬪兼搷浣滅“=”,move()鍙槸綆鍗曞湴鎺ュ彈涓涓彸鍊煎紩鐢ㄦ垨鑰呭乏鍊煎紩鐢ㄤ綔涓哄弬鏁幫紝鐒跺悗鐩存帴榪斿洖鐩稿簲瀵硅薄鐨勫彸鍊煎紩鐢ㄣ傝繖涓榪囩▼涓嶄細(xì)浜х敓鎷瘋礉(Copy)鎿嶄綔錛岃屽彧浼?xì)灏嗘簮瀵硅薄绉诲?Move)鍒扮洰鏍囧璞°?/p>
鏈枃鏉ヨ嚜CSDN鍗氬錛岃漿杞借鏍囨槑鍑哄錛?a >http://blog.csdn.net/yincheng01/archive/2010/03/11/5367032.aspx
]]>
#include<stdio.h>
long test(int a,int b)
{
a = a + 1;
b = b + 100;
return a + b;
}
鍐欐垚32浣嶆眹緙栧氨鏄繖鏍?br />;//////////////////////////////////////////////////////////////////////////////////////////////////////
.386
.module flat,stdcall ;榪欓噷鎴戜滑鐢╯tdcall 灝辨槸鍑芥暟鍙傛暟 鍘嬫爤鐨勬椂鍊欎粠鏈鍚庝竴涓紑濮嬪帇錛屽拰琚皟鐢ㄥ嚱鏁拌礋璐f竻鏍?br />option casemap:none ;鍖哄垎澶у皬鍐?br />
includelib msvcrt.lib ;榪欓噷鏄紩鍏ョ被搴?#160;鐩稿綋浜?#160;#include<stdio.h>浜?#160;
printf PROTO C:DWORD,:VARARG ;榪欎釜灝辨槸澹版槑涓涓嬫垜浠鐢ㄧ殑鍑芥暟澶達(dá)紝鍒版椂鍊?#160;姹囩紪紼嬪簭浼?xì)鑷姩鍒癿svcrt.lib閲岄潰鎵劇殑浜?#160;
;:VARARG 琛ㄥ悗闈㈢殑鍙傛暟涓嶇‘瀹?#160;鍥犱負(fù)C灝辨槸榪欐牱鐨刾rintf(const char *, ...);
;榪欐牱鐨勫嚱鏁拌娉ㄦ剰 涓嶆槸琚皟鐢ㄥ嚱鏁拌礋璐f竻鏍?#160;鍥犱負(fù)瀹冩湰韜笉鐭ラ亾鏈夊灝戜釜鍙傛暟
;鑰屾槸鏈夎皟鐢ㄨ呰礋璐f竻鏍?#160; 涓嬮潰浼?xì)璇l嗚鏄?br />.data
szTextFmt BYTE '%d',0 ;榪欎釜鏄敤鏉ョ被鍨嬭漿鎹㈢殑錛岃窡C鐨勪竴鏍?瀛楃鐢ㄥ瓧鑺傜被鍨?br />a dword 1000 ;鍋囪
b dword 2000 ;澶勭悊鏁板奸兘鐢ㄥ弻瀛?#160;娌℃湁int 璺焞ong 鐨勫尯鍒?br />
;/////////////////////////////////////////////////////////////////////////////////////////
.code
_test proc A:DWORD,B:DWORD
push ebp
mov ebp,esp
mov eax,dword ptr ss:[ebp+8]
add eax,1
mov edx,dword ptr ss:[ebp+0Ch]
add edx,100
add eax,edx
pop ebp
retn 8
_test endp
_main proc
push dword ptr ds:b ;鍙嶆眹緙栨垜浠湅鍒扮殑b灝變笉鏄痓浜嗚屾槸涓涓猍*****]鏁板瓧 dword ptr 灝辨槸鎴戜滑鍦╠s(鏁版嵁孌?鎶奫*****]
;寮濮嬬殑涓涓弻瀛楅暱鏁板煎彇鍑烘潵
push dword ptr ds:a ;璺熷ス瀵瑰簲鐨勮繕鏈?#160;byte ptr ****灝辨槸鍙栦竴涓瓧鑺傚嚭鏉?#160;姣斿榪欐牱 mov al,byte ptr ds:szTextFmt
;灝辨妸 % 鍙栧嚭鏉?#160;鑰屼笉鍖呮嫭 d
call _test
push eax ;鍋囪push eax鐨勫湴鍧鏄?#215;××××
push offset szTextFmt
call printf
add esp,8
ret
_main endp
end _main
;////////////////////////////////////////////////////////////// 涓嬮潰浠嬬粛鍫嗘爤鐨勫彉鍖?br />棣栧厛瑕佹槑鐧界殑鏄?#160;鎿嶄綔鍫嗘爤孌?#160;ss 鍙兘鐢?#160;esp鎴杄bp瀵勫瓨鍣?#160;鍏朵粬鐨勫瘎瀛樺櫒eax ebx edx絳夐兘涓嶈兘澶熺敤 鑰?#160;esp姘歌繙鎸囧悜鍫嗘爤鏍堥《 ebp鐢ㄦ潵 鍦ㄥ爢鏍堟
閲岄潰瀵誨潃
push 鎸囦護(hù)鏄帇鏍?#160;ESP=ESP-4
pop 鎸囦護(hù)鏄嚭鏍?#160;ESP=ESP+4
鎴戜滑鍋囪main鍑芥暟涓寮濮嬪爢鏍堝畾鏄?#160;ESP=400
push dword ptr ds:b ;ESP-4=396 ->閲岄潰鐨勫煎氨鏄?#160;2000 灝辨槸b鐨勬暟鍊?br />push dword ptr ds:a ;ESP-4=392 ->閲岄潰鐨勫煎氨鏄?#160;1000 灝辨槸a鐨勬暟鍊?br />call test ;ESP-4=388錛?gt;閲岄潰鐨勬暟鍊兼槸浠涔堬紵榪欎釜澶噸瑕佷簡 灝辨槸鎴戜滑鐢ㄦ潵鎵炬父鎴忓嚱鏁扮殑鍘熺悊鎵鍦ㄣ?br /> 閲岄潰鐨勬暟鍊煎氨鏄痗all test 鎸囦護(hù)涓嬩竴鏉℃寚浠ょ殑鍦板潃錛?gt;鍗硃ush eax鐨勫湴鍧×××××
鍒頒簡test鍑芥暟閲岄潰
push ebp ;ESP-4=384->閲岄潰淇濆瓨浜嗗綋鍓峞bp鐨勫?#160;鑰屼笉鏄妸ebp娓呴浂
mov ebp,esp ;榪欓噷ESP錛?84灝辨病鍙樺寲浜嗭紝浣嗘槸 ebp=esp=384,涓轟粈涔堣榪欐牱鍋氬憿 鍥犱負(fù)鎴戜滑瑕佺敤ebp鍒板爢鏍堥噷闈㈡壘鍙傛暟
mov eax,dword ptr ss:[ebp+8] ;鍙嶆眹緙栨槸榪欐牱鐨?#160;鎯蟲兂涓轟粈涔坅灝辨槸[ebp+8]鍛?br /> ;鎴戜滑寰涓婄湅鐪嬪爢鏍堥噷鍦板潃392澶勫氨淇濆瓨鐫a鐨勫?#160;榪欓噷ebp=384 鍔犱笂8姝eソ灝辨槸392浜?br /> ;榪欐牱灝辨妸浼犻掕繃鏉ョ殑1000鎷夸簡鍑烘潵eax=1000
add eax,1 ;鐩稿綋浜?#160;a+1浜?#160;eax=1001
mov edx,dword ptr ss:[ebp+0Ch] ; 0Ch=12 涓鏍烽亾鐞嗚繖閲屾寚鍚戝爢鏍堢殑鍦板潃鏄?84+12=396 灝辨槸2000浜?#160;edx=2000
add edx,100 ;鐩稿綋浜?#160;b+100 edx=2100
add eax,edx ;eax=eax+edx錛?001錛?100錛?101 榪欓噷eax宸茬粡淇濆瓨浜嗘渶緇堢殑緇撴灉浜?#160;
;鍥犱負(fù)win32姹囩紪涓鑸敤eax榪斿洖緇撴灉 鎵浠ュ鏋滄渶緇堢粨鏋滀笉鏄湪eax閲岄潰鐨勮瘽 榪樿鎶婂畠鏀懼埌eax
;姣斿鍋囪鎴戠殑緇撴灉淇濆瓨鍦ㄥ彉閲弉Ret閲岄潰 鏈鍚庤繕鏄榪欐牱 mov eax,dword ptr nRet
pop ebp ;ESP=384+4=388 鑰屼繚瀛樺湪鏍堥《384鐨勫?#160;淇濆瓨鍒?#160;ebp涓?#160;鍗蟲仮澶峞bp鍘熸潵鐨勫?#160;
;鍥犱負(fù)涓寮濮嬫垜浠氨鎶奺bp鐨勫煎帇鏍堜簡錛宮ov ebp,esp宸茬粡鏀瑰彉浜唀bp鐨勫鹼紝榪欓噷鎭㈠灝辨槸淇濊瘉浜嗗爢鏍堝鉤琛?br />retn 8 ;ESP+8->396 榪欓噷retn鏄敱緋葷粺璋冪敤鐨?#160;鎴戜滑涓嶇敤綆?#160;緋葷粺浼?xì)鑷姩鎶奅IP鎸囬拡鎸囧悜 鍘熸潵鐨刢all鐨勪笅涓鏉℃寚浠?br /> ;鐢變簬鏄郴緇熻嚜鍔ㄦ仮澶嶄簡call閭i噷鐨勫帇鏍堟墍浠?#160;鐪熸榪斿洖鍒扮殑鏃跺橢SP+4灝辨槸鎭㈠浜哻all鍘嬫爤鐨勫爢鏍?br /> ;鍒頒簡榪欎釜鏃跺?#160;ESP=400 灝辨槸鍑芥暟璋冪敤寮濮嬬殑鍫嗘爤錛屽氨鏄鍑芥暟璋冪敤鍓嶈窡鍑芥暟璋冪敤鍚庣殑鍫嗘爤鏄竴鏍風(fēng)殑
;榪欏氨鏄爢鏍堝鉤琛?#160;
鐢變簬鎴戜滑鐢╯tdcall涓婇潰retn 8灝辨槸琚皟鐢ㄨ呰礋璐f仮澶嶅爢鏍堢殑鎰忔濅簡錛屽嚱鏁皌est鏄璋冪敤鑰咃紝鎵浠ヨ礋璐f妸鍫嗘爤鍔?,call 閭i噷鏄郴緇熻嚜鍔ㄦ仮澶嶇殑
push eax ;ESP-4=396->閲岄潰淇濆瓨浜唀ax鐨勫?101
;涓婇潰宸茬粡鐪嬪埌浜唀ax淇濆瓨鐫榪斿洖鍊鹼紝鎴戜滑瑕佹妸瀹冧紶緇檖rintf涔熸槸閫氳繃鍫嗘爤浼犻?#160;
push offset szTextFmt ;ESP-4=392->閲岄潰淇濆瓨浜唖zTextFmt鐨勫湴鍧 涔熷氨鏄疌閲岄潰鐨勬寚閽?#160;瀹為檯涓婃病鏈変粈涔堟妸瀛楃涓蹭紶閫掔殑錛屾垜浠紶鐨勯兘鏄湴鍧
;鏃犺鏄湪姹囩紪鎴朇 鎵浠ュ湪姹囩紪閲屾病鏈変粈涔堝瓧絎︿覆綾誨瀷 鐢ㄦ渶澶氱殑灝辨槸DWORD銆傚樋鍢挎父鎴忛噷闈紶閫掑弬鏁?#160;綆鍗曞浜?br />call printf ;ESP-4=388->閲岄潰淇濆瓨浜嗕笅涓鏉℃寚浠ょ殑鍦板潃
add esp,8 ;ESP+8=400 鎭㈠浜嗚皟鐢╬rintf鍓嶇殑鍫嗘爤鐘舵?br /> ;涓婇潰璇翠簡鐢變簬printf鍚庨潰鍙傛暟鏄?VARARG 榪欐牱鐨勭被鍨嬫槸鏈夎皟鐢ㄨ呮仮澶嶅爢鏍堢殑 鎵浠rintf閲岄潰娌℃湁retn 8涔嬬被鐨勬寚浠?br /> ;榪欐槸鐢辮皟鐢ㄨ呰礋璐f竻鏍?#160;main鏄皟鐢ㄨ?#160;鎵浠ヤ笅闈竴鍙ュ氨鏄?#160;add esp,8 鎶婂爢鏍堟仮澶嶅埌璋冪敤printf涔嬪墠
;鑰宑all printf閭i噷鐨勫帇鏍?#160;鏄敱緋葷粺鍋氱殑 鎭㈠鐨勫伐浣滀篃鏄郴緇熷畬鎴?#160;鎴戜滑涓嶇敤鐞?#160;鍙槸鐭ラ亾閲岄潰淇濆瓨鏄繑鍥炲湴鍧灝卞
;浜?br />ret ;main 鍑芥暟榪斿洖 鍏朵粬鐨勪簨鎯呮槸緋葷粺鑷姩鎼炲畾 鎴戜滑涓嶇敤鐞?#160;浠誨姟瀹屾垚
]]>
浣滆咃細(xì)璁稿紡浼?2006騫?1鏈堟煇鏃?/font>
鍐呭瓨綆$悊鏄疌++紼嬪簭鍛樼殑鐥涖傛垜鐨勩婂唴瀛樼鐞嗗彉闈┿嬬郴鍒楀氨鏄瘯鍥捐璁烘洿涓烘湁鏁堢殑鍐呭瓨綆$悊鏂瑰紡錛屼互鏉滅粷錛堟垨鍑忓皯錛夊唴瀛樻硠婕忥紝鍑忚交C++紼嬪簭鍛樼殑璐熸媴銆傜敱浜庡伐浣滃繖鐨勭紭鏁咃紝榪欎釜緋誨垪鐩墠鏈畬錛屾殏鍋溿?br />聽
榪欑瘒鐭枃鎴戞兂鎹釜鏂瑰紡錛岃璁轟竴涓嬪浣曚互鏈蹇殑閫熷害鎵懼埌鍐呭瓨娉勬紡銆?br />聽
紜鏄惁瀛樺湪鍐呭瓨娉勬紡
鎴戜滑鐭ラ亾錛孧FC紼嬪簭濡傛灉媯嫻嬪埌瀛樺湪鍐呭瓨娉勬紡錛岄鍑虹▼搴忕殑鏃跺欎細(xì)鍦ㄨ皟璇曠獥鍙f彁閱掑唴瀛樻硠婕忋備緥濡傦細(xì)
class CMyApp : public CWinApp
{
public:
聽聽 BOOL InitApplication()
聽聽 {
聽聽聽聽聽聽 int* leak = new int[10];
聽聽聽聽聽聽 return TRUE;
聽聽 }
};
浜х敓鐨勫唴瀛樻硠婕忔姤鍛婂ぇ浣撳涓嬶細(xì)
Detected memory leaks!
Dumping objects ->
c:\work\test.cpp(186) : {52} normal block at 0x003C4410, 40 bytes long.
聽Data: <聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
榪欐尯濂姐傞棶棰樻槸錛屽鏋滄垜浠笉鍠滄MFC錛岄偅涔堥毦閬撳氨娌℃湁鍔炴硶錛熸垨鑰呰嚜宸卞仛錛?
鍛靛懙錛岃繖涓嶉渶瑕併傚叾瀹烇紝MFC涔熸病鏈夎嚜宸卞仛銆傚唴瀛樻硠婕忔嫻嬬殑宸ヤ綔鏄疺C++鐨凜榪愯搴撳仛鐨勩備篃灝辨槸璇達(dá)紝鍙浣犳槸VC++紼嬪簭鍛橈紝閮藉彲浠ュ緢鏂逛究鍦版嫻嬪唴瀛樻硠婕忋傛垜浠繕鏄粰涓牱渚嬶細(xì)
#include <crtdbg.h>
inline void EnableMemLeakCheck()
{
聽聽 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}
void main()
{
聽聽 EnableMemLeakCheck();
聽聽 int* leak = new int[10];
}
聽榪愯錛堟彁閱掞細(xì)涓嶈鎸塁trl+F5錛屾寜F5錛夛紝浣犲皢鍙戠幇錛屼駭鐢熺殑鍐呭瓨娉勬紡鎶ュ憡涓嶮FC綾諱技錛屼絾鏈夌粏鑺備笉鍚岋紝濡備笅錛?/font>
Detected memory leaks!
Dumping objects ->
{52} normal block at 0x003C4410, 40 bytes long.
聽Data: <聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
涓轟粈涔堝憿錛熺湅涓嬮潰銆?/font>
瀹氫綅鍐呭瓨娉勬紡鐢變簬鍝竴鍙ヨ瘽寮曡搗鐨?br />浣犲凡緇忓彂鐜扮▼搴忓瓨鍦ㄥ唴瀛樻硠婕忋傜幇鍦ㄧ殑闂鏄紝鎴戜滑瑕佹壘娉勬紡鐨勬牴婧愩?/font>
涓鑸垜浠鍏堢‘瀹氬唴瀛樻硠婕忔槸鐢變簬鍝竴鍙ュ紩璧楓傚湪MFC涓紝榪欎竴鐐瑰緢瀹規(guī)槗銆備綘鍙屽嚮鍐呭瓨娉勬紡鎶ュ憡鐨勬枃瀛楋紝鎴栬呭湪Debug紿楀彛涓寜F4錛孖DE灝卞府浣犲畾浣嶅埌鐢寵璇ュ唴瀛樺潡鐨勫湴鏂廣傚浜庝笂渚嬶紝涔熷氨鏄繖涓鍙ワ細(xì)
聽聽 int* leak = new int[10];
榪欏澶氬皯灝戝浣犲垎鏋愬唴瀛樻硠婕忔湁鐐瑰府鍔┿傜壒鍒湴錛屽鏋滆繖涓猲ew浠呭搴斾竴鏉elete錛堟垨鑰呬綘鎶奷elete婕忓啓錛夛紝榪欏皢寰堝揩鍙互紜闂鐨勭棁緇撱?
鎴戜滑鍓嶉潰宸茬粡鐪嬪埌錛屼笉浣跨敤MFC鐨勬椂鍊欙紝鐢熸垚鐨勫唴瀛樻硠婕忔姤鍛婁笌MFC涓嶅悓錛岃屼笖浣犵珛鍒誨彂鐜版寜F4涓嶇伒銆傞偅涔堥毦閬揗FC鍋氫簡浠涔堟墜鑴氾紵
鍏跺疄涓嶆槸錛屾垜浠潵妯℃嫙涓婱FC鍋氱殑浜嬫儏銆傜湅涓嬩緥錛?
inline void EnableMemLeakCheck()
{
聽聽 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}
#ifdef _DEBUG
#define new聽聽 new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
void main()
{
聽聽 EnableMemLeakCheck();
聽聽 int* leak = new int[10];
}
鍐嶈繍琛岃繖涓牱渚嬶紝浣犳儕鍠滃湴鍙戠幇錛岀幇鍦ㄥ唴瀛樻硠婕忔姤鍛婂拰MFC娌℃湁浠諱綍鍒嗗埆浜嗐?/font>
蹇熸壘鍒板唴瀛樻硠婕?br />鍗曠‘瀹氫簡鍐呭瓨娉勬紡鍙戠敓鍦ㄥ摢涓琛岋紝鏈夋椂鍊欏茍涓嶈凍澶熴傜壒鍒槸鍚屼竴涓猲ew瀵瑰簲鏈夊澶勯噴鏀劇殑鎯呭艦銆傚湪瀹為檯鐨勫伐紼嬩腑錛屼互涓嬩袱縐嶆儏鍐靛緢鍏稿瀷錛?
鍒涘緩瀵硅薄鐨勫湴鏂規(guī)槸涓涓被宸ュ巶錛圕lassFactory錛夋ā寮忋傚緢澶氱敋鑷沖叏閮ㄧ被瀹炰緥鐢卞悓涓涓猲ew鍒涘緩銆傚浜庢錛屽畾浣嶅埌浜唍ew鍑哄璞$殑鎵鍦ㄨ鍩烘湰娌℃湁澶氬ぇ甯姪銆?
聽
COM瀵硅薄銆傛垜浠煡閬揅OM瀵硅薄閲囩敤Reference Count緇存姢鐢熷懡鍛ㄦ湡銆備篃灝辨槸璇達(dá)紝瀵硅薄new鐨勫湴鏂瑰彧鏈変竴涓紝浣嗘槸Release鐨勫湴鏂瑰緢澶氾紝浣犺涓涓釜鎺掗櫎銆?
閭d箞錛屾湁浠涔堝ソ鍔炴硶錛屽彲浠ヨ繀閫熷畾浣嶅唴瀛樻硠婕忥紵
絳旓細(xì)鏈夈?/font>
鍦ㄥ唴瀛樻硠婕忔儏鍐靛鏉傜殑鏃跺欙紝浣犲彲浠ョ敤浠ヤ笅鏂規(guī)硶瀹氫綅鍐呭瓨娉勬紡銆傝繖鏄垜涓漢璁や負(fù)閫氱敤鐨勫唴瀛樻硠婕忚拷韙柟娉曚腑鏈鏈夋晥鐨勬墜孌點?/font>
鎴戜滑鍐嶅洖澶寸湅鐪媍rtdbg鐢熸垚鐨勫唴瀛樻硠婕忔姤鍛婏細(xì)
Detected memory leaks!
Dumping objects ->
c:\work\test.cpp(186) : {52} normal block at 0x003C4410, 40 bytes long.
聽Data: <聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 > CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD CD
Object dump complete.
闄や簡浜х敓璇ュ唴瀛樻硠婕忕殑鍐呭瓨鍒嗛厤璇彞鎵鍦ㄧ殑鏂囦歡鍚嶃佽鍙蜂負(fù)錛屾垜浠敞鎰忓埌鏈変竴涓瘮杈冮檶鐢熺殑淇℃伅錛歿52}銆傝繖涓暣鏁板間唬琛ㄤ簡浠涔堟剰鎬濆憿錛?/font>
鍏跺疄錛屽畠浠h〃浜嗙鍑犳鍐呭瓨鍒嗛厤鎿嶄綔銆傝薄榪欎釜渚嬪瓙錛寋52}浠h〃浜嗙52嬈″唴瀛樺垎閰嶆搷浣滃彂鐢熶簡娉勬紡銆備綘鍙兘瑕佽錛屾垜鍙猲ew榪囦竴嬈★紝鎬庝箞浼?xì)鏄W?2嬈★紵榪欏緢瀹規(guī)槗鐞嗚В錛屽叾浠栫殑鍐呭瓨鐢寵鎿嶄綔鍦–鐨勫垵濮嬪寲榪囩▼璋冪敤鐨勫憲銆?)
鏈夋病鏈夊彲鑳斤紝鎴戜滑璁╃▼搴忚繍琛屽埌絎?2嬈″唴瀛樺垎閰嶆搷浣滅殑鏃跺欙紝鑷姩鍋滀笅鏉ワ紝榪涘叆璋冭瘯鐘舵侊紵鎵騫革紝crtdbg紜疄鎻愪緵浜嗚繖鏍風(fēng)殑鍑芥暟錛氬嵆 long _CrtSetBreakAlloc(long nAllocID)銆傛垜浠姞涓婂畠錛?/font>
inline void EnableMemLeakCheck()
{
聽聽 _CrtSetDbgFlag(_CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) | _CRTDBG_LEAK_CHECK_DF);
}
#ifdef _DEBUG
#define new聽聽 new(_NORMAL_BLOCK, __FILE__, __LINE__)
#endif
void main()
{
聽聽 EnableMemLeakCheck();
聽聽 _CrtSetBreakAlloc(52);
聽聽 int* leak = new int[10];
}
浣犲彂鐜幫紝紼嬪簭榪愯鍒?int* leak = new int[10]; 涓鍙ユ椂錛岃嚜鍔ㄥ仠涓嬫潵榪涘叆璋冭瘯鐘舵併傜粏緇嗕綋浼?xì)涓涓嬶紝浣犲彲浠ュ彂鐜幫紝榪欑鏂瑰紡浣犺幏寰楃殑淇℃伅榪滄瘮鍦ㄧ▼搴忛鍑烘椂鑾峰緱鏂囦歡鍚嶅強(qiáng)琛屽彿鏈変環(huán)鍊煎緱澶氥傚洜涓烘姤鍛婃硠婕忔枃浠跺悕鍙?qiáng)琛屽忴P紝浣犺幏寰楃殑鍙槸闈欐佺殑淇℃伅錛岀劧鑰宊CrtSetBreakAlloc鍒欐槸鎶婃暣涓幇鍦烘仮澶嶏紝浣犲彲浠ラ氳繃瀵瑰嚱鏁拌皟鐢ㄦ爤鍒嗘瀽錛堟垜鍙戠幇寰堝浜轟笉涔?fàn)鎯湅鍑芥暟璋冪敤鏍堝Q屽鏋滀綘灞炰簬榪欑鎯呭喌錛屾垜寮虹儓鎺ㄨ崘浣犲幓琛ヤ笂榪欎竴璇撅紝鍥犱負(fù)瀹冨お閲嶈浜嗭級浠ュ強(qiáng)鍏朵粬鍦ㄧ嚎璋冭瘯鎶宸э紝鏉ュ垎鏋愪駭鐢熷唴瀛樻硠婕忕殑鍘熷洜銆傞氬父鎯呭喌涓嬶紝榪欑鍒嗘瀽鏂規(guī)硶鍙互鍦?鍒嗛挓鍐呮壘鍒拌倗浜嬭呫?/font>
褰撶劧錛宊CrtSetBreakAlloc瑕佹眰浣犵殑紼嬪簭鎵ц榪囩▼鏄彲榪樺師鐨勶紙澶氭鎵ц榪囩▼鐨勫唴瀛樺垎閰嶉『搴忎笉浼?xì)鍙戠敓鍙樺寲锛夈傝繖涓亣璁懼湪澶氭暟鎯呭喌涓嬫垚绔嬨備笉榪囷紝鍦ㄥ綰跨▼鐨勬儏鍐典笅錛岃繖涓鐐規(guī)湁鏃墮毦浠ヤ繚璇併?/font>
聽
闄勫姞璇存槑錛?br />瀵光滃唴瀛樼鐞嗏濈浉鍏崇殑鎶鏈劅鍏磋叮錛熻繖閲屽彲浠ョ湅鍒版垜鐨勬墍鏈夊叧浜庡唴瀛樼鐞嗙殑鏂囩珷銆?/font>
聽
鏈枃鏉ヨ嚜CSDN鍗氬錛岃漿杞借鏍囨槑鍑哄錛?/font> http://blog.csdn.net/i_like_cpp/archive/2007/06/28/1669962.aspx