Пространства имён
Варианты
Действия

realloc

Материал из cppreference.com
< c | memory

Заголовочный файл <stdlib.h>
void *realloc( void *ptr, size_t new_size );
Перераспределяет данной области памяти. Он должен быть предварительно выделенные malloc(), calloc() или realloc() и еще не освобожден с free(), в противном случае, результат будет неопределенным.
Original:
Reallocates the given area of memory. It must be previously allocated by malloc(), calloc() or realloc() and not yet freed with free(), otherwise, the results are undefined.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Перераспределение осуществляется путем:
Original:
The reallocation is done by either:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
a)
расширение существующей области, на которую указывает ptr, если это возможно. Содержание области остаются неизменными до меньшего из нового и старого размеров. Если зона расширилась, содержимое новой части массива не определен.
Original:
expanding the existing area pointed to by ptr, if possible. The contents of the area remain unchanged up to the lesser of the new and old sizes. If the area is expanded, the contents of the new part of the array are undefined.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
b)
выделении нового блока памяти в байтах размер new_size, копирование области памяти, размер которого равен меньшему из нового и старого размеров, и освобождение старого блока.
Original:
allocating a new memory block of size new_size bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Если не хватает памяти, старый блок памяти не освобождается и нуль-указатель возвращается.
Original:
If there is not enough memory, the old memory block is not freed and null-pointer is returned.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Содержание

[править] Параметры

ptr -
Указатель на область памяти, чтобы быть перераспределены
Original:
pointer to the memory area to be reallocated
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
new_size -
Новый размер массива
Original:
new size of the array
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[править] Возвращаемое значение

Указатель на начало новой выделенной памяти или NULL, если произошла ошибка. Указатель должен быть освобожден с free().
Original:
Pointer to the beginning of newly allocated memory or NULL if error has occurred. The pointer must be deallocated with free().
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[править] Пример

[править] См. также