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

  • Review Aplikasi Barcode No Thanks
  • Cukur Kumis Viral di TikTok: Link Video 3 Menit 56 Detik Diserbu Netizen, Awas Link Bahaya!
  • Belinesia: Peluang Emas atau Jebakan Penipuan? Bongkar Fakta & Cara Aman Gabung!
  • Inilah Bahaya Aplikasi Auto Followers: Jebakan NCSE yang Harus Kalian Waspadai!
  • Apakah MLM Meta Force Indonesia itu Aman, Legal, atau Penipuan?
  • Apakah Zalora Community Influencer Program itu? Penipuan atau Tidak?
  • Apakah Ocean of Games Legal/Resmi & Bebas Virus?
  • Bahaya Hacking Dibalik No WA Duda Mapan Cari Jodoh Istri Pengusaha Kaya Raya yang Serius
  • Apa arti Kota Penerbit Identitas di Bank BNI?
  • Apakah Bank Saqu Astra Aman atau Penipuan?
  • Apakah Platform Amalan.id Penipu?
  • Apa itu Rekening Pasif di Bank BRI? Apa Resikonya?
  • Apa Arti Kode ‘Bunga Rekening’ di Mutasi Rekening Bank BRI?
  • Apa itu BRI Nex Card? Keuntungan, Fitur, dan Cara Daftarnya
  • Cara Mudah Membuat Rate Card dengan AI Tahun 2026
  • Cara Memperbaiki Glance Error di OnePlus 13
  • Apakah Langkah Open-source dari X Ancam Akun Anonim?
  • Kenapa Hewan Tidak Punya Klorofil?
  • Apa itu Kegiatan Review dan Pengertian Review
  • Bulgaria Lumpuhkan Situs Pembajakan Streaming & Game, AS Ikut Campur!
  • Apa itu Google Analytics 4?
  • Cara Cek Status DTSEN Terbaru, DTSEN Jadi Penentu Bansos PKH & BPNT 2026
  • Nvidia GeForce Now Resmi di Linux!
  • Kode Redeem Blade Ball Roblox Terbaru 1 Februari 2026: Raih Hadiah Spesialmu!
  • Kode Paket Internet Indosat Murah Terbaru 2026: Kuota 25GB Cuma 50 Ribu?
  • AFK Fishing Roblox: Auto Panen Ikan 24 Jam Pakai CC Cloud Phone!
  • Rahasia Meta AI WhatsApp: Cara Ampuh Dapat Cuan dari Chatbot!
  • Gokil! Case Pixel Diskon 90% di Verizon, Buruan Sikat!
  • Apakah APK CashCepat Penipu? Ada Izin Pinjol OJK?
  • Lowongan Kerja Leader di BRI Melalui BFLP Specialist 2026, Cek Syarat Lengkapnya Di Sini!
  • What are Microsoft Copilot Reminders?
  • What’s New in Plasma 6.7? Quick Notification History Clear-Up!
  • Awesome Alternatives to Microsoft Defender Application Guard (MDAG)
  • How tto Enable DLSS 4 & 5 for Your GPU
  • Backlinks: Why They’re Super Important for Your Website!
  • Cara Membuat Podcast dari PDF dengan NotebookLlama dan Groq
  • Tutorial Membuat Sistem Automatic Content Recognition (ACR) untuk Deteksi Logo
  • Apa itu Google Code Wiki?
  • Cara Membuat Agen AI Otomatis untuk Laporan ESG dengan Python dan LangChain
  • Cara Membuat Pipeline RAG dengan Framework AutoRAG
  • 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
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ
Beli Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV

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