kdelibs/kdecore

Stefan Heimers kde-optimize@mail.kde.org
Wed, 19 Mar 2003 23:07:43 +0100


Am Wednesday 19 March 2003 22:55 schrieb Tim Jansen:
> On Wednesday 19 March 2003 21:12, Eray Ozkural wrote:
> > -        Z *newdata = new Z[index + 1];
> > +        // open table has amortized O(1) access time
> > +        // when N elements appended -- exa
> > +        Z *newdata = new Z[max(2 * sz, index+1)];
>
> How can the rest of the code know that the array is bigger than
> index+1 if you don't modify it as well?

He might have forgotten to set sz to the new size, or just didn't post 
everything. Does the following make more sense? (IMHO still a waste of 
memory)

else if (index >= sz) {
...
+        Z *newdata = new Z[max(sz = 2 * sz, index+1)];


sz might not be unique, so you need to put it in the data structure, eg. 
newdata->sz.