Using realloc in c

I am using void *realloc(void *pointer, size_t size);

to increase the size of my pointer. how does realloc work?
does this create an address space and copy the old value to the new address space and return a pointer to that address? or does it just allocate more memory and bind it to the old one?

+2


source to share


5 answers


@Iraklis has the correct answer: he does the second (if he can get away with it) or the first (but only if he should).

However, sometimes this cannot be done and it will fail. Be careful: if it cannot resize your data, it will return NULL

, but memory will NOT be free

d. Code like this is wrong:

ptr = realloc(ptr, size);

      

If it realloc

returns NULL

, the old one ptr

will never get free

d, because you overwrote it with NULL

. To do this correctly, you must:

void *tmp = realloc(ptr, size);
if(tmp) ptr = tmp;
else /* handle error, often with: */ free(ptr);

      



On BSD systems, the above turns into a named library function reallocf

that can be implemented like this:

void *reallocf(void *p, size_t s)
{
    void *tmp = realloc(p, s);
    if(tmp) return tmp;
    free(p);
    return NULL;
}

      

Letting you use it safely:

ptr = reallocf(ptr, size);

      

Note that if realloc

new space must be allocated and old data copied, it will be free

old data. Only if it cannot resize does it leave your data intact, in case the resize error is a recoverable error.

+5


source


It depends! If it cannot resize the memory area in place, it allocates a new memeory area, copies the old data, and frees the old memory.



+3


source


You are using the term "address space" incorrectly. All of your process's memory exists in one address space. The memory not used by your program, its globals and its stack, is called the "heap". malloc

and realloc

(and calloc

, which is simple malloc

and clear) allocate memory from the heap. Most implementations realloc

will check if there is enough free space ( size

bytes) starting at pointer

(which should point to a previously allocated block malloc

or realloc

- realloc

) this block), and if so, just increase the size of the block allocated at the location indicated pointer

and return without copying. If there isn't enough space, it will do the equivalent newptr = malloc(size); memcpy(newptr, pointer, size_of_old_block); free(pointer); return newptr;

... that is, it will allocate a block large enough to holdsize

bytes, copy the data into pointer

that block, free the old block, and return the address of the new block.

+2


source


I think the answer depends on the requested size and the available heap.

From a programmer's point of view, I think all we guarantee is that pointer

it is not null if the new allocation is successful. Therefore, the pointer can remain unchanged, although it now points to a larger block of memory represented by size_t

.

0


source


Realloc does not change the size of your pointer, the size of pointers is always the same in the same architecture. It changes the size of the allocated memory that your pointer is pointing to. The way it works is described here: http://msdn.microsoft.com/en-us/library/xbebcx7d.aspx . In short, yes, it allocates more memory while keeping your content intact; if memory needs to be moved, it copies the contents. You can of course specify a shorter size, in which case it truncates the allocated memory, again leaving the content intact.

0


source







All Articles