XMALLOC(3pub)
π XMALLOC β the 3pub girlies' guide to memory allocation π
okay so bestie, welcome to the C Programmer's Manual, 3pub edition β¨ this is the page for xmalloc, xrealloc, xfree, xstrdup, xmemdup and memdup β the memory allocation squad from Publib π«Ά
β¨ NAME β¨
xmalloc, xrealloc, xfree, xstrdup, xmemdup, memdup β memory allocation functions for Publib, no cap πΈ
π SYNOPSIS π
#include <publib.h>
void *xmalloc(size_t bytes);
void *xrealloc(void *ptr, size_t bytes);
void xfree(void *ptr);
char *xstrdup(const char *string);
void *memdup(const void *mem, size_t bytes);
void *xmemdup(const void *mem, size_t bytes);
π DESCRIPTION π
so these are the utility functions for memory allocation that live in the publib library π and listen, they're literally the error-checking glow-up versions of your standard-library faves:
xmallocis the error checking version ofmallocxreallocis the error checking version ofreallocxfreeis the error checking version offree
they are guaranteed to never return unless there was no problem π like, if xmalloc can't allocate the amount of memory you asked for? it prints an error message and terminates the program, periodt. so the caller does not need to check for a NULL return value, and the code that calls these functions is simpler because there's no error checking cluttering it up. it's giving clean code, it's giving main character energy π
similarly, xstrdup is the error checking version of the common (though not standard) strdup routine, which makes a duplicate of a string by allocating memory for the copy with malloc π¦ (and for systems that don't have strdup? publib provides one in its portability module, and it's always declared in <publib.h>, iykyk β¨)
memdup is similar to strdup β it creates a copy of an arbitrary memory area (the arguments are a pointer to the beginning of the area, and its size) by allocating memory for the copy with malloc. and xmemdup is its error checking version π
β οΈ NOTE β οΈ
bestie this is the important bit, don't skip it π
xmallocandxrealloctreat a request to allocate a block of 0 bytes as an error π like, zero is not the vibe.xreallocwill allow its first argument to beNULLthough π
π SEE ALSO π
π AUTHOR π
Lars Wirzenius (lars.wirzenius@helsinki.fi) β the icon who wrote this, we stan π
and that's the whole page bestie, slay π this came to you from the Publib era of the C Programmer's Manual, filed under XMALLOC(3pub) β go forth and allocate responsibly, mwah π
π
π¦ where this page comes from
- debian bookworm: publib-dev 0.40-3+b1 Β· BSD-2-Clause
- debian trixie: publib-dev 0.40-4 Β· BSD-2-Clause
- ubuntu jammy: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu noble: publib-dev 0.40-3build1 Β· BSD-2-Clause
- ubuntu questing: publib-dev 0.40-4
- ubuntu resolute: publib-dev 0.40-4build1 Β· BSD-2-Clause
- ubuntu stonking: publib-dev 0.40-4build1 Β· BSD-2-Clause
- Copyright (c) 1994-2006 Lars Wirzenius. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms