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 Rekomendasi Sepeda Lipat Listrik Jarak Tempuh Terjauh Biar Mobilitas Kalian Makin Praktis!
  • Inilah 30 Ucapan Hari Kartini 2026 Paling Bermakna untuk Caption Media Sosial dan Pesan Personal yang Inspiratif
  • Inilah Cara Daftar Manajer Koperasi Merah Putih, Peluang Karir BUMN dengan 30.000 Formasi
  • Inilah 5 Rekomendasi Bedak Two Way Cake Anti Oksidasi Supaya Wajah Nggak Terlihat Kusam dan Gelap
  • Inilah Syarat dan Cara Mengurus Surat Sehat Jasmani Rohani untuk Daftar Manajer Kopdes Merah Putih
  • Inilah Alasan Harga Plastik Naik dan 7 Alternatif Kemasan Ramah Lingkungan yang Lebih Hemat buat UMKM
  • Inilah Profil Syekh Ahmad Al Misry dan Rekam Jejaknya yang Sedang Viral di Media Sosial
  • Inilah Alasan HP Nokia Jadul Harganya Masih Selangit dan Banyak Diburu Kolektor di Tahun 2026
  • Inilah HP Samsung A Series Terbaru yang Pakai Layar AMOLED, Mewah Tapi Harganya Terjangkau Banget!
  • Inilah Vivo T5 Pro, HP Midrange dengan Baterai 9.020 mAh yang Siap Comeback ke Indonesia!
  • Inilah Cara Daftar Manajer Koperasi Desa Merah Putih 2026, Ada 30.000 Formasi yang Bisa Kalian Lamar!
  • Inilah Alasan Kenapa Kemkomdigi Ancam Blokir Wikipedia dan Dampak Seriusnya Buat Masa Depan Pendidikan Kita
  • Inilah Cara Cek BLT Desa 2026 Tahap 2, Cair Rp900 Ribu Langsung ke Tangan Kalian!
  • Inilah Cara Cek PIP Lewat HP 2026 dan Panduan Lengkap Pencairannya Biar Dana Nggak Hangus
  • Inilah 51 Kode Redeem FF Aktif 17 April 2026, Buruan Klaim Skin Blue Angelic dan Bundle Langka!
  • Inilah Huawei Mate 80 Pro, Sang Legenda Flagship yang Kembali dengan Kamera True-to-Colour dan Ketahanan Luar Biasa
  • Inilah Deretan iPhone Terlawas yang Masih Layak Dibeli Tahun 2026, Performa Tetap Ngebut dan Harga Terjangkau!
  • Inilah HP Oppo Paling Worth It di Tahun 2026, Spesifikasi Gahar Tapi Harga Tetap Merakyat!
  • Inilah Daftar Link Nonton Film Ilegal yang Diblokir Komdigi dan Alasan Kenapa Kalian Harus Waspada
  • Inilah Lenovo Legion Y70 (2026), HP Gaming yang Siap Guncang Pasar dengan Teknologi AI
  • Inilah Rekomendasi HP Oppo Murah Spek Mewah Tahun 2026 yang Nggak Bakal Bikin Kantong Kalian Jebol!
  • Inilah 15 Situs Nonton Film Gratis Legal 2026, Bye-Bye Iklan Ganggu dan Drama Situs Ilegal!
  • Inilah Beasiswa Bekraf Digital Talent 2026: Solusi Buat Kalian yang Mau Jago AI dan Makin Produktif
  • Inilah Cara Memilih Power Bank yang Aman Dibawa ke Pesawat Agar Tidak Disita Petugas
  • Inilah Bocoran Harga dan Spesifikasi Redmi K90 Max, HP Gaming Dimensity 9500 Termurah yang Segera Dirilis!
  • Inilah RedMagic Gaming Tablet 5 Pro, Monster Gaming dengan Layar 185Hz dan Chipset Snapdragon 8 Elite yang Bikin Melongo!
  • Inilah Kenapa IGRS Trending di X, Bocorkan Spoiler Penting Game 007 First Light Hingga Ending!
  • Inilah 5 HP Murah Terbaru dengan Baterai Jumbo yang Siap Masuk Indonesia 2026, Ada yang Sampai 7.800 mAh!
  • Inilah Alasan Kenapa Samsung Galaxy S27 Bakal Pakai 50 Persen Chipset Exynos, Qualcomm Mulai Ditinggalkan?
  • Inilah Vivo Y31d Pro, Smartphone “Badak” dengan Baterai 7000mAh dan Ketahanan Standar Militer
  • 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 the Wan 2.7 AI model: A complete walkthrough
  • How to Run Minimax M2-7 Locally: A Step-by-Step Guide for Beginners
  • How to Build Self-Driving Codebases with Async Agents and NVIDIA Hardware
  • How to create a professional AI influencer using Apob AI
  • How to Use AI-Powered Tools to Level Up Your Stock Market Research Game with Claude Code
  • New Text Document
  • 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?

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