作者ric2k1 (Ric)
看板EE_DSnP
标题Re: [问题] 关於end()
时间Mon Dec 25 22:29:59 2006
※ 引述《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