blob: 36a5ad8f3641bba08c39ec192b3223d19dd95d79 (
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
|
<h1 id="firstHeading" class="firstHeading">free_sized</h1> <table class="t-dcl-begin"> <tr class="t-dsc-header"> <th> Defined in header <code><stdlib.h></code> </th> <th> </th> <th> </th> </tr> <tr class="t-dcl t-since-c23"> <td> <pre data-language="c">void free_sized( void* ptr, size_t size );</pre>
</td> <td class="t-dcl-nopad"> </td> <td> <span class="t-mark-rev t-since-c23">(since C23)</span> </td> </tr> </table> <p>Deallocates the space 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><a href="realloc" title="c/memory/realloc">realloc()</a></code> (but not <code>aligned_alloc()</code>).</p>
<h3 id="Parameters"> Parameters</h3> <table class="t-par-begin"> <tr class="t-par"> <td> ptr </td> <td> - </td> <td> pointer to the memory to deallocate </td>
</tr> <tr class="t-par"> <td> size </td> <td> - </td> <td> size of memory previously passed to an allocation function </td>
</tr>
</table> <h3 id="Return_value"> Return value</h3> <p>(none)</p>
<h3 id="Notes"> Notes</h3> <h3 id="Possible_implementation"> Possible implementation</h3> <div class="c source-c"><pre data-language="c">void free_sized(void* ptr, size_t /*size*/)
{
free(ptr);
}</pre></div> <h3 id="Example"> Example</h3> <div class="t-example"> <div class="c source-c"><pre data-language="c">#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
size_t size; // current number of elements
size_t capacity; // reserved number of elements
void** data;
} PtrVector;
PtrVector vector_create(size_t initial_capacity)
{
PtrVector ret =
{
.capacity = initial_capacity,
.data = (void**) malloc(initial_capacity * sizeof(void*))
};
return ret;
}
void vector_delete(PtrVector* self)
{
free_sized(self->data, self->capacity * sizeof(void*));
}
void vector_push_back(PtrVector* self, void* value)
{
if (self->size == self->capacity)
{
self->capacity *= 2;
self->data = (void**) realloc(self->data, self->capacity * sizeof(void*));
}
self->data[self->size++] = value;
}
int main()
{
int data = 42;
float pi = 3.141592f;
PtrVector v = vector_create(8);
vector_push_back(&v, &data);
vector_push_back(&v, &pi);
printf("data[0] = %i\n", *(int*)v.data[0]);
printf("data[1] = %f\n", *(float*)v.data[1]);
vector_delete(&v);
}</pre></div> <p>Output:</p>
<div class="text source-text"><pre data-language="c">data[0] = 42
data[1] = 3.141592</pre></div> </div> <h3 id="References"> References</h3> <ul>
<li> C23 standard (ISO/IEC 9899:2023): </li>
<ul><li> 7.24.3.4 The free_sized function </li></ul>
</ul> <h3 id="See_also"> See also</h3> <table class="t-dsc-begin"> <tr class="t-dsc"> <td> <div><a href="free" title="c/memory/free"> <span class="t-lines"><span>free</span></span></a></div> </td> <td> deallocates previously allocated memory <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="free_aligned_sized" title="c/memory/free aligned sized"> <span class="t-lines"><span>free_aligned_sized</span></span></a></div>
<div><span class="t-lines"><span><span class="t-mark-rev t-since-c23">(C23)</span></span></span></div> </td> <td> deallocates previously allocated sized and aligned memory <br> <span class="t-mark">(function)</span> </td>
</tr> <tr class="t-dsc"> <td> <div><a href="malloc" title="c/memory/malloc"> <span class="t-lines"><span>malloc</span></span></a></div> </td> <td> allocates memory <br> <span class="t-mark">(function)</span> </td>
</tr> </table> <div class="_attribution">
<p class="_attribution-p">
© cppreference.com<br>Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.<br>
<a href="https://en.cppreference.com/w/c/memory/free_sized" class="_attribution-link">https://en.cppreference.com/w/c/memory/free_sized</a>
</p>
</div>
|