[問題] 要抓出存放Array Size的記憶體位置
看板C_and_CPP作者jerrypotato (我是湯姆貓 不是傑利薯)時間14年前 (2009/10/05 21:24)推噓3(3推 0噓 11→)留言14則, 4人參與討論串1/3 (看更多)
現在課堂上正在複習C++語法
順便介紹變數宣告時 電腦是如何實際的指派記憶體位置給那些被宣告的變數
其中有一題是這樣的:(只列出關鍵部份)
class A
{
int data;
}
int main()
{
A *arr = new A[17];
//Some Commands......
return 0;
}
根據教授的說法,"17"這一個數字會實際存在於記憶體中,
而且其位置位於arr之前的幾個byte,
要我們定位出"17"這個數字,所佔據的記憶體位址。
我試過好幾個方法,可是最後都是失敗的,
最後還用到超級暴力法:
int *intptr = (int *) &arr //強制取出arr的記憶體位址
for (int k = 0; k < 50; k++ )
cout << *(intptr - k) << endl //把arr位址之前的記憶體全部掃瞄過一遍
結果還是不能掃到17這個數字 囧
實在沒辦法,只好PO上版來請教各位強者,
請問我要如何定位出"17"這個數字所佔據的記憶體位址呢?
謝謝<(_ _)>
--
BED ▎●▅ ▅ ▎●▁▁ ▎●▅▅▅ ▎●▅▅ ┌ ▎ ┐
▇▇▇ ▇ ▇▇▇▇ ▇▇▇▇ ●▅▅ ▇▇▇▇ │ ▇▇▇▇ │
& ▎ ▎ ▎ ▎ ▎ ▎ ▎●▅▅ │ ▎ ▎│
▇▇▇▇ │ │
I magician super model basketball homeless prisoner └ engineer ┘
player
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.112.243.205
→
10/05 21:31, , 1F
10/05 21:31, 1F
→
10/05 21:32, , 2F
10/05 21:32, 2F
→
10/05 21:33, , 3F
10/05 21:33, 3F
→
10/05 21:33, , 4F
10/05 21:33, 4F
→
10/05 21:34, , 5F
10/05 21:34, 5F
→
10/05 21:34, , 6F
10/05 21:34, 6F
→
10/05 21:36, , 7F
10/05 21:36, 7F
→
10/05 21:36, , 8F
10/05 21:36, 8F
推
10/05 21:40, , 9F
10/05 21:40, 9F
→
10/05 21:40, , 10F
10/05 21:40, 10F
→
10/05 21:41, , 11F
10/05 21:41, 11F
他的原文是 The array size "17" is recorded around "arr's" location in the heap
memory. (hint: some bytes before arr's address). please locate this piece of
memory and verify that the number in that memory is indeed "17".
因此我就把上面那段話想成要找出"17"的所在位置了 囧
※ 編輯: jerrypotato 來自: 140.112.243.205 (10/05 21:51)
推
10/05 22:06, , 12F
10/05 22:06, 12F
推
10/05 22:11, , 13F
10/05 22:11, 13F
→
10/06 16:33, , 14F
10/06 16:33, 14F
討論串 (同標題文章)
以下文章回應了本文:
完整討論串 (本文為第 1 之 3 篇):