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.

#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);

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!)

#include <glib.h>
g_new(type, count); 
g_new0(type, count); 
g_renew(type, mem, count);

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

Terbaru

  • Inilah Cara Mengatur Raspberry Pi 5 dengan Ubuntu Server untuk Python dan Desktop GUI Tanpa Ribet
  • Inilah Alasan Kenapa Galaxy Z Fold 8 Ultra Bisa Jadi Produk yang Mengecewakan
  • Inilah Alasan Intel Merilis Raptor Lake Next di Socket LGA 1700, Masih Setia dengan DDR4!
  • Gini Caranya Menghilangkan Recycle Bin dari Desktop Windows 11 Supaya Lebih Bersih!
  • Inilah Huawei AirEngine 8771-X1T, Solusi Wi-Fi 7 Super Cepat untuk Bisnis Masa Kini
  • Inilah Cara Mengatasi Error Koneksi VMware Horizon Akibat Intersepsi SSL Proxy
  • Inilah Cara Mengatasi Connection Server Authentication Failed di VMware Horizon Client
  • Cara Laptop Nggak Lemot Pas Colok SD Card, Gampang Banget!
  • Inilah Caranya Mengatasi SD Card Reader yang Tidak Terbaca di Laptop
  • Inilah Cara Ampuh Atasi Perangkat USB yang Sering Terputus di Windows 10 dan 11
  • Cara Atasi USB Error dengan Update USB Root Hub dan Chipset Driver
  • Inilah Cara Mengatasi Unknown USB Device Descriptor Request Failed yang Paling Ampuh
  • Inilah 20 Kampus Swasta Terbaik di Bandung Versi EduRank 2026 untuk Referensi Kuliah Kalian
  • Inilah Syarat dan Cara Daftar Sekolah Kedinasan STPN 2026, Kuota Terbatas!
  • Inilah Cara Daftar PPKB UI 2026 Lengkap dengan Rincian Uang Pangkal Semua Jurusan S1
  • Inilah Aturan Resmi MPLS 2026 dari Kemendikdasmen, Guru dan Sekolah Wajib Catat Pedoman Lengkap Ini!
  • Inilah Cara Daftar Beasiswa S1/D4 Guru Kemendikdasmen 2026, Masa Pendaftaran Diperpanjang!
  • Inilah Cara Mengatasi Unknown USB Device (Device Descriptor Request Failed) dan Penjelasan Lengkapnya
  • Inilah Cara Membuat File Koneksi RDP Secara Manual Biar Akses Remote Kalian Nggak Error Lagi
  • Inilah Cara Clear RDP Cache dan Registry MRU Biar Remote Desktop Kalian Kembali Segar
  • Cara Restore File Association .rdp Agar Remote Desktop Bisa Terbuka Otomatis Lagi
  • Apa itu Probabilistic Methods dalam Klasifikasi Data?
  • Apa itu Klasifikasi Data dengan Metode Feature Selection?
  • Inilah Panduan Lengkap Jalur Afirmasi Disabilitas SPMB Kota Malang 2026, Simak Syarat dan Jadwalnya!
  • Inilah Cara Lengkap Daftar UM Undip 2026: Panduan Teknis, Jadwal, dan Syarat Biar Nggak Salah Langkah!
  • Inilah Daftar Kampus Swasta Terbaik di Indonesia 2026 Versi Webometrics dan QS WUR, Nggak Kalah Sama Negeri!
  • Inilah Cara Daftar PPKB UI 2026, Kesempatan Emas Masuk Kampus Jaket Kuning Tanpa Tes!
  • Inilah Tampilan Baru Aplikasi Cek Bansos Kemensos 2026, Cara Cek Status dan Nominal Bantuan yang Cair!
  • Inilah Aturan PIN SPMB Jatim 2026, Bisa Dipakai Berapa Kali Sih?
  • Apa itu Common Techniques in Data Classification?
  • Auditd Custom Rules & Tips
  • Securing SSH Server with fail2ban
  • Fedora Linux Firewalld Drop Zone and Rich Rules
  • How to SSH Hardening 2026
  • How to Add Password Protection to GRUB
  • How to Automate Your Entire SEO Strategy Using a Swarm of 100 Free AI Agents Working in Parallel
  • How to create professional presentations easily using NotebookLM’s AI power for school projects and beyond
  • How to Master SEO Automation with Google Gemini 3.1 Flash-Lite in Google AI Studio
  • How to create viral AI video ads and complete brand assets using the Claude and Higgsfield MCP integration
  • How to Transform Your Mac Into a Supercharged AI Assistant with Perplexity Personal Computer
  • Inilah Update Pasar Saham AS 31 Mei 2026: Menakar Peluang S&P 500 dan Nasib Sektor Teknologi Saat Inflasi Belum Jinak
  • Sinyal Update Kondisi Pasar IHSG 31 Mei 2026: Strategi Cerdas Menghadapi Gejolak IHSG dan Rupiah di Awal Juni
  • Inilah Alasan Ilmiah Kenapa Kita Menguap, Ternyata Bukan Cuma Kurang Oksigen!
  • Inilah Alasan China Larang PR Berlebihan dan Ujian Berat, Ternyata Demi Kesehatan Mental Siswa!
  • Inilah Cara Cek Peluang Lolos SNBT Unair 2026 dan Daftar Lengkap Daya Tampungnya

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