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

  • Inilah 5 Laptop Lenovo Paling Awet dan Tangguh Buat Investasi Jangka Panjang Kalian
  • Inilah Kronologi Tragis Mahasiswa PNP Padang Ditemukan Meninggal di Kamar Kos 11 April 2026
  • Inilah Kenapa Akun WhatsApp Kalian Sedang Ditinjau dan Cara Mengatasinya Biar Normal Lagi
  • Inilah Kronologi Gadis Lampung Nekat Menyamar Jadi Pria Demi Lamar Kekasih di Sinjai yang Berujung Urusan Polisi
  • Inilah Kronologi Kecelakaan Beruntun Jalur Purworejo-Magelang 11 April 2026: Berawal Dari Rem Mendadak Karena Kucing Melintas
  • Inilah Kabar Mengejutkan OTT KPK Bupati Tulungagung Gatut Sunu Wibowo, Begini Kronologi dan Profil Lengkapnya
  • Inilah Mod HUD Minecraft 2026 yang Bikin Tampilan Game Kalian Makin Keren dan Informatif
  • Inilah Panduan Lengkap UM-PTKIN 2026: Jadwal, Cara Daftar, dan Tips Strategis Memilih Jurusan di UIN IAIN STAIN
  • Inilah Alasan Kenapa Tiny House di Sleman Makin Populer dan Cara Bangunnya dengan Budget 100 Jutaan
  • Inilah Alasan Kenapa Akun Instagram Wapres Gibran Sempat Follow Akun Judi Online, Ternyata Begini Faktanya!
  • Inilah Kronologi Lengkap Kasus Dugaan Penistaan Agama Injak Alquran di Lebak Banten yang Lagi Viral
  • Inilah Rekomendasi Instagram Downloader Terbaik 2026, Cara Download Reels dan Foto Paling Sat Set!
  • Inilah Alasan Kenapa Channel YouTube Kalian Gak Berkembang Meskipun Kontennya Udah Bagus
  • Inilah Cara Buka Situs yang Diblokir di Tahun 2026, Trik Rahasia Tanpa VPN!
  • Inilah Cara Menghilangkan Iklan di HP Xiaomi dan POCO Paling Ampuh Tanpa Root, Update April 2026
  • Inilah 7 Rekomendasi HP 1 Jutaan Terbaik yang Layak Kalian Lirik, Speknya Nggak Kaleng-Kaleng!
  • Inilah 7 Cara Mengembalikan Foto yang Terhapus Permanen di HP, Ternyata Masih Bisa Diselamatkan!
  • Inilah Cara Mengatasi Baterai Boros Setelah Update HyperOS yang Paling Ampuh
  • Inilah Pokémon Champions 2026, Game Battle Kompetitif Terbaru dan Cara Download-nya yang Perlu Kalian Tahu!
  • Inilah Doods Viral: Pengertian, Bahaya, dan Kenapa Kalian Harus Ekstra Waspada!
  • Gini Caranya Ngebangun Bisnis AI yang Menguntungkan dalam 48 Jam Saja!
  • Pengertian “He is Risen” Adalah?
  • Inilah Rekomendasi HP Snapdragon Paling Murah dengan RAM 8 GB Terupdate April 2026
  • Inilah 12 Kampus Negeri di Jogja yang Jarang Diketahui, Ternyata Banyak yang Kasih Kuliah Gratis!
  • Inilah Rekomendasi HP Tecno Kamera Terbaik 2026, Spek Gahar Harga Tetap Pelajar!
  • Apa itu PPU UTBK? Ini Rahasia Taklukkan Skor Tinggi di SNBT 2026 Tanpa Harus Menghafal!
  • Inilah Alasan Kenapa Lapisan Es Greenland Ternyata Bisa Bergerak Kayak Adonan yang Dipanaskan
  • Inilah Kode Redeem FC Mobile 10 April 2026 dan Rahasia Panen Pemain OVR 117
  • Apa itu Benwit/Bensin Sawit? Benarkah Bisa Jadi Solusi Bahan Bakar Masa Depan atau Cuma Hoaks Belaka?
  • Inilah 4 Tablet 5G Termurah April 2026 yang Kencang dan Worth It untuk Kerja!
  • Is it Time to Replace Nano? Discover Fresh, the Terminal Text Editor You Actually Want to Use
  • How to Design a Services Like Google Ads
  • How to Fix 0x800ccc0b Outlook Error: Step-by-Step Guide for Beginners
  • How to Fix NVIDIA App Error on Windows 11: Simple Guide
  • How to Fix Excel Formula Errors: Quick Fixes for #NAME
  • How to Use VoxCPM2: The Complete Tutorial for Professional Voice Cloning and AI Speech Generation
  • Complete tutorial for Creao AI: How to build smart AI agents that automate your daily tasks
  • How to Streamline Your Digital Workflow with TeraBox AI: A Complete Tutorial for Beginners
  • How to Run Google Gemma 4 Locally: A Beginner’s Guide to Tiny but Mighty AI Models
  • A Beginner Tutorial on Cloning Website Source Code Using ChatGPT and AI Logic Reconstruction
  • Apa itu Spear-Phishing via npm? Ini Pengertian dan Cara Kerjanya yang Makin Licin
  • Apa Itu Predator Spyware? Ini Pengertian dan Kontroversi Penghapusan Sanksinya
  • Mengenal Apa itu TONESHELL: Backdoor Berbahaya dari Kelompok Mustang Panda
  • Siapa itu Kelompok Hacker Silver Fox?
  • Apa itu CVE-2025-52691 SmarterMail? Celah Keamanan Paling Berbahaya Tahun 2025

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