Skip to content

emka.web.id

menulis pengetahuan – merekam peradaban

Menu
  • Home
  • Tutorial
  • Search
Menu

Glib Memory

Posted on April 10, 2012

glib wraps the standard malloc() and free() with its own g_ variants, g_malloc() and g_free(), shown in Figure 2-5. These are nice in several small ways:
• g_malloc() always returns a gpointer, never a char*, so there’s no need to cast the return value.
• g_malloc() aborts the program if the underlying malloc() fails, so you don’t have to check for a NULL return value.
• g_malloc() gracefully handles a size of 0, by returning NULL.
• g_free() will ignore any NULL pointers you pass to it.

In addition to these minor conveniences, g_malloc() and g_free() can support various kinds of memory debugging and profiling. If you pass the -enable-mem-check option to glib’s configure script, the compiled g_free() will warn you whenever you free the same pointer twice. The -enable-mem-profile option enables code which keeps memory use statistics; when you call g_mem_profile() they are printed to the console. Finally, you can define USE_DMALLOC and the glib memory wrappers will use the MALLOC(), etc. debugging macros available in dmalloc.h on some plat- forms.

[code lang=”cpp”]
#include <glib.h>

gpointer g_malloc(gulong size);
void g_free(gpointer mem);
gpointer g_realloc(gpointer mem, gulong size);
gpointer g_memdup(gconstpointer mem, guint bytesize);

[/code]

It’s important to match g_malloc() with g_free(), plain malloc() with free(), and (if you’re using C++) new with delete. Otherwise bad things can happen, since these allocators may use different memory pools (and new/delete call constructors and destructors).

Of course there’s a g_realloc() equivalent to realloc(). There’s also a convenient g_malloc0() which fills allocated memory with 0s, and g_memdup() which returns a copy of bytesize bytes starting at mem. g_realloc() and g_malloc0() will both accept a size of 0, for consistency with g_malloc(). However, g_memdup() will not.

If it isn’t obvious: g_malloc0() fills raw memory with unset bits, not the value 0 for whatever type you intend to put there. Occasionally someone expects to get an array of floating point numbers initialized to 0.0; this will not work.

Finally, there are type-aware allocation macros, shown in Figure 2-6. The type argu- ment to each of these is the name of a type, and the count argument is the number of type-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 at- tempting 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!)

[code lang=”cpp”]
#include <glib.h>
g_new(type, count);
g_new0(type, count);
g_renew(type, mem, count);
[/code]

Source: Havoc Pennington. 1999. GTK Gnome Application Development. New York: New Riders Publishing

Terbaru

  • Kenapa Komputer Sangat Panas Saat Gunakan Fitur Virtualisasi Hyper-V?
  • Apa itu Bug React2Shell? Sudah Serang Lebih dari 30 Organisasi dan 77.000 IP Address
  • Google Store Black Friday 2025: Penawaran Spesial untuk Pixel, Nest, dan Lainnya!
  • Boxville 2 Gratis di Playstore, Plus Diskon Lainnya!
  • Cara Atasi Masalah Pembacaan Suara (Read Aloud) di Windows Copilot Tidak Berfungsi
  • Kementerian Kesehatan Inggris Akui Data Breach, Akibat Zero-day Oracle DB?
  • Google Akan Perkenalkan Autofill Google Wallet di Chrome untuk Pembayaran Lebih Mudah
  • Google Pixel Akan Perkenalkan Launcher Device Search Baru, Lebih Cepat dan Pintar
  • Hacker Serang Bug VPN di ArrayOS AG untuk Menanam Web Shell
  • Cara Menonaktifkan Error “ITS Almost time to restart in Windows”
  • Google Fi Mendukung Panggilan Telepon RCS Melalui Web, Lebih Mudah dan Efisien
  • Data Breach Marquis: Hajar Lebih Dari 74 Bank dan Koperasi AS
  • Google Search Akan Adopsi ‘Continuous Circle’ untuk Hasil Pencarian Terjemahan, Lebih Cerdas dan Kontekstual
  • Rusia Memblokir Roblox Karena Distribusi ‘Propaganda LGBT’
  • Google Gemini Redesain Web Total di Desember 2025, Fokus UX yang Lebih Baik
  • Apa itu Google Workspace Studio? Tool Baru untuk Pembuat Konten?
  • Cara Menggunakan Xbox Full-Screen Experience di Windows
  • Korea Tahan Tersangka Terkait Penjualan Video Intim dari Kamera CCTV yang Diretas
  • Kebocoran Galaxy Buds 4 Mengungkap Desain dan Fitur Baru, Mirip Apple?
  • Sudah Update Windows KB5070311 dan Apa Saja Yang Diperbaiki?
  • Cara Menonaktifkan Fitur AI Actions (Tindakan AI) di Menu Windows Explorer
  • Microsoft Edge AI vs. OpenAI’s Atlas Browser: Perbandingan dan Perbedaan Utama
  • Cara Memasang Folder Sebagai Drive di Windows 11
  • Cara Memperbaiki Error 0xC1900101 0x40021 pada Update Windows 11
  • Malware Glassworm Serang Lagi VSCode, Hati-hati!
  • Walmart dan Google Bermitra untuk Kamera Rumah Google Home: Pengalaman Langsung
  • Gemini Dapat Bisa Atur Perangkat Rumah Melalui Home Assistant Pakai Suara, Desember 2025
  • Asahi, Produsen Bir Jepang, Akui Kebocoran Data 15 Juta Pelanggan
  • Google Messages Ada Fitur Baru: Pesan Grup, Mode Gelap dan Integrasi dengan Google Duo
  • 5 Laptop ASUS Terbaik dengan Tampilan Mewah dan Build Quality Premium
  • Kenapa Komputer Sangat Panas Saat Gunakan Fitur Virtualisasi Hyper-V?
  • Apa itu Bug React2Shell? Sudah Serang Lebih dari 30 Organisasi dan 77.000 IP Address
  • Google Store Black Friday 2025: Penawaran Spesial untuk Pixel, Nest, dan Lainnya!

©2025 emka.web.id | Design: Newspaperly WordPress Theme