6e58c12752
This macros have several benefits over the standard functions: - The type of the allocated object (not the pointer) is specified as an argument, which improves readability: - It is directly obvious what is the type of the object just by reading the macro call. - It allows grepping for all allocations of a given type. This is admittedly similar to using sizeof() to get the size of the object, but we'll see why this is better. - In the case of reallocation macros, an extra check is performed to make sure that the previous pointer was compatible with the allocated type, which can avoid some mistakes. - The cast is performed automatically, with a pointer type derived from the type of the object. This is the best point of this macro, since it does an automatic cast, where there's no chance of typos. Usually, programmers have to decide whether to cast or not the result of malloc(3). Casts usually hide warnings, so are to be avoided. However, these functions already return a void *, so a cast doesn't really add much danger. Moreover, a cast can even add warnings in this exceptional case, if the type of the cast is different than the type of the assigned pointer. Performing a manual cast is still not perfect, since there are chances that a mistake will be done, and even ignoring accidents, they clutter code, hurting readability. And now we have a cast that is synced with sizeof. - Whenever the type of the object changes, since we perform an explicit cast to the old type, there will be a warning due to type mismatch in the assignment, so we'll be able to see all lines that are affected by such a change. This is especially important, since changing the type of a variable and missing to update an allocation call far away from the declaration is easy, and the consequences can be quite bad. Cc: Valentin V. Bartenev <vbartenev@gmail.com> Signed-off-by: Alejandro Colomar <alx@kernel.org> |
||
---|---|---|
.. | ||
.indent.pro | ||
alloc.h | ||
bit.h | ||
commonio.c | ||
commonio.h | ||
defines.h | ||
encrypt.c | ||
exitcodes.h | ||
faillog.h | ||
fields.c | ||
fputsx.c | ||
get_gid.c | ||
get_pid.c | ||
get_uid.c | ||
getdef.c | ||
getdef.h | ||
getlong.c | ||
getulong.c | ||
groupio.c | ||
groupio.h | ||
groupmem.c | ||
gshadow_.h | ||
gshadow.c | ||
lockpw.c | ||
Makefile.am | ||
mempcpy.h | ||
nscd.c | ||
nscd.h | ||
nss.c | ||
pam_defs.h | ||
port.c | ||
port.h | ||
prototypes.h | ||
pwauth.c | ||
pwauth.h | ||
pwio.c | ||
pwio.h | ||
pwmem.c | ||
run_part.c | ||
run_part.h | ||
selinux.c | ||
semanage.c | ||
sgetgrent.c | ||
sgetpwent.c | ||
sgetspent.c | ||
sgroupio.c | ||
sgroupio.h | ||
shadow.c | ||
shadowio.c | ||
shadowio.h | ||
shadowlog_internal.h | ||
shadowlog.c | ||
shadowlog.h | ||
shadowmem.c | ||
spawn.c | ||
sssd.c | ||
sssd.h | ||
stpecpy.h | ||
stpeprintf.h | ||
subordinateio.c | ||
subordinateio.h | ||
tcbfuncs.c | ||
tcbfuncs.h |