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

  • Tips Pajak Coretax: Inilah Cara Memastikan Lembaga Amil Zakat yang Sah Agar Pajak Kalian Berkurang!
  • Kenapa FreeFire Advance Server Tidak Bisa Diunduh? Ini Penjelasannya!
  • Inilah Realita Biaya Hidup Mahasiswa di Bogor: Ternyata Nggak Semahal yang Kalian Kira!
  • Inilah Cara Blokir Email Spam di Gmail Biar Penyimpanan Nggak Gampang Penuh
  • Inilah Cara Aktivasi Keaktifan PTK di EMIS GTK IMP 2026 Biar Tunjangan Cair Lancar!
  • Inilah Cara Menilai Sumbangan yang Disetarakan dengan Uang Supaya Pajak Kalian Berkurang
  • Apa itu Pin di iMessage?
  • SKTP Nggak Muncul di Info GTK padahal Sudah Terbit? Ini Trik Rahasia Biar Data Langsung Update!
  • Ini Trik Nuyul Cari Cuan di Game Puzzle Farm 2026 Biar Koin Melimpah Tanpa Undang Teman
  • Inilah Ukuran Kertas Thermal 58mm ISO Di Word, Berapa dan Panduan Lengkap Memilihnya
  • Bukan Cuma Zakat! Ternyata Sumbangan Jenis Ini Bisa Ngurangin Pajak Kalian! Simak Penjelasannya
  • Inilah Caranya Mengajar Bahasa Indonesia di Amerika Serikat Lewat Beasiswa Fulbright FLTA 2026
  • Inilah 6 Rekomendasi HP yang Awet dan Tahan Lama Biar Kalian Nggak Gonta-ganti Terus!
  • Apa itu Proses BOP dan Psikotes BRI Life?
  • Ini Cara Input Tugas Tambahan Guru di EMIS GTK IMP 2026 Biar Jam Mengajar Aman!
  • APK Juice Pack Frenzy Penipuan? Benarkah Membayar atau Cuma Tipuan Iklan? Ini Faktanya!
  • Apakah Apk ReelAct Penipu? Mau Tarik 100 Dolar dari Reel Act? Cek Dulu Faktanya Biar Nggak Rugi Waktu!
  • Inilah Rekomendasi Game Turn Base Android dan PC Terbaik Buat Kalian yang Suka Strategi!
  • Inilah Cara Membuat Sertifikat di Canva dan Ukuran Standar yang Wajib Kalian Tahu
  • Inilah Aturan Zakat yang Bisa Jadi Pengurang Pajak Bruto Kalian, Sudah Tahu Belum?
  • Inilah Data Pendaftar KIP Kuliah 2026 Jalur SNBP dan Bocoran Kriteria yang Lolos!
  • Inilah Game Silent Hill: Townfall, Teror Psikologis Baru yang Bakal Bikin Kalian Gemetar di Tahun 2026!
  • Inilah Trailer Mortal Kombat 2, Johnny Cage Resmi Gabung dan Siap Hadapi Shao Kahn!
  • Inilah Spesifikasi Lengkap Samsung Galaxy S26 yang Baru Meluncur, Ternyata Harganya Naik Segini!
  • Inilah Cara Mematikan MSA Xiaomi Supaya HP Nggak Lemot dan Bebas Iklan, Ternyata Langkahnya Simpel Banget!
  • Inilah Kronologi Mobil Calya Plat D yang Viral Lawan Arus di Jakpus, Ternyata Bawa Banyak Plat Palsu!
  • Inilah Axioo Hype AI 5, Laptop AI Canggih yang Nggak Bikin Kantong Bolong!
  • Ini Loh Kejahatan Modus Phishing Google Tasks Terbaru yang Bisa Nguras Data Perusahaan Kalian
  • Inilah Poco X7 5G dan M7 Pro 5G, HP Gaming Performa Ekstrem yang Ngebikin Mabar Kalian Jadi Anti Lag!
  • Inilah Cara Mengubah Lahan Kosong Jadi Uang Lewat Strategi Land Banking
  • Aerynos Feb 2026 Update: Faster Desktops and Moss Performance Boost
  • Pangolin 1.16 Adds SSH Auth Daemon: What You Need to Know
  •  How to Fix Windows Audio Endpoint Builder Service Not Starting Errors
  • What’s New in elementary OS 8.1.1 with Linux Kernel 6.17?
  • Microsoft Tests AI Feature to Monitor Open Apps on Windows 11 Taskbar
  • 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 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