⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 glib-memory-allocation.html

📁 glid编写实例
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<html xmlns:cf="http://docbook.sourceforge.net/xmlns/chunkfast/1.0"><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Memory Allocation</title><meta name="generator" content="DocBook XSL Stylesheets V1.69.0"><link rel="start" href="index.html" title="GLib Reference Manual"><link rel="up" href="glib-core.html" title="GLib Core Application Support"><link rel="prev" href="glib-Dynamic-Loading-of-Modules.html" title="Dynamic Loading of Modules"><link rel="next" href="glib-IO-Channels.html" title="IO Channels"><meta name="generator" content="GTK-Doc V1.4 (XML mode)"><link rel="stylesheet" href="style.css" type="text/css"><link rel="chapter" href="glib.html" title="GLib Overview"><link rel="chapter" href="glib-fundamentals.html" title="GLib Fundamentals"><link rel="chapter" href="glib-core.html" title="GLib Core Application Support"><link rel="chapter" href="glib-utilities.html" title="GLib Utilities"><link rel="chapter" href="glib-data-types.html" title="GLib Data Types"><link rel="chapter" href="tools.html" title="GLib Tools"><link rel="index" href="ix01.html" title="Index"><link rel="index" href="ix02.html" title="Index of deprecated symbols"><link rel="index" href="ix03.html" title="Index of new symbols in 2.2"><link rel="index" href="ix04.html" title="Index of new symbols in 2.4"><link rel="index" href="ix05.html" title="Index of new symbols in 2.6"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" width="100%" summary="Navigation header" cellpadding="2" cellspacing="2"><tr valign="middle"><td><a accesskey="p" href="glib-Dynamic-Loading-of-Modules.html"><img src="left.png" width="24" height="24" border="0" alt="Prev"></a></td><td><a accesskey="u" href="glib-core.html"><img src="up.png" width="24" height="24" border="0" alt="Up"></a></td><td><a accesskey="h" href="index.html"><img src="home.png" width="24" height="24" border="0" alt="Home"></a></td><th width="100%" align="center">GLib Reference Manual</th><td><a accesskey="n" href="glib-IO-Channels.html"><img src="right.png" width="24" height="24" border="0" alt="Next"></a></td></tr></table><div class="refentry" lang="en"><a name="glib-Memory-Allocation"></a><div class="titlepage"></div><div class="refnamediv"><table width="100%"><tr><td valign="top"><h2><span class="refentrytitle">Memory Allocation</span></h2><p>Memory Allocation &#8212; general memory-handling.</p></td><td valign="top" align="right"></td></tr></table></div><div class="refsynopsisdiv"><h2>Synopsis</h2><pre class="synopsis">#include &lt;glib.h&gt;#define     <a href="glib-Memory-Allocation.html#g-new">g_new</a>                           (struct_type, n_structs)#define     <a href="glib-Memory-Allocation.html#g-new0">g_new0</a>                          (struct_type, n_structs)#define     <a href="glib-Memory-Allocation.html#g-renew">g_renew</a>                         (struct_type, mem, n_structs)<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Memory-Allocation.html#g-malloc">g_malloc</a>                        (<a href="glib-Basic-Types.html#gulong">gulong</a> n_bytes);<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Memory-Allocation.html#g-malloc0">g_malloc0</a>                       (<a href="glib-Basic-Types.html#gulong">gulong</a> n_bytes);<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Memory-Allocation.html#g-realloc">g_realloc</a>                       (<a href="glib-Basic-Types.html#gpointer">gpointer</a> mem,                                             <a href="glib-Basic-Types.html#gulong">gulong</a> n_bytes);<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Memory-Allocation.html#g-try-malloc">g_try_malloc</a>                    (<a href="glib-Basic-Types.html#gulong">gulong</a> n_bytes);<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Memory-Allocation.html#g-try-realloc">g_try_realloc</a>                   (<a href="glib-Basic-Types.html#gpointer">gpointer</a> mem,                                             <a href="glib-Basic-Types.html#gulong">gulong</a> n_bytes);void        <a href="glib-Memory-Allocation.html#g-free">g_free</a>                          (<a href="glib-Basic-Types.html#gpointer">gpointer</a> mem);#define     <a href="glib-Memory-Allocation.html#g-alloca">g_alloca</a>                        (size)#define     <a href="glib-Memory-Allocation.html#g-newa">g_newa</a>                          (struct_type, n_structs)#define     <a href="glib-Memory-Allocation.html#g-memmove">g_memmove</a>                       (d,s,n)<a href="glib-Basic-Types.html#gpointer">gpointer</a>    <a href="glib-Memory-Allocation.html#g-memdup">g_memdup</a>                        (<a href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> mem,                                             <a href="glib-Basic-Types.html#guint">guint</a> byte_size);            <a href="glib-Memory-Allocation.html#GMemVTable">GMemVTable</a>;void        <a href="glib-Memory-Allocation.html#g-mem-set-vtable">g_mem_set_vtable</a>                (<a href="glib-Memory-Allocation.html#GMemVTable">GMemVTable</a> *vtable);<a href="glib-Basic-Types.html#gboolean">gboolean</a>    <a href="glib-Memory-Allocation.html#g-mem-is-system-malloc">g_mem_is_system_malloc</a>          (void);extern      GMemVTable	*<a href="glib-Memory-Allocation.html#glib-mem-profiler-table">glib_mem_profiler_table</a>;void        <a href="glib-Memory-Allocation.html#g-mem-profile">g_mem_profile</a>                   (void);</pre></div><div class="refsect1" lang="en"><a name="id2902569"></a><h2>Description</h2><p>These functions provide support for allocating and freeing memory.</p><div class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>If any call to allocate memory fails, the application is terminated.This also means that there is no need to check if the call succeeded.</p></div></div><div class="refsect1" lang="en"><a name="id2902589"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2902594"></a><h3><a name="g-new"></a>g_new()</h3><a class="indexterm" name="id2902605"></a><pre class="programlisting">#define     g_new(struct_type, n_structs)</pre><p>Allocates <em class="parameter"><code>n_structs</code></em> elements of type <em class="parameter"><code>struct_type</code></em>.The returned pointer is cast to a pointer to the given type.If <em class="parameter"><code>count</code></em> is 0 it returns <code class="literal">NULL</code>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>struct_type</code></em>&#160;:</span></td><td>the type of the elements to allocate.</td></tr><tr><td><span class="term"><em class="parameter"><code>n_structs</code></em>&#160;:</span></td><td>the number of elements to allocate.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></td><td>a pointer to the allocated memory, cast to a pointer to <em class="parameter"><code>struct_type</code></em>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2902695"></a><h3><a name="g-new0"></a>g_new0()</h3><a class="indexterm" name="id2902706"></a><pre class="programlisting">#define     g_new0(struct_type, n_structs)</pre><p>Allocates <em class="parameter"><code>n_structs</code></em> elements of type <em class="parameter"><code>struct_type</code></em>, initialized to 0's.The returned pointer is cast to a pointer to the given type.If <em class="parameter"><code>count</code></em> is 0 it returns <code class="literal">NULL</code>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>struct_type</code></em>&#160;:</span></td><td>the type of the elements to allocate.</td></tr><tr><td><span class="term"><em class="parameter"><code>n_structs</code></em>&#160;:</span></td><td>the number of elements to allocate.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></td><td>a pointer to the allocated memory, cast to a pointer to <em class="parameter"><code>struct_type</code></em>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2902797"></a><h3><a name="g-renew"></a>g_renew()</h3><a class="indexterm" name="id2902806"></a><pre class="programlisting">#define     g_renew(struct_type, mem, n_structs)</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_struct</code></em> elements of type <em class="parameter"><code>struct_type</code></em>. 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><span class="term"><em class="parameter"><code>struct_type</code></em>&#160;:</span></td><td>the type of the elements to allocate.</td></tr><tr><td><span class="term"><em class="parameter"><code>mem</code></em>&#160;:</span></td><td>the currently allocated memory.</td></tr><tr><td><span class="term"><em class="parameter"><code>n_structs</code></em>&#160;:</span></td><td>the number of elements to allocate.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></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></div><hr><div class="refsect2" lang="en"><a name="id2902907"></a><h3><a name="g-malloc"></a>g_malloc ()</h3><a class="indexterm" name="id2902917"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    g_malloc                        (<a href="glib-Basic-Types.html#gulong">gulong</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 <code class="literal">NULL</code>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>n_bytes</code></em>&#160;:</span></td><td>the number of bytes to allocate.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></td><td>a pointer to the allocated memory.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2902993"></a><h3><a name="g-malloc0"></a>g_malloc0 ()</h3><a class="indexterm" name="id2903003"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    g_malloc0                       (<a href="glib-Basic-Types.html#gulong">gulong</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 <code class="literal">NULL</code>.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>n_bytes</code></em>&#160;:</span></td><td>the number of bytes to allocate.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></td><td>a pointer to the allocated memory.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2903079"></a><h3><a name="g-realloc"></a>g_realloc ()</h3><a class="indexterm" name="id2903090"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    g_realloc                       (<a href="glib-Basic-Types.html#gpointer">gpointer</a> mem,                                             <a href="glib-Basic-Types.html#gulong">gulong</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 <code class="literal">NULL</code>, in which case it's considered to have zero-length. <em class="parameter"><code>n_bytes</code></em> may be 0, in which case <code class="literal">NULL</code> will be returned.</p><div class="variablelist"><table border="0"><col align="left" valign="top"><tbody><tr><td><span class="term"><em class="parameter"><code>mem</code></em>&#160;:</span></td><td>the memory to reallocate.</td></tr><tr><td><span class="term"><em class="parameter"><code>n_bytes</code></em>&#160;:</span></td><td>new size of the memory in bytes.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></td><td>the new address of the allocated memory.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2903207"></a><h3><a name="g-try-malloc"></a>g_try_malloc ()</h3><a class="indexterm" name="id2903218"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    g_try_malloc                    (<a href="glib-Basic-Types.html#gulong">gulong</a> n_bytes);</pre><p>Attempts to allocate <em class="parameter"><code>n_bytes</code></em>, and returns <code class="literal">NULL</code> on failure. Contrast with <a 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><span class="term"><em class="parameter"><code>n_bytes</code></em>&#160;:</span></td><td>number of bytes to allocate.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></td><td>the allocated memory, or <code class="literal">NULL</code>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2903305"></a><h3><a name="g-try-realloc"></a>g_try_realloc ()</h3><a class="indexterm" name="id2903316"></a><pre class="programlisting"><a href="glib-Basic-Types.html#gpointer">gpointer</a>    g_try_realloc                   (<a href="glib-Basic-Types.html#gpointer">gpointer</a> mem,                                             <a href="glib-Basic-Types.html#gulong">gulong</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 <code class="literal">NULL</code>on failure. Contrast with <a 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 <code class="literal">NULL</code>, behaves the same as <a 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><span class="term"><em class="parameter"><code>mem</code></em>&#160;:</span></td><td>previously-allocated memory, or <code class="literal">NULL</code>.</td></tr><tr><td><span class="term"><em class="parameter"><code>n_bytes</code></em>&#160;:</span></td><td>number of bytes to allocate.</td></tr><tr><td><span class="term"><span class="emphasis"><em>Returns</em></span>&#160;:</span></td><td>the allocated memory, or <code class="literal">NULL</code>.</td></tr></tbody></table></div></div><hr><div class="refsect2" lang="en"><a name="id2903458"></a><h3><a name="g-free"></a>g_free ()</h3><a class="indexterm" name="id2903468"></a><pre class="programlisting">void        g_free                          (<a href="glib-Basic-Types.html#gpointer">gpointer</a> mem);</pre>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -