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

  • Bingung Pilih Paket? Inilah Perbedaan Telkomsel Data dan Telkomsel Data Flash yang Wajib Kalian Tahu!
  • Ini Alasan Pohon adalah Mahluk Hidup Terbesar di Dunia
  • Sempat Panas! Kronologi Perseteruan Cak Ji vs Madas di Surabaya, Gini Endingnya
  • Gila! Norwegia Bikin Terowongan Melayang di Bawah Laut
  • Cuma Terpisah 20 Mil, Kenapa Hewan di Bali dan Lombok Beda Total? Ternyata Ini Alasannya
  • Heboh Video Umari Viral 7 Menit 11 Detik dari Pakistan, Isinya Beneran Ada atau Cuma Jebakan Link? Cek Faktanya!
  • Tertipu Online atau HP Hilang? Ini Cara Melacak Nomor HP yang Nggak Tipu-Tipu!
  • Apakah Aplikasi Labora Bisa Hasilkan Uang Jutaan atau Cuma Tipu-Tipu?
  • Apakah APK Cashcash Pro Penipu? Cek Reviewnya Dulu
  • Google Kliks Communicator: Pengalaman Awal yang Mengejutkan – Apa yang Kami Rasakan?
  • Belum Tahu? Ini Ukuran Banner YouTube yang Pas Biar Channel Kalian Makin Kece!
  • Inilah Rencana Gila China Balap AS ke Bulan Sebelum 2030
  • Inilah Spesifikasi Resmi Macbook Pro M5 yang Masuk Indonesia
  • Nonton Drama Bisa Dapat Cuan? Simak Cara Main Yudia APK Biar Nggak Salah Langkah!
  • Cuma Nonton Drama Pendek Bisa Cair Saldo DANA? Cobain Aplikasi Melolo, Begini Caranya!
  • Nonton Drama Bisa Dapat Cuan? Jangan Senang Dulu, Simak Fakta Mengejutkan Aplikasi Cash Drama Ini!
  • Ini Video Asli Bocil Block Blast 1 VS 3 di TikTok, Awas Jangan Asal Klik Link Sembarangan!
  • Instapop Terbukti Membayar atau Cuma Tipuan? Cek Faktanya Sebelum Kalian Buang Waktu Main Game!
  • Geger Video Botol Coca Cola 24 Detik di TikTok, Jangan Asal Klik! Ini Fakta Sebenarnya
  • Apa itu Video Botol Makarizo Viral? Ini Faktanya
  • Apa itu Merge Fruit Master? Ini Pengertian dan Cara Main Game yang Katanya Bisa Menghasilkan Saldo
  • Apa itu Shortbox? Ini Pengertian dan Cara Menghasilkan Uang di Aplikasinya
  • Masih Bingung Bedanya CRM dan ERP? Ini Tips Cari Software CRM dan ERP Terbaik
  • Begini Cara Hapus Akun DANA Premium Secara Permanen!
  • Bukan Cuma Mitos! Ternyata Dinasti Song Punya Tempat Nongkrong Mirip PIK, Begini Isinya
  • Bukan Fiksi! Ini Bukti Manusia Punya ‘Kekuatan Super’ Akibat Mutasi Genetik dan Lingkungan
  • Dikelilingi Cincin Api, Kok Kalimantan Malah Nggak Punya Gunung Berapi? Ini Jawabannya!
  • Misteri Newgrange: Kuburan Batu 5.000 Tahun yang Menyimpan Peta Galaksi, Kok Bisa?
  • Transformasi Ekstrem Pesawat IL-96: Ganti 4 Mesin Jadi 2 Mesin Raksasa PD-35, Emang Bisa?
  • Apakah Aplikasi Cash Reels Penipu?
  • Apa itu Cosmic Desktop: Pengertian dan Cara Pasangnya di Ubuntu 26.04?
  • Apa Itu Auvidea X242? Pengertian Carrier Board Jetson T5000 dengan Dual 10Gbe
  • Elementary OS 8.1 Resmi Rilis: Kini Pakai Wayland Secara Standar!
  • Apa Itu Raspberry Pi Imager? Pengertian dan Pembaruan Versi 2.0.3 yang Wajib Kalian Tahu
  • Performa Maksimal! Ini Cara Manual Update Ubuntu ke Linux Kernel 6.18 LTS
  • Inilah Cara Melatih AI Agent Agar Bisa Belajar Sendiri Menggunakan Microsoft Agent Lightning
  • Tutorial Optimasi LangGraph dengan Node-Level Caching untuk Performa Lebih Cepat
  • Tutorial Membuat Game Dengan LangChain
  • X Terancam Sanksi Eropa Gara-Gara AI Grok Bikin Deepfake Anak Kecil
  • Nano Banana 2 Flash Itu Apa Sih? Ini Penjelasan Lengkapnya
  • Inilah Risiko Fatal yang Mengintai Kreator OnlyFans, Dari Doxxing sampai Penipuan!
  • Apa itu Kerentanan FortiCloud SSO? Ini Pengertian dan Bahayanya
  • Apa itu Covenant Health Data Breach? Ini Pengertian dan Kronologi Lengkapnya
  • Apa Itu Integrasi Criminal IP dan Cortex XSOAR? Ini Definisinya
  • Gawat! VVS Stealer Cuma Seharga Makan Siang Tapi Bisa Habisi Akun Discord Kalian
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