作者TRium (要常常換暱稱才不煩)
看板EE_DSnP
標題[請益] 印出array前存的數字
時間Sat Dec 16 18:12:50 2006
#include <iostream>
using namespace std;
int main ()
{
int* a=new int [2];
a[0]=1;
a[1]=2;
cout << *(int*)( size_t(a)-4) <<endl;
return 0;
}
我這樣寫試過(int*)( size_t(a)-4)可以得到減4byte的位置
可是加*後卻得不到2,這是為什麼啊?
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.135.13.3
推 ric2k1:Uh... predefined type like "int" has different rules... 12/16 18:16
推 ric2k1:Only your own clas with "destructor" will work. 12/16 18:17
推 ric2k1:I haven't figure out either... 12/16 18:18
推 TRium:那是要用自己定的class才能得到我們想要的結果嗎? 12/16 18:20
推 ric2k1:是啊, 如果有人找到是為什麼, 請跟我分享... 12/16 22:47