implementing stacks using dynamic arrays

sai sai_member at pathlink.com
Sun Apr 9 20:38:28 PDT 2006


But in the posted code, for every pop() operation, the length is set

> arr.length = n;

doesn't this do a realloc the memory every time it is called ?
if pop is done many times, this should definitly make it 
inefficient. 

does't it ?

Thanks
Sai


In article <e1c92h$o2s$1 at digitaldaemon.com>, Walter Bright says...
>
>Mike Capp wrote:
>> In article <e1b0dn$28vl$1 at digitaldaemon.com>, Boyko Bantchev says...
>>> int[] a;
>>> int t;
>>> ..
>>> a ~= t;
>>> which makes a beautiful generic push operation for stacks.
>> 
>> Maybe beautifully generic, but horrendously inefficient. It gives you linear
>> complexity for each push, which is really not what you're looking for in a Stack
>> implementation.
>
>No, it is rather efficient. It does't realloc/copy for each push, as it 
>uses a power of 2 algorithm to resize the array.





More information about the Digitalmars-d mailing list