April 20, 2005
"Regan Heath" <regan@netwin.co.nz> wrote in message news:opspg60tk423k2f5@nrage.netwin.co.nz...
> Are you sure? That seems to be a bad way to do it, given that "realloc" can and will extend the block of memory you currently have if there is space. How do you explain these results:

Arrays do in fact use realloc.  Proof:

[from array.c]

void Array::reserve(unsigned nentries)
{
    //printf("Array::reserve: size = %d, offset = %d, nbytes = %d\n", size,
offset, nbytes);
    if (allocdim - dim < nentries)
    {
        allocdim = dim + nentries;
        data = (void **)mem.realloc(data, allocdim * sizeof(*data));
    }
}
void Array::setDim(unsigned newdim)
{
    if (dim < newdim)
    {
        reserve(newdim - dim);
    }
    dim = newdim;
}

setDim is called when an array's length is set.  It calls reserve, which in turn calls mem.realloc.

[from mem.c]

void *Mem::realloc(void *p, size_t size)
{
    if (!size)
    {
        if (p)
        {
            ::free(p);
            p = NULL;
        }
    }
    else if(!p)
    {
        p = ::malloc(size);
        if (!p)
            error();
    }
    else
    {
        p = ::realloc(p, size);
        if (!p)
            error();
    }
    return p;
}

And as you can see, mem.realloc() calls C's realloc(), explaining the behavior of your code.


April 20, 2005
Regan Heath wrote:
> On Tue, 19 Apr 2005 14:15:08 +0300, Georg Wrede <georg.wrede@nospam.org>  wrote:
> 
>> Setting it later to something larger will cause a reallocation only if  there is not room on the heap to grow in-place to the required size.
> 
> 
> That is what I was trying to proove.
> 
> Regan

That's what I wanted to know (and had assumed is what it did).
So I shall probably go ahead with my original plan of doing my array growing by hand and putting old arrays into temporary storage until there is some sort of downtime in the game (or until memory is above my threshold)

-- 
-PIB

--
"C++ also supports the notion of *friends*: cooperative classes that
are permitted to see each other's private parts." - Grady Booch
May 16, 2005
> No, whenever length increases (also when concatenating and such), the array is duplicated, and the duplicate is given new length. The old array stays floating for GC to collect - or, perhaps, for other parts of software to use, if they need not be aware of what was happening.

I assume there is no protection against pointers to the original values?

                                          Brian
                                 ( bcwhite@precidia.com )

-------------------------------------------------------------------------------
             The best way to predict the future... is to create it.
1 2
Next ›   Last »