锘??xml version="1.0" encoding="utf-8" standalone="yes"?>国产亚洲精品影视在线,久久91亚洲人成电影网站,亚洲国产成人无码AV在线影院http://m.tkk7.com/shiliqiang/category/40697.htmlI'm on my way!zh-cnSun, 20 Sep 2009 21:07:24 GMTSun, 20 Sep 2009 21:07:24 GMT60瀵瑰拰鏍堢殑鍖哄埆http://m.tkk7.com/shiliqiang/articles/295780.html鐭沖ご@鐭沖ご@Sun, 20 Sep 2009 14:15:00 GMThttp://m.tkk7.com/shiliqiang/articles/295780.htmlhttp://m.tkk7.com/shiliqiang/comments/295780.htmlhttp://m.tkk7.com/shiliqiang/articles/295780.html#Feedback0http://m.tkk7.com/shiliqiang/comments/commentRss/295780.htmlhttp://m.tkk7.com/shiliqiang/services/trackbacks/295780.html涓銆侀澶囩煡璇嗏旂▼搴忕殑鍐呭瓨鍒嗛厤

鐢盋/C++緙栬瘧鐨勭▼搴忓崰鐢ㄧ殑鍐呭瓨鍒嗕負(fù)浠ヤ笅鍑犱釜閮ㄥ垎
1銆佹爤鍖?stack)錛?鐢辯紪璇戝櫒鑷姩鍒嗛厤閲婃斁 錛屽瓨鏀懼嚱鏁扮殑鍙傛暟鍊鹼紝灞閮ㄥ彉閲忕殑鍊肩瓑銆傚叾鎿嶄綔鏂瑰紡綾諱技浜庢暟鎹粨鏋勪腑鐨勬爤銆?
2銆佸爢鍖?heap)錛?涓鑸敱紼嬪簭鍛樺垎閰嶉噴鏀撅紝 鑻ョ▼搴忓憳涓嶉噴鏀撅紝紼嬪簭緇撴潫鏃跺彲鑳界敱O(jiān)S鍥炴敹銆傛敞鎰忓畠涓庢暟鎹粨鏋勪腑鐨勫爢鏄袱鍥炰簨錛屽垎閰嶆柟寮忓掓槸綾諱技浜庨摼琛ㄣ?
3銆佸叏灞鍖?static)錛?鍏ㄥ眬鍙橀噺鍜岄潤鎬佸彉閲忕殑瀛樺偍鏄斁鍦ㄤ竴鍧楃殑錛屽垵濮嬪寲鐨勫叏灞鍙橀噺鍜岄潤鎬佸彉閲忓湪涓鍧楀尯鍩燂紝 鏈垵濮嬪寲鐨勫叏灞鍙橀噺鍜屾湭鍒濆鍖栫殑闈欐佸彉閲忓湪鐩擱偦鐨勫彟涓鍧楀尯鍩燂紝紼嬪簭緇撴潫鍚庢湁緋葷粺閲婃斁 銆?
4銆佹枃瀛楀父閲忓尯錛?甯擱噺瀛楃涓插氨鏄斁鍦ㄨ繖閲岀殑錛?紼嬪簭緇撴潫鍚庣敱緋葷粺閲婃斁銆?
5銆佺▼搴忎唬鐮佸尯錛?瀛樻斁鍑芥暟浣撶殑浜岃繘鍒朵唬鐮併?

Example:

int a = 0; // 鍏ㄥ眬鍒濆鍖栧尯
char *p1; // 鍏ㄥ眬鏈垵濮嬪寲鍖?
main()
{
int b; // 鏍?
char s[] = "abc"; // 鏍?
char *p2; // 鏍?
char *p3 = "123456"; // 123456\0鍦ㄥ父閲忓尯錛宲3鍦ㄦ爤涓娿?
static int c =0錛?// 鍏ㄥ眬(闈欐?鍒濆鍖栧尯
p1 = (char *)malloc(10);
p2 = (char *)malloc(20); // 鍒嗛厤寰楁潵寰?0鍜?0瀛楄妭鐨勫尯鍩熷氨鍦ㄥ爢鍖恒?
strcpy(p1, "123456"); // 123456\0鏀懼湪甯擱噺鍖猴紝緙栬瘧鍣ㄥ彲鑳戒細(xì)灝嗗畠涓巔3鎵鎸囧悜鐨?123456"浼樺寲鎴愪竴涓湴鏂廣?
}


浜屻佸爢鍜屾爤鐨勭悊璁虹煡璇?

2.1 鐢寵鏂瑰紡
鏍? 鐢辯郴緇熻嚜鍔ㄥ垎閰嶃?渚嬪錛屽0鏄庡湪鍑芥暟涓竴涓眬閮ㄥ彉閲?int b; 緋葷粺鑷姩鍦ㄦ爤涓負(fù)b寮杈熺┖闂?
鍫? 闇瑕佺▼搴忓憳鑷繁鐢寵錛屽茍鎸囨槑澶у皬錛屽湪c涓璵alloc鍑芥暟錛氬p1 = (char *)malloc(10); 鍦–++涓敤new榪愮畻絎?濡俻2 = (char *)malloc(10); 浣嗘槸娉ㄦ剰p1銆乸2鏈韓鏄湪鏍堜腑鐨勩?

2.2 鐢寵鍚庣郴緇熺殑鍝嶅簲
鏍堬細(xì)鍙鏍堢殑鍓╀綑絀洪棿澶т簬鎵鐢寵絀洪棿錛岀郴緇熷皢涓虹▼搴忔彁渚涘唴瀛橈紝鍚﹀垯灝嗘姤寮傚父鎻愮ず鏍堟孩鍑恒?
鍫嗭細(xì)棣栧厛搴旇鐭ラ亾鎿嶄綔緋葷粺鏈変竴涓褰曠┖闂插唴瀛樺湴鍧鐨勯摼琛紝褰撶郴緇熸敹鍒扮▼搴忕殑鐢寵鏃訛紝 浼?xì)閬嶅巻璇ラ摼琛ㄥQ屽鎵劇涓涓┖闂村ぇ浜庢墍鐢寵絀洪棿鐨勫爢緇撶偣錛岀劧鍚庡皢璇ョ粨鐐逛粠絀洪棽緇撶偣閾捐〃涓垹闄わ紝騫跺皢璇ョ粨鐐圭殑絀洪棿鍒嗛厤緇欑▼搴忥紝鍙﹀錛屽浜庡ぇ澶氭暟緋葷粺錛屼細(xì)鍦ㄨ繖鍧? 鍐呭瓨絀洪棿涓殑棣栧湴鍧澶勮褰曟湰嬈″垎閰嶇殑澶у皬錛岃繖鏍鳳紝浠g爜涓殑delete璇彞鎵嶈兘姝g‘鐨勯噴鏀炬湰鍐呭瓨絀洪棿銆傚彟澶栵紝鐢變簬鎵懼埌鐨勫爢緇撶偣鐨勫ぇ灝忎笉涓瀹氭濂界瓑浜庣敵璇風(fēng)殑 澶у皬錛岀郴緇熶細(xì)鑷姩鐨勫皢澶氫綑鐨勯偅閮ㄥ垎閲嶆柊鏀懼叆絀洪棽閾捐〃涓?

2.3 鐢寵澶у皬鐨勯檺鍒?
鏍堬細(xì)鍦╓indows涓?鏍堟槸鍚戜綆鍦板潃鎵╁睍鐨勬暟鎹粨鏋勶紝鏄竴鍧楄繛緇殑鍐呭瓨鍖哄煙銆傝繖鍙ヨ瘽鐨勬剰鎬濇槸鏍堥《鐨勫湴鍧鍜屾爤鐨勬渶澶у閲忔槸緋葷粺棰勫厛瑙勫畾濂界殑錛屽湪 WINDOWS涓嬶紝鏍堢殑澶у皬鏄?M(涔熸湁鐨勮鏄?M錛屾諱箣鏄竴涓紪璇戞椂灝辯‘瀹氱殑甯告暟)錛屽鏋滅敵璇風(fēng)殑絀洪棿瓚呰繃鏍堢殑鍓╀綑絀洪棿鏃訛紝灝嗘彁紺簅verflow銆傚洜 姝わ紝鑳戒粠鏍堣幏寰楃殑絀洪棿杈冨皬銆?
鍫嗭細(xì)鍫嗘槸鍚戦珮鍦板潃鎵╁睍鐨勬暟鎹粨鏋勶紝鏄笉榪炵畫鐨勫唴瀛樺尯鍩熴傝繖鏄敱浜庣郴緇熸槸鐢ㄩ摼琛ㄦ潵瀛樺偍鐨勭┖闂插唴瀛樺湴鍧鐨勶紝鑷劧鏄笉榪炵畫鐨勶紝鑰岄摼琛ㄧ殑閬嶅巻鏂瑰悜鏄敱浣庡湴鍧鍚戦珮鍦板潃銆傚爢鐨勫ぇ灝忓彈闄愪簬璁$畻鏈虹郴緇熶腑鏈夋晥鐨勮櫄鎷熷唴瀛樸傜敱姝ゅ彲瑙侊紝鍫嗚幏寰楃殑絀洪棿姣旇緝鐏墊椿錛屼篃姣旇緝澶с?

2.4 鐢寵鏁堢巼鐨勬瘮杈冿細(xì)
鏍堬細(xì)鐢辯郴緇熻嚜鍔ㄥ垎閰嶏紝閫熷害杈冨揩銆備絾紼嬪簭鍛樻槸鏃犳硶鎺у埗鐨勩?
鍫嗭細(xì)鏄敱new鍒嗛厤鐨勫唴瀛橈紝涓鑸熷害姣旇緝鎱紝鑰屼笖瀹規(guī)槗浜х敓鍐呭瓨紕庣墖,涓嶈繃鐢ㄨ搗鏉ユ渶鏂逛究銆?
鍙﹀錛屽湪WINDOWS涓嬶紝鏈濂界殑鏂瑰紡鏄敤VirtualAlloc鍒嗛厤鍐呭瓨錛屼粬涓嶆槸鍦ㄥ爢錛屼篃涓嶆槸鍦ㄦ爤鏄洿鎺ュ湪榪涚▼鐨勫湴鍧絀洪棿涓繚鐣欎竴蹇唴瀛橈紝铏界劧鐢ㄨ搗鏉ユ渶涓嶆柟渚褲備絾鏄熷害蹇紝涔熸渶鐏墊椿銆?

2.5 鍫嗗拰鏍堜腑鐨勫瓨鍌ㄥ唴瀹?
鏍堬細(xì) 鍦ㄥ嚱鏁拌皟鐢ㄦ椂錛岀涓涓繘鏍堢殑鏄富鍑芥暟涓悗鐨勪笅涓鏉℃寚浠?鍑芥暟璋冪敤璇彞鐨勪笅涓鏉″彲鎵ц璇彞)鐨勫湴鍧錛岀劧鍚庢槸鍑芥暟鐨勫悇涓弬鏁幫紝鍦ㄥぇ澶氭暟鐨凜緙栬瘧鍣ㄤ腑錛屽弬鏁版槸鐢? 鍙沖線宸﹀叆鏍堢殑錛岀劧鍚庢槸鍑芥暟涓殑灞閮ㄥ彉閲忋傛敞鎰忛潤鎬佸彉閲忔槸涓嶅叆鏍堢殑銆傚綋鏈鍑芥暟璋冪敤緇撴潫鍚庯紝灞閮ㄥ彉閲忓厛鍑烘爤錛岀劧鍚庢槸鍙傛暟錛屾渶鍚庢爤欏舵寚閽堟寚鍚戞渶寮濮嬪瓨鐨勫湴鍧錛? 涔熷氨鏄富鍑芥暟涓殑涓嬩竴鏉℃寚浠わ紝紼嬪簭鐢辮鐐圭戶緇繍琛屻?
鍫嗭細(xì)涓鑸槸鍦ㄥ爢鐨勫ご閮ㄧ敤涓涓瓧鑺傚瓨鏀懼爢鐨勫ぇ灝忋傚爢涓殑鍏蜂綋鍐呭鏈夌▼搴忓憳瀹夋帓銆?

2.6 瀛樺彇鏁堢巼鐨勬瘮杈?
char s1[] = "aaaaaaaaaaaaaaa";
char *s2 = "bbbbbbbbbbbbbbbbb";
aaaaaaaaaaa鏄湪榪愯鏃跺埢璧嬪肩殑錛?
鑰宐bbbbbbbbbb鏄湪緙栬瘧鏃跺氨紜畾鐨勶紱
浣嗘槸錛屽湪浠ュ悗鐨勫瓨鍙栦腑錛屽湪鏍堜笂鐨勬暟緇勬瘮鎸囬拡鎵鎸囧悜鐨勫瓧絎︿覆(渚嬪鍫?蹇?
姣斿錛?
#include
void main()
{
char a = 1;
char c[] = "1234567890";
char *p ="1234567890";
a = c[1];
a = p[1];
return;
}
瀵瑰簲鐨勬眹緙栦唬鐮?
10: a = c[1];
00401067 8A 4D F1 mov cl,byte ptr [ebp-0Fh]
0040106A 88 4D FC mov byte ptr [ebp-4],cl
11: a = p[1];
0040106D 8B 55 EC mov edx,dword ptr [ebp-14h]
00401070 8A 42 01 mov al,byte ptr [edx+1]
00401073 88 45 FC mov byte ptr [ebp-4],al
絎竴縐嶅湪璇誨彇鏃剁洿鎺ュ氨鎶婂瓧絎︿覆涓殑鍏冪礌璇誨埌瀵勫瓨鍣╟l涓紝鑰岀浜岀鍒欒鍏堟妸鎸囬拡鍊艱鍒癳dx涓紝鍦ㄦ牴鎹甧dx璇誨彇瀛楃錛屾樉鐒舵參浜嗐?

2.7 灝忕粨錛?
鍫嗗拰鏍堢殑鍖哄埆鍙互鐢ㄥ涓嬬殑姣斿柣鏉ョ湅鍑猴細(xì) 浣跨敤鏍堝氨璞℃垜浠幓楗閲屽悆楗紝鍙鐐硅彍(鍙戝嚭鐢寵)銆佷粯閽便佸拰鍚?浣跨敤)錛屽悆楗變簡灝辮蛋錛屼笉蹇呯悊浼?xì)鍒囪彍銆佹礂鑿滅瓑鍑嗗宸ヤ綔鍜屾礂紕椼佸埛閿呯瓑鎵熬宸ヤ綔錛屼粬鐨勫ソ澶勬槸 蹇嵎錛屼絾鏄嚜鐢卞害灝忋?浣跨敤鍫嗗氨璞℃槸鑷繁鍔ㄦ墜鍋氬枩嬈㈠悆鐨勮彍鑲達(dá)紝姣旇緝楹葷儲錛屼絾鏄瘮杈冪鍚堣嚜宸辯殑鍙e懗錛岃屼笖鑷敱搴﹀ぇ銆?

榪樻湁灝辨槸鍑芥暟璋冪敤鏃朵細(xì)鍦ㄦ爤涓婃湁涓緋誨垪鐨勪繚鐣欑幇鍦哄強(qiáng)浼犻掑弬鏁扮殑鎿嶄綔銆傛爤鐨勭┖闂村ぇ灝忔湁闄愬畾錛孷C鐨勭己鐪佹槸2M銆傛爤涓嶅鐢ㄧ殑鎯呭喌涓鑸槸紼嬪簭涓垎閰嶄簡澶ч噺鏁扮粍鍜岄? 褰掑嚱鏁板眰嬈″お娣便傛湁涓鐐瑰繀欏葷煡閬擄紝褰撲竴涓嚱鏁拌皟鐢ㄥ畬榪斿洖鍚庡畠浼?xì)閲婃斁璇ュ嚱鏁颁腑鎵鏈夌殑鏍堢┖闂淬傛爤鏄敱緙栬瘧鍣ㄨ嚜鍔ㄧ鐞嗙殑錛屼笉鐢ㄤ綘鎿嶅績銆傚爢鏄姩鎬佸垎閰嶅唴瀛樼殑錛屽茍涓? 浣犲彲浠ュ垎閰嶄嬌鐢ㄥ緢澶х殑鍐呭瓨銆備絾鏄敤涓嶅ソ浼?xì)漶旂敓鍐呭瓨娉勬紡銆傚茍涓旈綣佸湴malloc鍜宖ree浼?xì)漶旂敓鍐呭瓨纰庣?鏈夌偣綾諱技紓佺洏紕庣墖)錛屽洜涓篊鍒嗛厤鍔ㄦ佸唴瀛樻椂鏄 鎵懼尮閰嶇殑鍐呭瓨鐨勩傝岀敤鏍堝垯涓嶄細(xì)浜х敓紕庣墖銆傚湪鏍堜笂瀛樺彇鏁版嵁姣旈氳繃鎸囬拡鍦ㄥ爢涓婂瓨鍙栨暟鎹揩浜涖備竴鑸ぇ瀹惰鐨勫爢鏍堝拰鏍堟槸涓鏍風(fēng)殑錛屽氨鏄爤(stack)錛岃岃鍫嗘椂鎵? 鏄爢heap銆傛爤鏄厛鍏ュ悗鍑虹殑錛屼竴鑸槸鐢遍珮鍦板潃鍚戜綆鍦板潃鐢熼暱銆?br />

鎽樿嚜錛歨ttp://linux.chinaunix.net/bbs/thread-1119653-1-1.html


鐭沖ご@ 2009-09-20 22:15 鍙戣〃璇勮
]]>
鑳芥祴璇曚綘c璇█鍔熷簳鐨勫嚑涓棶棰?/title><link>http://m.tkk7.com/shiliqiang/articles/291970.html</link><dc:creator>鐭沖ご@</dc:creator><author>鐭沖ご@</author><pubDate>Thu, 20 Aug 2009 08:24:00 GMT</pubDate><guid>http://m.tkk7.com/shiliqiang/articles/291970.html</guid><wfw:comment>http://m.tkk7.com/shiliqiang/comments/291970.html</wfw:comment><comments>http://m.tkk7.com/shiliqiang/articles/291970.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/shiliqiang/comments/commentRss/291970.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/shiliqiang/services/trackbacks/291970.html</trackback:ping><description><![CDATA[涓嬮潰鏈夊嚑涓畝鍗曟祴璇曞皢鑳藉彂鐜頒綘瀵笴璇█鐨勬帉鎻℃儏鍐點(diǎn)?br /> int x=35;<br /> char str[10];<br /> //闂細(xì)strlen(str)鍜宻izeof(str)鐨勫煎垎鍒槸澶氬皯錛?br /> strcpy(str,"www.it315.org"/*鍏?3涓瓧姣?/);<br /> //闂?姝ゆ椂x鍜宻trlen(str)鐨勫煎垎鍒槸澶氬皯錛?br /> str="it315.org";//緙栬瘧鑳介氳繃鍚楋紵<br /> char *pstr;<br /> strcpy(pstr,"http://www.it315.org");<br /> //涓婂彞緙栬瘧鑳介氳繃鍚楋紵榪愯鏃舵湁闂鍚楋紵<br /> const char *p1;<br /> char * const p2;<br /> //涓婇潰涓ゅ彞鏈変粈涔堝尯鍒悧錛?br /> p1=(const char *)str;<br /> //濡傛灉鏄痯1=str錛涚紪璇戣兘澶熼氳繃鍚楋紵鏄庣櫧涓轟粈涔堣綾誨瀷杞崲錛熺被鍨嬭漿鎹㈢殑鏈川鏄粈涔堬紵<br /> strcpy(p1,"abc");//緙栬瘧鑳藉閫氳繃鍚楋紵<br /> printf("%d",str);//鏈夐棶棰樺悧錛?br /> pstr=3000;//緙栬瘧鑳借繃鍚楋紵濡傛灉涓嶈錛岃濡備綍淇敼浠ヤ繚璇佺紪璇戦氳繃鍛紵<br /> long y=(long)pstr;//鍙互榪欐牱鍋氬悧錛?br /> int *p=str;<br /> *p=0x00313200;<br /> printf("%s",str);//浼?xì)鏄粈涔堟晥鏋滐紵鎻愮ず0x31瀵瑰簲瀛楃'1',0x32瀵瑰簲瀛楃'2'銆?br /> p=3000;//p+1鐨勭粨鏋滀細(xì)鏄灝戯紵<br /> char *pc=new char[100];//涓婅堪璇彞鍦ㄥ唴瀛樹腑鍗犳嵁鍑犱釜鍐呭瓨鍧楋紝鎬庢牱鐨勫竷灞鎯呭喌錛?br /> void test(char **p)<br /> {<br />        *p=new char[100];<br /> }//榪欎釜緙栬瘧鍑芥暟鏈夐棶棰樺悧錛熷闈㈣璋冪敤榪欎釜鍑芥暟錛岃鎬庢牱浼犻掑弬鏁幫紵<br /> //鑳芥槑鐧絫ypedef int (*PFUN)(int x,int y)鍙?qiáng)鍏朵綔鐢ㄥ悧锛?br /> <br /> <br /> 鎽樿嚜錛歨ttp://blog.csdn.net/zhangxiaoxiang/archive/2006/05/17/742931.aspx<br /> <img src ="http://m.tkk7.com/shiliqiang/aggbug/291970.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/shiliqiang/" target="_blank">鐭沖ご@</a> 2009-08-20 16:24 <a href="http://m.tkk7.com/shiliqiang/articles/291970.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>void 綾誨瀷鐨勬寚閽?/title><link>http://m.tkk7.com/shiliqiang/articles/290353.html</link><dc:creator>鐭沖ご@</dc:creator><author>鐭沖ご@</author><pubDate>Sat, 08 Aug 2009 09:09:00 GMT</pubDate><guid>http://m.tkk7.com/shiliqiang/articles/290353.html</guid><wfw:comment>http://m.tkk7.com/shiliqiang/comments/290353.html</wfw:comment><comments>http://m.tkk7.com/shiliqiang/articles/290353.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://m.tkk7.com/shiliqiang/comments/commentRss/290353.html</wfw:commentRss><trackback:ping>http://m.tkk7.com/shiliqiang/services/trackbacks/290353.html</trackback:ping><description><![CDATA[<div id="iwcyu6k" class="posttitle"> <a id="viewpost1_TitleUrl" class="singleposttitle" >void鎸囬拡</a> </div> 鎸囬拡鏈変袱涓睘鎬?鎸囧悜鍙橀噺/瀵硅薄鐨?font style="line-height: 1.3em;" color="#ff0000">鍦板潃</font> <div id="aqy24ii" class="singlepost"><wbr>鍜?font style="line-height: 1.3em;" color="#ff0000">闀垮害</font><wbr> <br /> 浣嗘槸鎸囬拡鍙瓨鍌ㄥ湴鍧,闀垮害鍒欏彇鍐充簬鎸囬拡鐨勭被鍨?<br /> 緙栬瘧鍣ㄦ牴鎹寚閽堢殑綾誨瀷浠庢寚閽堟寚鍚戠殑鍦板潃鍚戝悗瀵誨潃 <br /> 鎸囬拡綾誨瀷涓嶅悓鍒欏鍧鑼冨洿涔熶笉鍚?姣斿: <br /> int*浠庢寚瀹氬湴鍧鍚戝悗瀵繪壘4瀛楄妭浣滀負(fù)鍙橀噺鐨勫瓨鍌ㄥ崟鍏?<br /> double*浠庢寚瀹氬湴鍧鍚戝悗瀵繪壘8瀛楄妭浣滀負(fù)鍙橀噺鐨勫瓨鍌ㄥ崟鍏?<br /> <br /> 1.void鎸囬拡鏄竴縐嶇壒鍒殑鎸囬拡 <br />    void *vp <br />   //璇村畠鐗瑰埆鏄洜涓哄畠娌℃湁綾誨瀷 <br />   //鎴栬呰榪欎釜綾誨瀷涓嶈兘鍒ゆ柇鍑烘寚鍚戝璞$殑闀垮害 <br /> <br /> 2.浠諱綍鎸囬拡閮藉彲浠ヨ祴鍊肩粰void鎸囬拡 <br />   type *p; <br />   vp=p; <br />   //涓嶉渶杞崲 <br />   //鍙幏寰楀彉閲?瀵硅薄鍦板潃鑰屼笉鑾峰緱澶у皬 <br /> <br /> 3.void鎸囬拡璧嬪肩粰鍏朵粬綾誨瀷鐨勬寚閽堟椂閮借榪涜杞崲 <br />    type *p=(type*)vp; <br />    //杞崲綾誨瀷涔熷氨鏄幏寰楁寚鍚戝彉閲?瀵硅薄澶у皬 <br /> 杞?http://icoding.spaces.live.com/blog/cns!209684E38D520BA6!130.entry <br /> <br /> 4.void鎸囬拡涓嶈兘澶嶅紩鐢?<br />   *vp//閿欒 <br />   鍥犱負(fù)void鎸囬拡鍙煡閬?鎸囧悜鍙橀噺/瀵硅薄鐨勮搗濮嬪湴鍧 <br />   鑰屼笉鐭ラ亾鎸囧悜鍙橀噺/瀵硅薄鐨勫ぇ灝?鍗犲嚑涓瓧鑺?鎵浠ユ棤娉曟紜紩鐢?<br /> <br /> 5.void鎸囬拡涓嶈兘鍙備笌鎸囬拡榪愮畻,闄ら潪榪涜杞崲 <br />    (type*)vp++; <br />   //vp==vp+sizeof(type)<br /> <br /> <br /> <br /> #include<iostream><br /> #include<stdlib.h><br /> #include<string><br /> using namespace std;<br /> typedef struct tag_st <br /> { <br /> char id[10];<br /> float fa[2];<br /> }ST; <br /> //鎴戝湪紼嬪簭閲岄潰榪欐牱浣跨敤鐨?<br /> int main()<br /> {<br /> ST * P=(ST *)malloc(sizeof(ST));<br /> strcpy(P->id,"hello!");<br /> P->fa[0]=1.1;<br /> P->fa[1]=2.1;<br /> <br /> ST * Q=(ST *)malloc(sizeof(ST));<br /> strcpy(Q->id,"world!");<br /> Q->fa[0]=3.1;<br /> Q->fa[1]=4.1;<br /> void ** plink=(void **)P;<br /> *((ST *)(plink)) = * Q; //<font style="line-height: 1.3em;" color="#ff0000">plink瑕佸厛寮哄埗杞崲涓涓?鐩殑鏄負(fù)浜嗚瀹冨厛鐭ラ亾瑕佽鐩栫殑澶у皬.</font><wbr><br />                          //<font style="line-height: 1.3em;" color="#ff0000">P鐨勫唴瀹圭珶鐒剁粰Q鐨勫唴瀹硅鐩栨帀浜?</font><wbr><br /> cout<<P->id<<" "<<P->fa[0]<<" "<<P->fa[1]<<endl;<br /> return 0;<br /> }<br /> <br /> <br /> <br /> 杞嚜錛歨ttp://www.cppblog.com/dragon/archive/2008/09/02/60760.aspx<br /> </div> <img src ="http://m.tkk7.com/shiliqiang/aggbug/290353.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://m.tkk7.com/shiliqiang/" target="_blank">鐭沖ご@</a> 2009-08-08 17:09 <a href="http://m.tkk7.com/shiliqiang/articles/290353.html#Feedback" target="_blank" style="text-decoration:none;">鍙戣〃璇勮</a></div>]]></description></item><item><title>鎸囬拡璇﹁Вhttp://m.tkk7.com/shiliqiang/articles/285866.html鐭沖ご@鐭沖ご@Tue, 07 Jul 2009 13:21:00 GMThttp://m.tkk7.com/shiliqiang/articles/285866.htmlhttp://m.tkk7.com/shiliqiang/comments/285866.htmlhttp://m.tkk7.com/shiliqiang/articles/285866.html#Feedback0http://m.tkk7.com/shiliqiang/comments/commentRss/285866.htmlhttp://m.tkk7.com/shiliqiang/services/trackbacks/285866.html
The memory of your computer can be imagined as a succession of memory cells, each one of the minimal size that computers manage (one byte). These single-byte memory cells are numbered in a consecutive way, so as, within any block of memory, every cell has the same number as the previous one plus one.

This way, each cell can be easily located in the memory because it has a unique address and all the memory cells follow a successive pattern. For example, if we are looking for cell 1776 we know that it is going to be right between cells 1775 and 1777, exactly one thousand cells after 776 and exactly one thousand cells before cell 2776.

Reference operator (&)

As soon as we declare a variable, the amount of memory needed is assigned for it at a specific location in memory (its memory address). We generally do not actively decide the exact location of the variable within the panel of cells that we have imagined the memory to be - Fortunately, that is a task automatically performed by the operating system during runtime. However, in some cases we may be interested in knowing the address where our variable is being stored during runtime in order to operate with relative positions to it.

The address that locates a variable within memory is what we call a reference to that variable. This reference to a variable can be obtained by preceding the identifier of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". For example:

ted = &andy;


This would assign to ted the address of variable andy, since when preceding the name of the variable andy with the reference operator (&) we are no longer talking about the content of the variable itself, but about its reference (i.e., its address in memory).

From now on we are going to assume that andy is placed during runtime in the memory address 1776. This number (1776) is just an arbitrary assumption we are inventing right now in order to help clarify some concepts in this tutorial, but in reality, we cannot know before runtime the real value the address of a variable will have in memory.

Consider the following code fragment:

andy = 25;
fred = andy;
ted = &andy;


The values contained in each variable after the execution of this, are shown in the following diagram:



First, we have assigned the value 25 to andy (a variable whose address in memory we have assumed to be 1776).

The second statement copied to fred the content of variable andy (which is 25). This is a standard assignment operation, as we have done so many times before.

Finally, the third statement copies to ted not the value contained in andy but a reference to it (i.e., its address, which we have assumed to be 1776). The reason is that in this third assignment operation we have preceded the identifier andy with the reference operator (&), so we were no longer referring to the value of andy but to its reference (its address in memory).

The variable that stores the reference to another variable (like ted in the previous example) is what we call a pointer. Pointers are a very powerful feature of the C++ language that has many uses in advanced programming. Farther ahead, we will see how this type of variable is used and declared.

Dereference operator (*)



We have just seen that a variable which stores a reference to another variable is called a pointer. Pointers are said to "point to" the variable whose reference they store.

Using a pointer we can directly access the value stored in the variable which it points to. To do this, we simply have to precede the pointer's identifier with an asterisk (*), which acts as dereference operator and that can be literally translated to "value pointed by".

Therefore, following with the values of the previous example, if we write:

beth = *ted;


(that we could read as: "beth equal to value pointed by ted") beth would take the value 25, since ted is 1776, and the value pointed by 1776 is 25.



You must clearly differentiate that the expression ted refers to the value 1776, while *ted (with an asterisk * preceding the identifier) refers to the value stored at address 1776, which in this case is 25. Notice the difference of including or not including the dereference operator (I have included an explanatory commentary of how each of these two expressions could be read):

beth = ted;   // beth equal to ted ( 1776 )
beth = *ted; // beth equal to value pointed by ted ( 25 )


Notice the difference between the reference and dereference operators:
  • & is the reference operator and can be read as "address of"
  • * is the dereference operator and can be read as "value pointed by"
Thus, they have complementary (or opposite) meanings. A variable referenced with & can be dereferenced with *.

Earlier we performed the following two assignment operations:

andy = 25;
ted = &andy;


Right after these two statements, all of the following expressions would give true as result:

andy == 25
&andy == 1776
ted == 1776
*ted == 25


The first expression is quite clear considering that the assignment operation performed on andy was andy=25. The second one uses the reference operator (&), which returns the address of variable andy, which we assumed it to have a value of 1776. The third one is somewhat obvious since the second expression was true and the assignment operation performed on ted was ted=&andy. The fourth expression uses the dereference operator (*) that, as we have just seen, can be read as "value pointed by", and the value pointed by ted is indeed 25.

So, after all that, you may also infer that for as long as the address pointed by ted remains unchanged the following expression will also be true:

*ted == andy


Declaring variables of pointer types

Due to the ability of a pointer to directly refer to the value that it points to, it becomes necessary to specify in its declaration which data type a pointer is going to point to. It is not the same thing to point to a char as to point to an int or a float.

The declaration of pointers follows this format:

type * name;


where type is the data type of the value that the pointer is intended to point to. This type is not the type of the pointer itself! but the type of the data the pointer points to. For example:

int * number;
char * character;
float * greatnumber;


These are three declarations of pointers. Each one is intended to point to a different data type, but in fact all of them are pointers and all of them will occupy the same amount of space in memory (the size in memory of a pointer depends on the platform where the code is going to run). Nevertheless, the data to which they point to do not occupy the same amount of space nor are of the same type: the first one points to an int, the second one to a char and the last one to a float. Therefore, although these three example variables are all of them pointers which occupy the same size in memory, they are said to have different types: int*, char* and float* respectively, depending on the type they point to.

I want to emphasize that the asterisk sign (*) that we use when declaring a pointer only means that it is a pointer (it is part of its type compound specifier), and should not be confused with the dereference operator that we have seen a bit earlier, but which is also written with an asterisk (*). They are simply two different things represented with the same sign.

Now have a look at this code:

// my first pointer
#include <iostream>
using namespace std;

int main ()
{
int firstvalue, secondvalue;
int * mypointer;

mypointer = &firstvalue;
*mypointer = 10;
mypointer = &secondvalue;
*mypointer = 20;
cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
firstvalue is 10
secondvalue is 20


Notice that even though we have never directly set a value to either firstvalue or secondvalue, both end up with a value set indirectly through the use of mypointer. This is the procedure:

First, we have assigned as value of mypointer a reference to firstvalue using the reference operator (&). And then we have assigned the value 10 to the memory location pointed by mypointer, that because at this moment is pointing to the memory location of firstvalue, this in fact modifies the value of firstvalue.

In order to demonstrate that a pointer may take several different values during the same program I have repeated the process with secondvalue and that same pointer, mypointer.

Here is an example a little bit more elaborated:

// more pointers
#include <iostream>
using namespace std;

int main ()
{
int firstvalue = 5, secondvalue = 15;
int * p1, * p2;

p1 = &firstvalue; // p1 = address of firstvalue
p2 = &secondvalue; // p2 = address of secondvalue
*p1 = 10; // value pointed by p1 = 10
*p2 = *p1; // value pointed by p2 = value pointed by p1
p1 = p2; // p1 = p2 (value of pointer is copied)
*p1 = 20; // value pointed by p1 = 20

cout << "firstvalue is " << firstvalue << endl;
cout << "secondvalue is " << secondvalue << endl;
return 0;
}
firstvalue is 10
secondvalue is 20


I have included as a comment on each line how the code can be read: ampersand (&) as "address of" and asterisk (*) as "value pointed by".

Notice that there are expressions with pointers p1 and p2, both with and without dereference operator (*). The meaning of an expression using the dereference operator (*) is very different from one that does not: When this operator precedes the pointer name, the expression refers to the value being pointed, while when a pointer name appears without this operator, it refers to the value of the pointer itself (i.e. the address of what the pointer is pointing to).

Another thing that may call your attention is the line:

int * p1, * p2;


This declares the two pointers used in the previous example. But notice that there is an asterisk (*) for each pointer, in order for both to have type int* (pointer to int).

Otherwise, the type for the second variable declared in that line would have been int (and not int*) because of precedence relationships. If we had written:

int * p1, p2;


p1 would indeed have int* type, but p2 would have type int (spaces do not matter at all for this purpose). This is due to operator precedence rules. But anyway, simply remembering that you have to put one asterisk per pointer is enough for most pointer users.

Pointers and arrays

The concept of array is very much bound to the one of pointer. In fact, the identifier of an array is equivalent to the address of its first element, as a pointer is equivalent to the address of the first element that it points to, so in fact they are the same concept. For example, supposing these two declarations:

int numbers [20];
int * p;


The following assignment operation would be valid:

p = numbers; 


After that, p and numbers would be equivalent and would have the same properties. The only difference is that we could change the value of pointer p by another one, whereas numbers will always point to the first of the 20 elements of type int with which it was defined. Therefore, unlike p, which is an ordinary pointer, numbers is an array, and an array can be considered a constant pointer. Therefore, the following allocation would not be valid:

numbers = p;


Because numbers is an array, so it operates as a constant pointer, and we cannot assign values to constants.

Due to the characteristics of variables, all expressions that include pointers in the following example are perfectly valid:

// more pointers
#include <iostream>
using namespace std;

int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p = &numbers[2]; *p = 30;
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
10, 20, 30, 40, 50, 


In the chapter about arrays we used brackets ([]) several times in order to specify the index of an element of the array to which we wanted to refer. Well, these bracket sign operators [] are also a dereference operator known as offset operator. They dereference the variable they follow just as * does, but they also add the number between brackets to the address being dereferenced. For example:

a[5] = 0;       // a [offset of 5] = 0
*(a+5) = 0; // pointed by (a+5) = 0


These two expressions are equivalent and valid both if a is a pointer or if a is an array.

Pointer initialization

When declaring pointers we may want to explicitly specify which variable we want them to point to:

int number;
int *tommy = &number;


The behavior of this code is equivalent to:

int number;
int *tommy;
tommy = &number;


When a pointer initialization takes place we are always assigning the reference value to where the pointer points (tommy), never the value being pointed (*tommy). You must consider that at the moment of declaring a pointer, the asterisk (*) indicates only that it is a pointer, it is not the dereference operator (although both use the same sign: *). Remember, they are two different functions of one sign. Thus, we must take care not to confuse the previous code with:

int number;
int *tommy;
*tommy = &number;


that is incorrect, and anyway would not have much sense in this case if you think about it.

As in the case of arrays, the compiler allows the special case that we want to initialize the content at which the pointer points with constants at the same moment the pointer is declared:

char * terry = "hello"; 


In this case, memory space is reserved to contain "hello" and then a pointer to the first character of this memory block is assigned to terry. If we imagine that "hello" is stored at the memory locations that start at addresses 1702, we can represent the previous declaration as:



It is important to indicate that terry contains the value 1702, and not 'h' nor "hello", although 1702 indeed is the address of both of these.

The pointer terry points to a sequence of characters and can be read as if it was an array (remember that an array is just like a constant pointer). For example, we can access the fifth element of the array with any of these two expression:

*(terry+4)
terry[4]


Both expressions have a value of 'o' (the fifth element of the array).

Pointer arithmetics



To conduct arithmetical operations on pointers is a little different than to conduct them on regular integer data types. To begin with, only addition and subtraction operations are allowed to be conducted with them, the others make no sense in the world of pointers. But both addition and subtraction have a different behavior with pointers according to the size of the data type to which they point.

When we saw the different fundamental data types, we saw that some occupy more or less space than others in the memory. For example, let's assume that in a given compiler for a specific machine, char takes 1 byte, short takes 2 bytes and long takes 4.

Suppose that we define three pointers in this compiler:

char *mychar;
short *myshort;
long *mylong;


and that we know that they point to memory locations 1000, 2000 and 3000 respectively.

So if we write:

mychar++;
myshort++;
mylong++;


mychar, as you may expect, would contain the value 1001. But not so obviously, myshort would contain the value 2002, and mylong would contain 3004, even though they have each been increased only once. The reason is that when adding one to a pointer we are making it to point to the following element of the same type with which it has been defined, and therefore the size in bytes of the type pointed is added to the pointer.



This is applicable both when adding and subtracting any number to a pointer. It would happen exactly the same if we write:

mychar = mychar + 1;
myshort = myshort + 1;
mylong = mylong + 1;


Both the increase (++) and decrease (--) operators have greater operator precedence than the dereference operator (*), but both have a special behavior when used as suffix (the expression is evaluated with the value it had before being increased). Therefore, the following expression may lead to confusion:

*p++


Because ++ has greater precedence than *, this expression is equivalent to *(p++). Therefore, what it does is to increase the value of p (so it now points to the next element), but because ++ is used as postfix the whole expression is evaluated as the value pointed by the original reference (the address the pointer pointed to before being increased).

Notice the difference with:

(*p)++

Here, the expression would have been evaluated as the value pointed by p increased by one. The value of p (the pointer itself) would not be modified (what is being modified is what it is being pointed to by this pointer).

If we write:

*p++ = *q++; 


Because ++ has a higher precedence than *, both p and q are increased, but because both increase operators (++) are used as postfix and not prefix, the value assigned to *p is *q before both p and q are increased. And then both are increased. It would be roughly equivalent to:

*p = *q;
++p;
++q;


Like always, I recommend you to use parentheses () in order to avoid unexpected results and to give more legibility to the code.

Pointers to pointers

C++ allows the use of pointers that point to pointers, that these, in its turn, point to data (or even to other pointers). In order to do that, we only need to add an asterisk (*) for each level of reference in their declarations:

char a;
char * b;
char ** c;
a = 'z';
b = &a;
c = &b;


This, supposing the randomly chosen memory locations for each variable of 7230, 8092 and 10502, could be represented as:



The value of each variable is written inside each cell; under the cells are their respective addresses in memory.

The new thing in this example is variable c, which can be used in three different levels of indirection, each one of them would correspond to a different value:

  • c has type char** and a value of 8092
  • *c has type char* and a value of 7230
  • **c has type char and a value of 'z'


void pointers

The void type of pointer is a special type of pointer. In C++, void represents the absence of type, so void pointers are pointers that point to a value that has no type (and thus also an undetermined length and undetermined dereference properties).

This allows void pointers to point to any data type, from an integer value or a float to a string of characters. But in exchange they have a great limitation: the data pointed by them cannot be directly dereferenced (which is logical, since we have no type to dereference to), and for that reason we will always have to cast the address in the void pointer to some other pointer type that points to a concrete data type before dereferencing it.

One of its uses may be to pass generic parameters to a function:

// increaser
#include <iostream>
using namespace std;

void increase (void* data, int psize)
{
if ( psize == sizeof(char) )
{ char* pchar; pchar=(char*)data; ++(*pchar); }
else if (psize == sizeof(int) )
{ int* pint; pint=(int*)data; ++(*pint); }
}

int main ()
{
char a = 'x';
int b = 1602;
increase (&a,sizeof(a));
increase (&b,sizeof(b));
cout << a << ", " << b << endl;
return 0;
}
y, 1603


sizeof is an operator integrated in the C++ language that returns the size in bytes of its parameter. For non-dynamic data types this value is a constant. Therefore, for example, sizeof(char) is 1, because char type is one byte long.

Null pointer

A null pointer is a regular pointer of any pointer type which has a special value that indicates that it is not pointing to any valid reference or memory address. This value is the result of type-casting the integer value zero to any pointer type.

int * p;
p = 0; // p has a null pointer value


Do not confuse null pointers with void pointers. A null pointer is a value that any pointer may take to represent that it is pointing to "nowhere", while a void pointer is a special type of pointer that can point to somewhere without a specific type. One refers to the value stored in the pointer itself and the other to the type of data it points to.

Pointers to functions

C++ allows operations with pointers to functions. The typical use of this is for passing a function as an argument to another function, since these cannot be passed dereferenced. In order to declare a pointer to a function we have to declare it like the prototype of the function except that the name of the function is enclosed between parentheses () and an asterisk (*) is inserted before the name:

// pointer to functions
#include <iostream>
using namespace std;

int addition (int a, int b)
{ return (a+b); }

int subtraction (int a, int b)
{ return (a-b); }

int operation (int x, int y, int (*functocall)(int,int))
{
int g;
g = (*functocall)(x,y);
return (g);
}

int main ()
{
int m,n;
int (*minus)(int,int) = subtraction;

m = operation (7, 5, addition);
n = operation (20, m, minus);
cout <<n;
return 0;
}
8


In the example, minus is a pointer to a function that has two parameters of type int. It is immediately assigned to point to the function subtraction, all in a single line:

int (* minus)(int,int) = subtraction;

 



鐭沖ご@ 2009-07-07 21:21 鍙戣〃璇勮
]]>
主站蜘蛛池模板: 亚洲熟妇av午夜无码不卡| 成人免费av一区二区三区| 国产性生交xxxxx免费| xxxxxx日本处大片免费看| 久久亚洲精品中文字幕| 波多野结衣免费视频观看| 成全视频在线观看免费| 在线精品亚洲一区二区| 国产亚洲精品AA片在线观看不加载| 中文字幕免费在线观看| 无人视频在线观看免费播放影院 | 久久久高清免费视频| 老牛精品亚洲成av人片| 久久久久亚洲Av无码专| 国产一区在线观看免费| 69av免费视频| 97国免费在线视频| 亚洲国产成人久久一区二区三区| 亚洲处破女AV日韩精品| 日本无吗免费一二区| jjizz全部免费看片| 成人网站免费大全日韩国产| 亚洲精品又粗又大又爽A片| 亚洲国产成人久久综合一 | 免费国产成人高清在线观看网站| 青青青视频免费观看| 亚洲人成伊人成综合网久久| 国产亚洲一区二区在线观看| 四虎影院永久免费观看| 成年在线观看网站免费| 日韩人妻无码精品久久免费一| 一区二区三区免费在线观看| 亚洲愉拍一区二区三区| 亚洲男人天堂影院| 久久亚洲AV午夜福利精品一区| 免费又黄又爽又猛的毛片| 午夜高清免费在线观看| 67194成是人免费无码| 18禁无遮挡无码国产免费网站| 久久精品免费大片国产大片| 国产亚洲福利一区二区免费看 |