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 3 Karakteristik Pembagian Masyarakat Menurut Sibrani yang Bikin Kita Paham Struktur Sosial
  • Inilah Cara Mengatasi Status Bansos Atensi YAPI NTPN Tidak Ditemukan Biar Bantuan Tetap Cair!
  • Cara Mudah Unduh Video DS2Play Tanpa Ribet
  • Apa itu Free Float di Dunia Saham? Ini Artinya
  • Hati-Hati Modus Penipuan Asuransi BCA, Ini Caranya!
  • Inilah Panduan Lengkap Pendaftaran PPDB SMA Unggul Garuda Baru 2026, Simak Syarat dan Alurnya!
  • Alternatif Terbaik Dari OmeTV, Tanpa Takut Kena Banned
  • Tips Nama Petugas TKA SD/SMP Muncul Otomatis di Berita Acara
  • Inilah Fakta di Balik Video Botol Teh Pucuk Viral yang Lagi Rame di TikTok!
  • Apa itu Aturan Waktu Futsal dan Extra Time di Permainan Futsal?
  • Contoh Jawaban Refleksi Diri “Bagaimana Refleksi tentang Praktik Kinerja Selama Observasi Praktik Kinerja”
  • Main Telegram Dapat Uang Hoax atau Fakta?
  • Apa itu Lock iCloud? Ini Artinya
  • Integrasi KBC dan PM di Madrasah? Ini Pengertian dan Contoh Praktiknya
  • Ini Trik Input Pelaksana PBJ di Dapodik 2026.C Biar Info GTK Langsung Valid dan Aman!
  • Apa Maksud Hukum Dasar yang Dijadikan Pegangan dalam Penyelenggaraan Suatu Negara? Ini Jawabannya
  • Apakah Apk Puskanas Penipuan?
  • Inilah 10 Alternatif Mesin Pencari Selain Yandex yang Anti Blokir dan Aman Digunakan
  • Caranya Supaya WhatsApp Nggak Kena Spam Terus Meski Sudah Ganti Nomor, Ternyata Ini Rahasianya!
  • Jangan Tergiur Harga Murah! Inilah Deretan Risiko Fatal Membeli iPhone Lock iCloud
  • Mudik Gratis Pemprov Jateng 2026? Ini Pengertian dan Alur Lengkapnya
  • Inilah Cara Cek KIS Aktif Atau Tidak Lewat HP dan Solusi Praktis Jika Kepesertaan Nonaktif
  • Apa tiu Keberagaman? dan Kenapa Kita Butuh Perbedaan
  • Inilah Rekomendasi Tablet RAM 8 GB Paling Murah 2026 Buat Kerja dan Kuliah!
  • Ini Bocoran Honorable Mention TOTY FC Mobile OVR 117 dan 34 Kode Redeem Paling Baru!
  • Inilah Cara Memilih Smartband GPS Terbaik Biar Olahraga Kalian Makin Efektif!
  • Cara Cek Garansi iPhone dengan Benar, Penting Banget Buat yang Mau Beli HP Baru atau Bekas!
  • Inilah Infinix Note 60 Pro, HP Midrange yang Punya Desain Mirip iPhone dan Fitur Unik ala Nothing Phone!
  • Cara Mengatasi Digi Bank BJB Error dan Nggak Bisa Dibuka!
  • Inilah Kronologi & Kenapa Link Video Viral Andira McQueen di Dalam Mobil yang Bikin Geger Netizen Makassar
  • How to Fix the Subscript Out of Range Error in Microsoft Excel
  • What’s New in Podman 5.8: Quadlet & SQLite Migration Explained
  • Microsoft Fixes Old Windows 10 Bug Affecting Parental Controls
  • OpenVPN 2.7 Released with Multi-Socket Support Explained
  • IPFire Launches Community-Driven DBL for Enhanced Domain Blocking
  • Prompt AI Menyusun Script Pola Suara Karakter agar Brand Jadi Ikonik
  • Prompt AI untuk Merancang Karakter Brand yang Ikonik
  • Prompt AI Audit Konten Sesuai Karakter Brand
  • Prompt AI Merubah Postingan LinkedIn Jadi Ladang Diskusi dengan ChatGPT
  • Prompt AI: Paksa Algoritma LinkedIn Promosikan Konten Kalian
  • 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 Morning Star Kursi Gaming/Kantor disini: https://s.shopee.co.id/805iTUOPRV
Beli Pemotong Rumput dengan Baterai IRONHOOF 588V Mesin Potong Rumput 88V disini https://s.shopee.co.id/70DBGTHtuJ

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