Question

Hello I try to understand how realloc works so here is my question:

Let's say that first we call malloc in order to allocate enough memory for 1 int.

int *p=malloc(sizeof(int))

then we call realloc like this:

p=realloc(p,sizeof(int)*2);

The pointer p points to memory with available space for 2 or 1+2 ints?

Était-ce utile?

La solution 2

void* realloc (void* ptr, size_t size);

Changes the size of the memory block pointed to by ptr.

The function may move the memory block to a new location (whose address is returned by the function).

The content of the memory block is preserved up to the lesser of the new and old sizes, even if the block is moved to a new location. If the new size is larger, the value of the newly allocated portion is indeterminate.

In case that ptr is a null pointer, the function behaves like malloc, assigning a new block of size bytes and returning a pointer to its beginning.

In C90 (C++98):

Otherwise, if size is zero, the memory previously allocated at ptr is deallocated as if a call to free was made, and a null pointer is returned.

In C99/C11 (C++11):

If size is zero, the return value depends on the particular library implementation: it may either be a null pointer or some other location that shall not be dereferenced.

Argument ptr:

Pointer to a memory block previously allocated with malloc, calloc or realloc. Alternatively, this can be a null pointer, in which case a new block is allocated (as if malloc was called).

Argument size:

New size for the memory block, in bytes. size_t is an unsigned integral type.

Return Value:

A pointer to the reallocated memory block, which may be either the same as ptr or a new location. The type of this pointer is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.

In C90 (C++98):

A null-pointer indicates either that size was zero (an thus ptr was deallocated), or that the function did not allocate storage (and thus the block pointed by ptr was not modified).

In C99/C11 (C++11):

A null-pointer indicates that the function failed to allocate storage, and thus the block pointed by ptr was not modified.

Autres conseils

As mentioned in the man pages:

void *realloc(void *ptr, size_t size);

[...]

The realloc() function changes the size of the memory block pointed to by ptr to size bytes. The contents will be unchanged in the range from the start of the region up to the minimum of the old and new sizes. If the new size is larger than the old size, the added memory will not be initialized. [...]

(My emphasis). In other words, the size parameter to realloc asks for how many bytes of memory you'd like allocated in total, not the number of bytes of memory that you'd like to add.

Hope this helps!

From realloc(3)

Synopsis
void *realloc(void *ptr, size_t size);
Description
The realloc() function changes the size of the memory block pointed to by ptr to size bytes.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top