summaryrefslogtreecommitdiff
path: root/devdocs/c/memory%2Ffree_sized.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/c/memory%2Ffree_sized.html')
-rw-r--r--devdocs/c/memory%2Ffree_sized.html70
1 files changed, 70 insertions, 0 deletions
diff --git a/devdocs/c/memory%2Ffree_sized.html b/devdocs/c/memory%2Ffree_sized.html
new file mode 100644
index 00000000..36a5ad8f
--- /dev/null
+++ b/devdocs/c/memory%2Ffree_sized.html
@@ -0,0 +1,70 @@
+ <h1 id="firstHeading" class="firstHeading">free_sized</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 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 &lt;stddef.h&gt;
+#include &lt;stdio.h&gt;
+#include &lt;stdlib.h&gt;
+
+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-&gt;data, self-&gt;capacity * sizeof(void*));
+}
+
+void vector_push_back(PtrVector* self, void* value)
+{
+ if (self-&gt;size == self-&gt;capacity)
+ {
+ self-&gt;capacity *= 2;
+ self-&gt;data = (void**) realloc(self-&gt;data, self-&gt;capacity * sizeof(void*));
+ }
+ self-&gt;data[self-&gt;size++] = value;
+}
+
+int main()
+{
+ int data = 42;
+ float pi = 3.141592f;
+ PtrVector v = vector_create(8);
+ vector_push_back(&amp;v, &amp;data);
+ vector_push_back(&amp;v, &amp;pi);
+ printf("data[0] = %i\n", *(int*)v.data[0]);
+ printf("data[1] = %f\n", *(float*)v.data[1]);
+ vector_delete(&amp;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">
+ &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/free_sized" class="_attribution-link">https://en.cppreference.com/w/c/memory/free_sized</a>
+ </p>
+</div>