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

  • Tertipu Saldo Palsu? Apakah Game Layer Drop Penipu?
  • Inilah Syarat Terbaru dan Cara Daftar Bansos PKH-BPNT 2026 Lewat HP!
  • Inilah Trik Hubungkan Telegram ke WaIDN Biar Saldo Ngalir Terus!
  • Caranya Mengatasi Kode Verifikasi PayPal yang Nggak Pernah Nyampe di HP
  • Inilah Cara Cek Pencairan KJP Plus Januari 2026 Biar Nggak Bingung Lagi
  • Inilah Cara Cek Dana PIP yang Cair Senin 19 Januari 2026 Lewat HP!
  • Ingin Kuliah Gratis di 2026? Ini Cara Daftar KIP Kuliah via HP dan Syarat Lengkapnya!
  • Inilah Cara Cek Status KIS Bansos Aktif Secara Instan Lewat Smartphone Kamu!
  • Inilah Cara Cek NIK KTP Penerima Bansos PKH dan BPNT Januari 2026 yang Paling Gampang!
  • Ini Trik Ampuh Mengatasi Kode Verifikasi PayPal yang Nggak Kunjung Masuk!
  • Sering Gagal Transaksi? Ini Cara Mengatasi Kode FP2769 di BRImo yang Bikin Pusing!
  • Layar Oppo Muncul Error Hubungan Baterai? Gini Cara Beresinnya Sampai Tuntas!
  • Cara Munculin Menu Hadiah Melolo di iPhone
  • Inilah Cara Main Melolo Drama Biar Gak Boros Kuota dan Saldo Cair Terus!
  • Ini Trik Rahasia Cara Memunculkan Potongan Harga TikTok Shop yang Nggak Muncul di Akun Kalian!
  • Threads Ternyata Sudah Lebih Rame dari X di Android
  • Bocoran Terbaru Pixel 10a: Tanggal Rilis Lebih Awal dan Harganya Nggak Jadi Naik?
  • Inilah Cara Main Aplikasi Layla Biar Dapat Teman dan Cuan Sekaligus!
  • Apa itu Apple Creator Studio?
  • Inilah Alasan Kenapa Tidak Bisa Melihat Status WA Padahal Tidak Diprivasi dan Trik Mengatasinya!
  • Lupa Email Akun Higgs Domino? Ini Cara Mengatasinya
  • Apa itu WhatsApp Aero? Aman atau Tidak + Cara Downloadnya
  • Inilah Kenapa Paket JNE Muncul Status Nobody At Home dan Cara Mengatasinya Biar Nggak Panik!
  • Gagal Aktivasi BSI Mobile? Inilah Arti Pesan Error 53 Saving Account Not Registered dan Solusinya
  • Cara Cuan dari Hobi Baca Novel/Komik Online
  • Hp Vivo Kalian Muncul Notif Data Spasial Sistem Rusak? Begini Trik Mengatasinya Sampai Tuntas!
  • Cara Buat Link Ujian Mencintai Diam-Diam Google Form, Tes Seberapa Besar Perasaan Kalian ke Crush!
  • Ini Penjelasan Mengenai Cara Mengubah Dosa Menjadi Diamond Game FF ML dan Saldo Shopeepay yang Sedang Viral
  • Trik Supaya Bisa Dapat Potongan Harga Rp100 di TikTok Tanpa Harus Reset HP!
  • Cara Input Bantuan IFP dan Laptop di Dapodik 2026.B, Aset Sekolah Aman
  • Apa Itu ErrTraffic? Mengenal Platform ClickFix yang Bikin Website Jadi ‘Error’ Palsu
  • What is Reflex Framework? A Full-stack Python Framework
  • CloudFlare Acquired AstroJS!
  • How to Completely Remove AI Features from Windows 11 Explained
  • How to AI Fine-Tuning with a New Red Hat’s New Modular Tools
  • Cara Menggunakan SAM3D untuk Segmentasi dan Pembuatan Model 3D dari Teks
  • Cara Membuat AI Agent Super Cerdas dengan DeepAgents dan LangGraph
  • Perbedaan GPU vs TPU, Mana yang Terbaik
  • Tutorial Langfuse: Pantau & Optimasi Aplikasi LLM
  • Begini Teknik KV Caching dan Hemat Memori GPU saat Menjalankan LLM
  • Apa Itu ErrTraffic? Mengenal Platform ClickFix yang Bikin Website Jadi ‘Error’ Palsu
  • Ini Kronologi Hacking ESA (European Space Agency) 2025
  • Apa itu Zoom Stealer? Ini Definisi dan Bahaya Tersembunyi di Balik Ekstensi Browser Kalian
  • Apa itu Skandal BlackCat Ransomware?
  • Apa itu ToneShell? Backdoor atau Malware Biasa?
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