Why realloc? (in __default_alloc_template, file: stl_alloc.h) (GCC ver 3.3.3)

F

Frank Chow

In file: stl_alloc.h

First, we can find the definition of "__new_alloc" which manipulate
memory by "new" and "delete":

class __new_alloc
{
public:
static void*
allocate(size_t __n)
{ return ::eek:perator new(__n); }

static void
deallocate(void* __p, size_t)
{ ::eek:perator delete(__p); }
};

Then, in "__default_alloc_template", we can find its implementation of
"allocate", "deallocate" and "reallocate":

// in class "__default_alloc_template"
static void* allocate(size_t __n)
{
...
if ((__n > (size_t) _MAX_BYTES) || (_S_force_new > 0))
__ret = __new_alloc::allocate(__n); // uses "new"
...
}

static void deallocate(void* __p, size_t __n)
{
if ((__n > (size_t) _MAX_BYTES) || (_S_force_new > 0))
__new_alloc::deallocate(__p, __n); // uses "delete"
...
}

static void* reallocate(void* __p, size_t __old_sz, size_t __new_sz)
{
...
if (__old_sz > (size_t) _MAX_BYTES && __new_sz > (size_t)
_MAX_BYTES)
return(realloc(__p, __new_sz)); // uses "realloc"
...
}

When there's a memory block that is bigger than _MAX_BYTES, both
"allocate" and "deallocate" use C++ new-based statement to handle it,
but only "reallocate" uses C function "realloc".

What's the reason for this inconsistency? And what if we pass a "__p"
allocated by "new" into the member function "reallocate"?

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top