Commit f7c922ed authored by Murilo Opsfelder Araujo's avatar Murilo Opsfelder Araujo Committed by Michael Tokarev

HACKING: document preference for g_new instead of g_malloc

This patch documents the preference for g_new instead of g_malloc. The
reasons were adapted from commit b45c03f5.

Discussion in QEMU's mailing list:

Cc: David Hildenbrand <>
Cc: Eduardo Habkost <>
Cc: Markus Armbruster <>
Cc: Paolo Bonzini <>
Signed-off-by: default avatarMurilo Opsfelder Araujo <>
Reviewed-by: default avatarEric Blake <>
Reviewed-by: default avatarAlex Bennée <>
Reviewed-by: default avatarDavid Hildenbrand <>
Signed-off-by: default avatarMichael Tokarev <>
parent 0b816e98
......@@ -118,6 +118,15 @@ Please note that g_malloc will exit on allocation failure, so there
is no need to test for failure (as you would have to with malloc).
Calling g_malloc with a zero size is valid and will return NULL.
Prefer g_new(T, n) instead of g_malloc(sizeof(T) * n) for the following
a. It catches multiplication overflowing size_t;
b. It returns T * instead of void *, letting compiler catch more type
Declarations like T *v = g_malloc(sizeof(*v)) are acceptable, though.
Memory allocated by qemu_memalign or qemu_blockalign must be freed with
qemu_vfree, since breaking this will cause problems on Win32.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment