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

📄 cha-glib.html

📁 gtk 开发手册和参考文档。 包括gtk glib gdk等
💻 HTML
📖 第 1 页 / 共 4 页
字号:
              </p>            </li>            <li>              <p>                <tt class="FUNCTION">g_free()</tt> will ignore any                <span class="STRUCTNAME">NULL</span> pointers you                pass to it.&#13;              </p>            </li>          </ul>          <p>            In addition to these minor conveniences, <tt class=             "FUNCTION">g_malloc()</tt> and <tt class="FUNCTION">            g_free()</tt> can support various kinds of memory            debugging and profiling. If you pass the <tt class=             "APPLICATION">--enable-mem-check</tt> option to glib's            configure script, the compiled <tt class="FUNCTION">            g_free()</tt> will warn you whenever you free the same            pointer twice. The <tt class="APPLICATION">            --enable-mem-profile</tt> option enables code which            keeps memory use statistics; when you call <tt class=             "FUNCTION">g_mem_profile()</tt> they are printed to the            console. Finally, you can define <span class=            "STRUCTNAME">USE_DMALLOC</span> and the glib memory            wrappers will use the <span class="STRUCTNAME">            MALLOC()</span>, etc. debugging macros available in <tt            class="FILENAME">dmalloc.h</tt> on some platforms.          </p>          <div class="FIGURE">            <a name="FL-MEMORY"></a>            <div class="FUNCSYNOPSIS">              <a name="FL-MEMORY.SYNOPSIS"></a>              <table border="0" bgcolor="#E0E0E0" width="100%">                <tr>                  <td><pre class="FUNCSYNOPSISINFO">#include &lt;glib.h&gt;</pre>                  </td>                </tr>              </table>              <p>                <code><code class="FUNCDEF">gpointer <tt class=                 "FUNCTION">g_malloc</tt></code>(gulong <tt class=                 "PARAMETER"><i>size</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">void <tt class=                "FUNCTION">g_free</tt></code>(gpointer <tt class=                 "PARAMETER"><i>mem</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">gpointer <tt class=                 "FUNCTION">g_realloc</tt></code>(gpointer <tt                class="PARAMETER"><i>mem</i></tt>, gulong <tt                class="PARAMETER"><i>size</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">gpointer <tt class=                 "FUNCTION">g_memdup</tt></code>(gconstpointer <tt                class="PARAMETER"><i>mem</i></tt>, guint <tt class=                 "PARAMETER"><i>bytesize</i></tt>);</code>              </p>            </div>            <p>              <b>Figure 5. glib memory allocation</b>            </p>          </div>          <p>            It's important to match <tt class="FUNCTION">            g_malloc()</tt> with <tt class="FUNCTION">            g_free()</tt>, plain <tt class="FUNCTION">malloc()</tt>            with <tt class="FUNCTION">free()</tt>, and (if you're            using C++) <span class="STRUCTNAME">new</span> with            <span class="STRUCTNAME">delete</span>. Otherwise bad            things can happen, since these allocators may use            different memory pools (and <span class="STRUCTNAME">            new</span>/<span class="STRUCTNAME">delete</span> call            constructors and destructors).          </p>          <p>            Of course there's a <tt class="FUNCTION">            g_realloc()</tt> equivalent to <tt class="FUNCTION">            realloc()</tt>. There's also a convenient <tt class=             "FUNCTION">g_malloc0()</tt> which fills allocated            memory with 0s, and <tt class="FUNCTION">            g_memdup()</tt> which returns a copy of <span class=             "STRUCTNAME">bytesize</span> bytes starting at <span            class="STRUCTNAME">mem</span>. <tt class="FUNCTION">            g_realloc()</tt> and <tt class="FUNCTION">            g_malloc0()</tt> will both accept a <span class=             "STRUCTNAME">size</span> of <span class="STRUCTNAME">            0</span>, for consistency with <span class=            "STRUCTNAME">g_malloc()</span>. However, <tt class=             "FUNCTION">g_memdup()</tt> will not.          </p>          <p>            If it isn't obvious: <tt class="FUNCTION">            g_malloc0()</tt> fills raw memory with unset bits, not            the value <span class="STRUCTNAME">0</span> for            whatever type you intend to put there. Occasionally            someone expects to get an array of floating point            numbers initialized to <span class="STRUCTNAME">            0.0</span>; this will <i class="EMPHASIS">not</i> work.          </p>          <p>            Finally, there are type-aware allocation macros, shown            in <a href="cha-glib.html#ML-G-NEW">Figure 6</a>. The            <span class="STRUCTNAME">type</span> argument to each            of these is the name of a type, and the <span class=             "STRUCTNAME">count</span> argument is the number of            <span class="STRUCTNAME">type</span>-size blocks to            allocate. These macros save you some typing and            multiplication, and are thus less error-prone. They            automatically cast to the target pointer type, so            attempting to assign the allocated memory to the wrong            kind of pointer should trigger a compiler warning. (If            you have warnings turned on, as a responsible            programmer should!)          </p>          <div class="FIGURE">            <a name="ML-G-NEW"></a>            <div class="FUNCSYNOPSIS">              <a name="ML-G-NEW.SYNOPSIS"></a>              <table border="0" bgcolor="#E0E0E0" width="100%">                <tr>                  <td><pre class="FUNCSYNOPSISINFO">#include &lt;glib.h&gt;</pre>                  </td>                </tr>              </table>              <p>                <code><code class="FUNCDEF"><tt class="FUNCTION">                g_new</tt></code>(<tt class=                "PARAMETER"><i>type</i></tt>, <tt class=                "PARAMETER"><i>count</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF"><tt class="FUNCTION">                g_new0</tt></code>(<tt class=                "PARAMETER"><i>type</i></tt>, <tt class=                "PARAMETER"><i>count</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF"><tt class="FUNCTION">                g_renew</tt></code>(<tt class=                "PARAMETER"><i>type</i></tt>, <tt class=                "PARAMETER"><i>mem</i></tt>, <tt class="PARAMETER">                <i>count</i></tt>);</code>              </p>            </div>            <p>              <b>Figure 6. Allocation macros</b>            </p>          </div>        </div>        <div class="SECT2">          <h2 class="SECT2">            <a name="Z28">String Handling</a>          </h2>          <p>            glib provides a number of functions for string            handling; some are unique to glib, and some solve            portability concerns. They all interoperate nicely with            the glib memory allocation routines.          </p>          <p>            For those interested in a better string than <span            class="STRUCTNAME">gchar*</span>, there's also a <span            class="STRUCTNAME">GString</span> type. It isn't            covered in this book, but documentation is available at            <a href="http://www.gtk.org/" target="_top">            http://www.gtk.org/</a>.          </p>          <div class="FIGURE">            <a name="FL-STREXT"></a>            <div class="FUNCSYNOPSIS">              <a name="FL-STREXT.SYNOPSIS"></a>              <table border="0" bgcolor="#E0E0E0" width="100%">                <tr>                  <td><pre class="FUNCSYNOPSISINFO">#include &lt;glib.h&gt;</pre>                  </td>                </tr>              </table>              <p>                <code><code class="FUNCDEF">gint <tt class=                "FUNCTION">g_snprintf</tt></code>(gchar* <tt class=                 "PARAMETER"><i>buf</i></tt>, gulong <tt class=                 "PARAMETER"><i>n</i></tt>, const gchar* <tt class=                 "PARAMETER"><i>format</i></tt>, <tt class=                "PARAMETER"><i>...</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">gint <tt class=                "FUNCTION">g_strcasecmp</tt></code>(const gchar*                <tt class="PARAMETER"><i>s1</i></tt>, const gchar*                <tt class="PARAMETER"><i>s2</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">gint <tt class=                "FUNCTION">g_strncasecmp</tt></code>(const gchar*                <tt class="PARAMETER"><i>s1</i></tt>, const gchar*                <tt class="PARAMETER"><i>s2</i></tt>, guint <tt                class="PARAMETER"><i>n</i></tt>);</code>              </p>            </div>            <p>              <b>Figure 7. Portability Wrappers</b>            </p>          </div>          <p>            <a href="cha-glib.html#FL-STREXT">Figure 7</a> shows            some substitutes glib provides for commonly-implemented            but unportable extensions to ANSI C.          </p>          <p>            One of the annoying things about C is that it provides            the crash-causing, security-hole-creating, generally            evil <tt class="FUNCTION">sprintf()</tt>, but the            relatively safe and widely implemented <tt class=             "FUNCTION">snprintf()</tt> is a vendor extension. <tt            class="FUNCTION">g_snprintf()</tt> wraps native <tt            class="FUNCTION">snprintf()</tt> on platforms that have            it, and provides an implementation on those that don't.            So you can say goodbye to <tt class="FUNCTION">            sprintf()</tt> forever. Even better: classically, <tt            class="FUNCTION">snprintf()</tt> does not guarantee            that it will <span class="STRUCTNAME">            NULL</span>-terminate the buffer it fills. <tt class=             "FUNCTION">g_snprintf()</tt> does.          </p>          <p>            <tt class="FUNCTION">g_strcasecmp()</tt> and <tt class=             "FUNCTION">g_strncasecmp()</tt> perform a            case-insensitive comparison of two strings, optionally            with a maximum length. <span class="STRUCTNAME">            strcasecmp()</span> is available on many platforms but            not universally, so using glib instead is advisable.          </p>          <p>            The functions in <a href="cha-glib.html#FL-STRMANIP">            Figure 8</a> modify a string in-place: the first two            convert the string to lowercase or uppercase,            respectively, while <tt class="FUNCTION">            g_strreverse()</tt> reverses its characters. <tt class=             "FUNCTION">g_strchug()</tt> and <tt class="FUNCTION">            g_strchomp()</tt> "chug" the string (remove leading            spaces), or "chomp" it (remove trailing spaces). These            last two return the string, in addition to modifying it            in-place; in some cases it may be convenient to use the            return value. There is a macro, <tt class="FUNCTION">            g_strstrip()</tt>, which combines both functions to            remove both leading and trailing spaces; it is used            just as the individual functions are.          </p>          <div class="FIGURE">            <a name="FL-STRMANIP"></a>            <div class="FUNCSYNOPSIS">              <a name="FL-STRMANIP.SYNOPSIS"></a>              <table border="0" bgcolor="#E0E0E0" width="100%">                <tr>                  <td><pre class="FUNCSYNOPSISINFO">#include &lt;glib.h&gt;</pre>                  </td>                </tr>              </table>              <p>                <code><code class="FUNCDEF">void <tt class=                "FUNCTION">g_strdown</tt></code>(gchar* <tt class=                 "PARAMETER"><i>string</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">void <tt class=                "FUNCTION">g_strup</tt></code>(gchar* <tt class=                 "PARAMETER"><i>string</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">void <tt class=                "FUNCTION">g_strreverse</tt></code>(gchar* <tt                class="PARAMETER"><i>string</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">gchar* <tt class=                 "FUNCTION">g_strchug</tt></code>(gchar* <tt class=                 "PARAMETER"><i>string</i></tt>);</code>              </p>              <p>                <code><code class="FUNCDEF">gchar* <tt class=                 "FUNCTION">g_strchomp</tt></code>(gchar* <tt class=                 "PARAMETER"><i>string</i></tt>);</code>              </p>            </div>            <p>              <b>Figure 8. In-place string modifications</b>

⌨️ 快捷键说明

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