LINUX.ORG.RU

[C][опять про память]realloc

 


0

1

Я правильно понимаю, что при использовании realloc, если нужное количество памяти выделить невозможно, старый блок данных тоже теряется? Если да, то есть ли аналогичная функция, которая при ошибке оставляет его нетронутым?

Неправильно понимаешь, старый блок в таком случае остаётся как был.

const86 ★★★★★
()
RETURN VALUES
     ...

     The realloc() function returns a pointer, possibly identical to ptr, to
     the allocated memory if successful; otherwise a NULL pointer is returned,
     and errno is set to ENOMEM if the error was the result of an allocation
     failure.  The realloc() function always leaves the original buffer intact
     when an error occurs.
yoghurt ★★★★★
()

в то время как kermzyxer возится с замыканиями лиспа... ;)

yaws
()

ISO/IEC 9899:1999

7.20.3.4 The realloc function

Synopsis

1

#include <stdlib.h>
void *realloc(void *ptr, size_t size);

Description

2

The realloc function deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. The contents of the new object shall be the same as that of the old object prior to deallocation, up to the lesser of the new and old sizes. Any bytes in the new object beyond the size of the old object have indeterminate values.

3

If ptr is a null pointer, the realloc function behaves like the malloc function for the specified size. Otherwise, if ptr does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to the free or realloc function, the behavior is undefined. If memory for the new object cannot be allocated, the old object is not deallocated and its value is unchanged.

Returns

4

The realloc function returns a pointer to the new object (which may have the same value as a pointer to the old object), or a null pointer if the new object could not be allocated.

ты уже понял всю свою ничтожность, да?

arsi ★★★★★
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.