Realloc in C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

realloc - C++ Reference http://www.cplusplus.

com/reference/cstdlib/realloc/

Search: Go
Not logged in

Reference <cstdlib> realloc register log in

C++
Information You are using a version without Ads of this website. Please, consider donating:
Tutorials
Reference
Articles [hide]
Forum
function
Reference
C library: realloc <cstdlib>

<cassert> (assert.h) void* realloc (void* ptr, size_t size);


<cctype> (ctype.h)
<cerrno> (errno.h) Reallocate memory block
<cfenv> (fenv.h) Changes the size of the memory block pointed to by ptr.
<cfloat> (float.h)
<cinttypes> (inttypes.h) The function may move the memory block to a new location (whose address is returned by the function).
<ciso646> (iso646.h)
<climits> (limits.h) 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
<clocale> (locale.h) new location. If the new size is larger, the value of the newly allocated portion is indeterminate.
<cmath> (math.h)
<csetjmp> (setjmp.h) In case that ptr is a null pointer, the function behaves like malloc, assigning a new block of size bytes and returning a
<csignal> (signal.h) pointer to its beginning.
<cstdarg> (stdarg.h)
<cstdbool> (stdbool.h) C90 (C++98) C99/C11 (C++11)
<cstddef> (stddef.h)
<cstdint> (stdint.h) Otherwise, if size is zero, the memory previously allocated at ptr is deallocated as if a call to free was made, and a
<cstdio> (stdio.h) null pointer is returned.
<cstdlib> (stdlib.h)
<cstring> (string.h) If the function fails to allocate the requested block of memory, a null pointer is returned, and the memory block pointed
<ctgmath> (tgmath.h) to by argument ptr is not deallocated (it is still valid, and with its contents unchanged).
<ctime> (time.h)
<cuchar> (uchar.h)
<cwchar> (wchar.h) Parameters
<cwctype> (wctype.h)
Containers: ptr
Input/Output: Pointer to a memory block previously allocated with malloc, calloc or realloc.
Multi-threading: Alternatively, this can be a null pointer, in which case a new block is allocated (as if malloc was called).
Other:
size
<cstdlib> (stdlib.h) New size for the memory block, in bytes.
functions: size_t is an unsigned integral type.
abort
abs
atexit
atof
Return Value
atoi A pointer to the reallocated memory block, which may be either the same as ptr or a new location.
atol The type of this pointer is void*, which can be cast to the desired type of data pointer in order to be dereferenceable.
atoll
C90 (C++98) C99/C11 (C++11)
at_quick_exit
bsearch A null-pointer indicates either that size was zero (an thus ptr was deallocated), or that the function did not allocate
calloc storage (and thus the block pointed by ptr was not modified).
div
exit
free Example
getenv
labs 1 /* realloc example: rememb-o-matic */
ldiv 2 #include <stdio.h> /* printf, scanf, puts */
llabs 3 #include <stdlib.h> /* realloc, free, exit, NULL */
lldiv 4
malloc 5 int main ()
mblen 6 {
mbstowcs 7 int input,n;
mbtowc 8 int count = 0;
qsort 9 int* numbers = NULL;
quick_exit 10 int* more_numbers = NULL;
rand 11
realloc 12 do {
srand 13 printf ("Enter an integer value (0 to end): ");
strtod 14 scanf ("%d", &input);
strtof 15 count++;
strtol 16
strtold 17 more_numbers = (int*) realloc (numbers, count * sizeof(int));
strtoll 18
strtoul 19 if (more_numbers!=NULL) {
strtoull 20 numbers=more_numbers;
system 21 numbers[count-1]=input;
wcstombs 22 }
23

1 of 2 Friday 07 November 2014 06:12 PM


realloc - C++ Reference http://www.cplusplus.com/reference/cstdlib/realloc/

wctomb 24 else {
functions (non-standard): 25 free (numbers);
itoa 26 puts ("Error (re)allocating memory");
types: 27 exit (1);
div_t 28 }
ldiv_t 29 } while (input!=0);
lldiv_t 30
size_t 31 printf ("Numbers entered: ");
macro constants: 32 for (n=0;n<count;n++) printf ("%d ",numbers[n]);
EXIT_FAILURE 33 free (numbers);
EXIT_SUCCESS 34
MB_CUR_MAX 35 return 0;
NULL }
RAND_MAX

Lenskart - 1st Frame Free The program prompts the user for numbers until a zero character is entered. Each time a new value is introduced the
Get thousands of Stylish
memory block pointed by numbers is increased by the size of an int.
Glasses. 14-Day Money Back.

Data races
Only the storage referenced by ptr and by the returned pointer are modified. No other storage locations are accessed
by the call.
If the function releases or reuses a unit of storage that is reused or released by another allocation or deallocation
function, the functions are synchronized in such a way that the deallocation happens entirely before the next allocation.

Exceptions (C++)
No-throw guarantee: this function never throws exceptions.

See also
free Deallocate memory block (function )
calloc Allocate and zero-initialize array (function )
malloc Allocate memory block (function )

Home page | Privacy policy


© cplusplus.com, 2000-2014 - All rights reserved - v3.1
Spotted an error? contact us

2 of 2 Friday 07 November 2014 06:12 PM

You might also like