📄 glib-memory-allocation.html
字号:
</tr></tbody></table></div><p class="since">Since 2.8</p></div><hr><div class="refsect2" lang="en"><a name="id2922059"></a><h3><a name="g-try-renew"></a>g_try_renew()</h3><a class="indexterm" name="id2922073"></a><pre class="programlisting">#define g_try_renew(struct_type, mem, n_structs)</pre><p>Attempts to reallocate the memory pointed to by <em class="parameter"><code>mem</code></em>, so that it now has space for <em class="parameter"><code>n_structs</code></em> elements of type <em class="parameter"><code>struct_type</code></em>, and returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> on failure. Contrast with <a class="link" href="glib-Memory-Allocation.html#g-renew"><code class="function">g_renew()</code></a>, which aborts the program on failure.It returns the new address of the memory, which may have been moved.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>struct_type</code></em> :</span></p></td><td>the type of the elements to allocate</td></tr><tr><td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td><td>the currently allocated memory</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_structs</code></em> :</span></p></td><td>the number of elements to allocate</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>a pointer to the new allocated memory, cast to a pointer to <em class="parameter"><code>struct_type</code></em></td></tr></tbody></table></div><p class="since">Since 2.8</p></div><hr><div class="refsect2" lang="en"><a name="id2922202"></a><h3><a name="g-malloc"></a>g_malloc ()</h3><a class="indexterm" name="id2922214"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_malloc (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);</pre><p>Allocates <em class="parameter"><code>n_bytes</code></em> bytes of memory.If <em class="parameter"><code>n_bytes</code></em> is 0 it returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>n_bytes</code></em> :</span></p></td><td>the number of bytes to allocate</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>a pointer to the allocated memory</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2922294"></a><h3><a name="g-malloc0"></a>g_malloc0 ()</h3><a class="indexterm" name="id2922307"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_malloc0 (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);</pre><p>Allocates <em class="parameter"><code>n_bytes</code></em> bytes of memory, initialized to 0's.If <em class="parameter"><code>n_bytes</code></em> is 0 it returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>n_bytes</code></em> :</span></p></td><td>the number of bytes to allocate</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>a pointer to the allocated memory</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2922387"></a><h3><a name="g-realloc"></a>g_realloc ()</h3><a class="indexterm" name="id2922400"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_realloc (<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);</pre><p>Reallocates the memory pointed to by <em class="parameter"><code>mem</code></em>, so that it now has space for<em class="parameter"><code>n_bytes</code></em> bytes of memory. It returns the new address of the memory, which mayhave been moved. <em class="parameter"><code>mem</code></em> may be <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>, in which case it's considered to have zero-length. <em class="parameter"><code>n_bytes</code></em> may be 0, in which case <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> will be returnedand <em class="parameter"><code>mem</code></em> will be freed unless it is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td><td>the memory to reallocate</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_bytes</code></em> :</span></p></td><td>new size of the memory in bytes</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>the new address of the allocated memory</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2922543"></a><h3><a name="g-try-malloc"></a>g_try_malloc ()</h3><a class="indexterm" name="id2922556"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_try_malloc (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);</pre><p>Attempts to allocate <em class="parameter"><code>n_bytes</code></em>, and returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> on failure. Contrast with <a class="link" href="glib-Memory-Allocation.html#g-malloc"><code class="function">g_malloc()</code></a>, which aborts the program on failure.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>n_bytes</code></em> :</span></p></td><td>number of bytes to allocate.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>the allocated memory, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2922652"></a><h3><a name="g-try-malloc0"></a>g_try_malloc0 ()</h3><a class="indexterm" name="id2922666"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_try_malloc0 (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);</pre><p>Attempts to allocate <em class="parameter"><code>n_bytes</code></em>, initialized to 0's, and returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> on failure. Contrast with <a class="link" href="glib-Memory-Allocation.html#g-malloc0"><code class="function">g_malloc0()</code></a>, which aborts the program on failure.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>n_bytes</code></em> :</span></p></td><td>number of bytes to allocate</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>the allocated memory, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a></td></tr></tbody></table></div><p class="since">Since 2.8</p></div><hr><div class="refsect2" lang="en"><a name="id2922769"></a><h3><a name="g-try-realloc"></a>g_try_realloc ()</h3><a class="indexterm" name="id2922782"></a><pre class="programlisting"><a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> g_try_realloc (<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem, <a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);</pre><p>Attempts to realloc <em class="parameter"><code>mem</code></em> to a new size, <em class="parameter"><code>n_bytes</code></em>, and returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>on failure. Contrast with <a class="link" href="glib-Memory-Allocation.html#g-realloc"><code class="function">g_realloc()</code></a>, which aborts the programon failure. If <em class="parameter"><code>mem</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>, behaves the same as <a class="link" href="glib-Memory-Allocation.html#g-try-malloc"><code class="function">g_try_malloc()</code></a>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td><td>previously-allocated memory, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_bytes</code></em> :</span></p></td><td>number of bytes to allocate.</td></tr><tr><td><p><span class="term"><span class="emphasis"><em>Returns</em></span> :</span></p></td><td>the allocated memory, or <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2922942"></a><h3><a name="g-free"></a>g_free ()</h3><a class="indexterm" name="id2922954"></a><pre class="programlisting">void g_free (<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem);</pre><p>Frees the memory pointed to by <em class="parameter"><code>mem</code></em>.If <em class="parameter"><code>mem</code></em> is <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> it simply returns.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"><em class="parameter"><code>mem</code></em> :</span></p></td><td>the memory to free</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2923023"></a><h3><a name="g-mem-gc-friendly"></a>g_mem_gc_friendly</h3><a class="indexterm" name="id2923036"></a><pre class="programlisting">extern gboolean g_mem_gc_friendly;</pre><p>This variable is <a class="link" href="glib-Standard-Macros.html#TRUE:CAPS"><code class="literal">TRUE</code></a> if the <code class="envar">G_DEBUG</code> environment variableincludes the key <a class="link" href="glib-running.html#G_DEBUG" title="G_DEBUG">gc-friendly</a>. </p></div><hr><div class="refsect2" lang="en"><a name="id2923074"></a><h3><a name="g-alloca"></a>g_alloca()</h3><a class="indexterm" name="id2923086"></a><pre class="programlisting">#define g_alloca(size)</pre><p>Allocates <em class="parameter"><code>size</code></em> bytes on the stack; these bytes will be freed when the currentstack frame is cleaned up. This macro essentially just wraps the <code class="function">alloca()</code> function present on most UNIX variants. Thus it provides the same advantages and pitfalls as <code class="function">alloca()</code>:</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><p><span class="term"></span></p></td><td><p> + <code class="function">alloca()</code> is very fast, as on most systems it's implemented by just adjusting the stack pointer register. </p></td></tr><tr><td><p><span class="term"></span></p></td><td><p> + It doesn't cause any memory fragmentation, within its scope, separate <code class="function">alloca()</code> blocks just build up and are released together at function end. </p></td></tr><tr><td><p><span class="term"></span></p></td><td><p> - Allocation sizes have to fit into the current stack frame. For instance in a threaded environment on Linux, the per-thread stack size is limited to 2 Megabytes, so be sparse with <code class="function">alloca()</code> uses. </p></td></tr><tr>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -