summaryrefslogtreecommitdiff
path: root/devdocs/c/memory%2Frealloc.html
blob: 65d5948de66e11ff2f77a16e220911db7db840c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    <h1 id="firstHeading" class="firstHeading">realloc</h1>            <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code>&lt;stdlib.h&gt;</code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl"> <td class="t-dcl-nopad"> <pre data-language="c">void *realloc( void *ptr, size_t new_size );</pre>
</td> <td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr>  </table> <p>Reallocates the given area of memory. If <code>ptr</code> is not NULL, it must be previously allocated by <code><a href="malloc" title="c/memory/malloc">malloc</a></code>, <code><a href="calloc" title="c/memory/calloc">calloc</a></code> or <code>realloc</code> and not yet freed with a call to <code><a href="free" title="c/memory/free">free</a></code> or <code>realloc</code>. Otherwise, the results are undefined.</p>
<p>The reallocation is done by either:</p>
<div class="t-li1">
<span class="t-li">a)</span> expanding or contracting the existing area pointed to by <code>ptr</code>, 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.</div> <div class="t-li1">
<span class="t-li">b)</span> allocating a new memory block of size <code>new_size</code> bytes, copying memory area with size equal the lesser of the new and the old sizes, and freeing the old block.</div> <p>If there is not enough memory, the old memory block is not freed and null pointer is returned.</p>
<p>If <code>ptr</code> is <code><a href="../types/null" title="c/types/NULL">NULL</a></code>, the behavior is the same as calling <code><a href="http://en.cppreference.com/w/c/memory/malloc"><span class="kw403">malloc</span></a><span class="br0">(</span>new_size<span class="br0">)</span></code>.</p>
<p>Otherwise,</p>
<table class="t-rev-begin"> <tr class="t-rev t-until-c23">
<td> <p>if <code>new_size</code> is zero, the behavior is implementation defined (null pointer may be returned (in which case the old memory block may or may not be freed), or some non-null pointer may be returned that may not be used to access storage). <span class="t-rev-inl t-since-c17"><span>Such usage is deprecated (via <a rel="nofollow" class="external text" href="https://open-std.org/JTC1/SC22/WG14/www/docs/n2396.htm#dr_400">C DR 400</a>).</span><span><span class="t-mark-rev t-until-c17">(since C17)</span></span></span></p>
</td> <td><span class="t-mark-rev t-until-c23">(until C23)</span></td>
</tr> <tr class="t-rev t-since-c23">
<td> <p>if <code>new_size</code> is zero, the behavior is undefined.</p>
</td> <td><span class="t-mark-rev t-since-c23">(since C23)</span></td>
</tr> </table> <table class="t-rev-begin"> <tr class="t-rev t-since-c11">
<td> <p><code>realloc</code> is thread-safe: it behaves as though only accessing the memory locations visible through its argument, and not any static storage.</p>
<p>A previous call to <code><a href="free" title="c/memory/free">free</a></code> or <code>realloc</code> that deallocates a region of memory <i>synchronizes-with</i> a call to any allocation function, including <code>realloc</code> that allocates the same or a part of the same region of memory. This synchronization occurs after any access to the memory by the deallocating function and before any access to the memory by <code>realloc</code>. There is a single total order of all allocation and deallocation functions operating on each particular region of memory.</p>
</td> <td><span class="t-mark-rev t-since-c11">(since C11)</span></td>
</tr> </table>  <h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> ptr </td> <td> - </td> <td> pointer to the memory area to be reallocated </td>
</tr> <tr class="t-par"> <td> new_size </td> <td> - </td> <td> new size of the array in bytes </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with <code><a href="free" title="c/memory/free">free</a></code> or <code>realloc</code>. The original pointer <code>ptr</code> is invalidated and any access to it is undefined behavior (even if reallocation was in-place).</p>
<p>On failure, returns a null pointer. The original pointer <code>ptr</code> remains valid and may need to be deallocated with <code><a href="free" title="c/memory/free">free</a></code> or <code>realloc</code>.</p>
<h3 id="Notes"> Notes</h3> <p>Originally (in C89), support for zero size was added to accommodate code such as</p>
<div class="c source-c"><pre data-language="c">OBJ *p = calloc(0, sizeof(OBJ)); // "zero-length" placeholder
/*...*/
while (1)
{
    p = realloc(p, c * sizeof(OBJ)); // reallocations until size settles
    /* code that may change c or break out of loop */
}</pre></div> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include &lt;assert.h&gt;
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;string.h&gt;
 
void print_storage_info(const int* next, const int* prev, int ints)
{
    if (next)
        printf("%s location: %p. Size: %d ints (%ld bytes).\n",
               (next != prev ? "New" : "Old"), (void*)next, ints, ints * sizeof(int));
    else
        printf("Allocation failed.\n");
}
 
int main(void)
{
    const int pattern[] = {1, 2, 3, 4, 5, 6, 7, 8};
    const int pattern_size = sizeof pattern / sizeof(int);
    int *next = NULL, *prev = NULL;
 
    if ((next = (int*)malloc(pattern_size * sizeof *next))) // allocates an array
    {
        memcpy(next, pattern, sizeof pattern); // fills the array
        print_storage_info(next, prev, pattern_size);
    }
    else
        return EXIT_FAILURE;
 
    // Reallocate in cycle using the following values as a new storage size.
    const int realloc_size[] = {10, 12, 512, 32768, 65536, 32768};
 
    for (int i = 0; i != sizeof realloc_size / sizeof(int); ++i)
    {
        if ((next = (int*)realloc(prev = next, realloc_size[i] * sizeof(int))))
        {
            print_storage_info(next, prev, realloc_size[i]);
            assert(!memcmp(next, pattern, sizeof pattern));  // is pattern held
        }
        else // if realloc failed, the original pointer needs to be freed
        {
            free(prev);
            return EXIT_FAILURE;
        }
    }
 
    free(next); // finally, frees the storage
    return EXIT_SUCCESS;
}</pre></div> <p>Possible output:</p>
<div class="text source-text"><pre data-language="c">New location: 0x144c010. Size: 8 ints (32 bytes).
Old location: 0x144c010. Size: 10 ints (40 bytes).
New location: 0x144c450. Size: 12 ints (48 bytes).
Old location: 0x144c450. Size: 512 ints (2048 bytes).
Old location: 0x144c450. Size: 32768 ints (131072 bytes).
New location: 0x7f490c5bd010. Size: 65536 ints (262144 bytes).
Old location: 0x7f490c5bd010. Size: 32768 ints (131072 bytes).</pre></div> </div> <h3 id="References"> References</h3>  <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 7.22.3.5 The realloc function (p: TBD) </li></ul>
<li> C17 standard (ISO/IEC 9899:2018): </li>
<ul><li> 7.22.3.5 The realloc function (p: 254) </li></ul>
<li> C11 standard (ISO/IEC 9899:2011): </li>
<ul><li> 7.22.3.5 The realloc function (p: 349) </li></ul>
<li> C99 standard (ISO/IEC 9899:1999): </li>
<ul><li> 7.20.3.4 The realloc function (p: 314) </li></ul>
<li> C89/C90 standard (ISO/IEC 9899:1990): </li>
<ul><li> 4.10.3.4 The realloc function </li></ul>
</ul>                   <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td colspan="2"> <span><a href="https://en.cppreference.com/w/cpp/memory/c/realloc" title="cpp/memory/c/realloc">C++ documentation</a></span> for <code>realloc</code> </td>
</tr> </table>           <div class="_attribution">
  <p class="_attribution-p">
    &copy; cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
    <a href="https://en.cppreference.com/w/c/memory/realloc" class="_attribution-link">https://en.cppreference.com/w/c/memory/realloc</a>
  </p>
</div>