※ 引述《samuelduan ()》之銘言:
: end() 在 array 中是pass the end
: 也就是指向 "the next address of the last element"
: 那我在寫 array.h 的時候
: 想說既然 end() 是如上的定義
: 那我就在 new array 的時候多加一個空位給它
: 例如會出現這樣的寫法 T* newArray = new T [_capacity * 2 + 1]
: 後來會發現這樣做有不對之處
: 是當我發現每個測資都能如所下的指令般 正確的運作
: 但是奇怪的是 存進去的值只有第一個一樣
: 其他都都不一樣
: 那我在 push_front() 中 讓它印出 d 的值
: 發現傳進來 d 的值和我存進去的值完全一樣
: 後來將 +1 拿掉之後又一切恢復正常了
: 所以我想這是不是跟作業一有點像
: 因為某種原因 連 d 的值也被 contaminate了 =.=
"new T[_capacity * 2 + 1]" will call "2 * _capacity + 1" times of
constructor. In other words, it will call this many times of LTestObj
constructor. Since everytime when the constructor is called, it will call
the random number generator to create the number in the pseudo-random number
sequence, you will see the different results from the reference program, where
it calls only "2 * _capacity" times of constructor.
Therefore, please try to allocate "2 * _capacity" memory, instead of
"2 * _capacity + 1".
Hint: it is OK for the "_end" pointer to points to the memory outside
the range. As long as you don't do "*_end". And please take care of the
initial condition where _end = 0.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 59.121.134.73