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

📄 glib-memory-allocation.html

📁 最新gtk中文资料集
💻 HTML
📖 第 1 页 / 共 3 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=US-ASCII"><title>Memory Allocation</title><meta name="generator" content="DocBook XSL Stylesheets V1.73.2"><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.9 (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"><link rel="index" href="ix06.html" title="Index of new symbols in 2.8"><link rel="index" href="ix07.html" title="Index of new symbols in 2.10"><link rel="index" href="ix08.html" title="Index of new symbols in 2.12"><link rel="index" href="ix09.html" title="Index of new symbols in 2.14"><link rel="index" href="ix10.html" title="Index of new symbols in 2.16"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table class="navigation" id="top" 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><tr><td colspan="5" class="shortcuts"><nobr><a href="#id2920965" class="shortcut">Top</a>                  &#160;|&#160;                  <a href="#id2921342" class="shortcut">Description</a></nobr></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><a name="id2920965"></a><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 class="link" href="glib-Memory-Allocation.html#g-new">g_new</a>                               (struct_type, n_structs)#define             <a class="link" href="glib-Memory-Allocation.html#g-new0">g_new0</a>                              (struct_type, n_structs)#define             <a class="link" href="glib-Memory-Allocation.html#g-renew">g_renew</a>                             (struct_type, mem, n_structs)#define             <a class="link" href="glib-Memory-Allocation.html#g-try-new">g_try_new</a>                           (struct_type, n_structs)#define             <a class="link" href="glib-Memory-Allocation.html#g-try-new0">g_try_new0</a>                          (struct_type, n_structs)#define             <a class="link" href="glib-Memory-Allocation.html#g-try-renew">g_try_renew</a>                         (struct_type, mem, n_structs)<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Allocation.html#g-malloc">g_malloc</a>                            (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Allocation.html#g-malloc0">g_malloc0</a>                           (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Allocation.html#g-realloc">g_realloc</a>                           (<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);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Allocation.html#g-try-malloc">g_try_malloc</a>                        (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Allocation.html#g-try-malloc0">g_try_malloc0</a>                       (<a class="link" href="glib-Basic-Types.html#gsize">gsize</a> n_bytes);<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Allocation.html#g-try-realloc">g_try_realloc</a>                       (<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);void                <a class="link" href="glib-Memory-Allocation.html#g-free">g_free</a>                              (<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a> mem);extern              gboolean <a class="link" href="glib-Memory-Allocation.html#g-mem-gc-friendly">g_mem_gc_friendly</a>;#define             <a class="link" href="glib-Memory-Allocation.html#g-alloca">g_alloca</a>                            (size)#define             <a class="link" href="glib-Memory-Allocation.html#g-newa">g_newa</a>                              (struct_type, n_structs)#define             <a class="link" href="glib-Memory-Allocation.html#g-memmove">g_memmove</a>                           (dest,src,len)<a class="link" href="glib-Basic-Types.html#gpointer">gpointer</a>            <a class="link" href="glib-Memory-Allocation.html#g-memdup">g_memdup</a>                            (<a class="link" href="glib-Basic-Types.html#gconstpointer">gconstpointer</a> mem,                                                         <a class="link" href="glib-Basic-Types.html#guint">guint</a> byte_size);                    <a class="link" href="glib-Memory-Allocation.html#GMemVTable">GMemVTable</a>;void                <a class="link" href="glib-Memory-Allocation.html#g-mem-set-vtable">g_mem_set_vtable</a>                    (<a class="link" href="glib-Memory-Allocation.html#GMemVTable">GMemVTable</a> *vtable);<a class="link" href="glib-Basic-Types.html#gboolean">gboolean</a>            <a class="link" href="glib-Memory-Allocation.html#g-mem-is-system-malloc">g_mem_is_system_malloc</a>              (void);extern              GMemVTable	*<a class="link" href="glib-Memory-Allocation.html#glib-mem-profiler-table">glib_mem_profiler_table</a>;void                <a class="link" href="glib-Memory-Allocation.html#g-mem-profile">g_mem_profile</a>                       (void);</pre></div><div class="refsect1" lang="en"><a name="id2921342"></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 class="note" style="margin-left: 0.5in; margin-right: 0.5in;"><h3 class="title">Note</h3><p>It's important to match <a class="link" href="glib-Memory-Allocation.html#g-malloc"><code class="function">g_malloc()</code></a> with <a class="link" href="glib-Memory-Allocation.html#g-free"><code class="function">g_free()</code></a>, plain <code class="function">malloc()</code> with <code class="function">free()</code>,and (if you're using C++) new with delete and new[] with delete[]. Otherwisebad things can happen, since these allocators may use different memorypools (and new/delete call constructors and destructors). See also<a class="link" href="glib-Memory-Allocation.html#g-mem-set-vtable"><code class="function">g_mem_set_vtable()</code></a>.</p></div></div><div class="refsect1" lang="en"><a name="id2921432"></a><h2>Details</h2><div class="refsect2" lang="en"><a name="id2921442"></a><h3><a name="g-new"></a>g_new()</h3><a class="indexterm" name="id2921455"></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>n_structs</code></em> is 0 it returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</p><p>Since the returned pointer is already casted to the right type,it is normally unnecessary to cast it explicitly, and doingso might hide memory allocation errors.</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>&#160;:</span></p></td><td>the type of the elements to allocate</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_structs</code></em>&#160;:</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>&#160;:</span></p></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="id2921556"></a><h3><a name="g-new0"></a>g_new0()</h3><a class="indexterm" name="id2921568"></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>n_structs</code></em> is 0 it returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a>.</p><p>Since the returned pointer is already casted to the right type,it is normally unnecessary to cast it explicitly, and doingso might hide memory allocation errors.</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>&#160;:</span></p></td><td>the type of the elements to allocate.</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_structs</code></em>&#160;:</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>&#160;:</span></p></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="id2921670"></a><h3><a name="g-renew"></a>g_renew()</h3><a class="indexterm" name="id2921682"></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_structs</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><p><span class="term"><em class="parameter"><code>struct_type</code></em>&#160;:</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>&#160;:</span></p></td><td>the currently allocated memory</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_structs</code></em>&#160;:</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>&#160;:</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></div><hr><div class="refsect2" lang="en"><a name="id2921782"></a><h3><a name="g-try-new"></a>g_try_new()</h3><a class="indexterm" name="id2921796"></a><pre class="programlisting">#define             g_try_new(struct_type, n_structs)</pre><p>Attempts to allocate <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-new"><code class="function">g_new()</code></a>, which aborts the program on failure.The returned pointer is cast to a pointer to the given type. If <em class="parameter"><code>n_structs</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>struct_type</code></em>&#160;:</span></p></td><td>the type of the elements to allocate</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_structs</code></em>&#160;:</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>&#160;:</span></p></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><p class="since">Since 2.8</p></div><hr><div class="refsect2" lang="en"><a name="id2921920"></a><h3><a name="g-try-new0"></a>g_try_new0()</h3><a class="indexterm" name="id2921935"></a><pre class="programlisting">#define             g_try_new0(struct_type, n_structs)</pre><p>Attempts to allocate <em class="parameter"><code>n_structs</code></em> elements of type <em class="parameter"><code>struct_type</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-new0"><code class="function">g_new0()</code></a>, which aborts the program on failure.The returned pointer is cast to a pointer to the given type.The function returns <a class="link" href="glib-Standard-Macros.html#NULL:CAPS"><code class="literal">NULL</code></a> when <em class="parameter"><code>n_structs</code></em> is 0.</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>&#160;:</span></p></td><td>the type of the elements to allocate</td></tr><tr><td><p><span class="term"><em class="parameter"><code>n_structs</code></em>&#160;:</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>&#160;:</span></p></td><td>a pointer to the allocated memory, cast to a pointer to <em class="parameter"><code>struct_type</code></em></td>

⌨️ 快捷键说明

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