XtMalloc(3)
π XtMalloc(3) β XT FUNCTIONS π
okay bestie, gather round, because today we're talking about the Xt memory management functions and honestly? they're the backbone of your whole X Toolkit life. this is section 3, XtMalloc, and it's giving "i will allocate your memory and i will not let you down." π«Ά
β¨ NAME β¨
XtMalloc, XtCalloc, XtRealloc, XtReallocArray, XtFree, XtNew, XtNewString β memory management functions. yes queen, the whole squad is here. π
πΈ SYNTAX πΈ
first things first, you gotta bring in the header or none of this works:
#include <X11/Intrinsic.h>
and now the cast, in order, no skips:
char *XtMalloc(Cardinal size);
char *XtCalloc(Cardinal num, Cardinal size);
char *XtRealloc(char *ptr, Cardinal size);
void *XtReallocArray(void *ptr, Cardinal num, Cardinal size);
void XtFree(char *ptr);
type *XtNew(type);
String XtNewString(String string);
Cardinal XtAsprintf(char **new_string, const char *format, ...);
not me listing every single one because every single one matters. π
π ARGUMENTS π
numβ the number of array elements. how many lil guys you want in your array. π’ptrβ a pointer to the old storage, or to the block of storage that's about to get freed. either the old thing or the doomed thing, iykyk. πsizeβ the size of an array element (in bytes), or the number of bytes you want. units matter bestie, don't come crying to me about bytes vs elements. πstringβ a previously declared string. you already made it, now you're handing it over. π§΅typeβ a previously declared data type. yes, the type itself. iconic. βnew_stringβ a pointer to write a newly allocated string to. that's where the baby string goes. πΌformatβ a formatting string as defined bysprintf(3c). the classic. π
π DESCRIPTION π
XtMalloc π¦
XtMalloc returns a pointer to a block of storage of at least the specified size bytes. at least! it's generous. and if there is insufficient memory to allocate the new block, XtMalloc calls XtErrorMsg. no silent failure here, she tells on you. π
XtCalloc πΈ
XtCalloc allocates space for the specified number of array elements of the specified size and initializes the space to zero. zeroed out, fresh, clean, no mystery garbage bytes. and if there is insufficient memory to allocate the new block, XtCalloc calls XtErrorMsg. periodt.
XtRealloc and XtReallocArray π₯
XtRealloc and XtReallocArray change the size of a block of storage (possibly moving it β yes, moving it, watch your pointers bestie). then they copy the old contents (or as much as will fit) into the new block and free the old block. glow-up complete. β¨
if there is insufficient memory to allocate the new block, or the calculations for the size of the new block would cause an integer overflow, these functions call XtErrorMsg. integer overflow is not a vibe, and they will not let it slide.
if ptr is NULL, these functions allocate the new storage without copying the old contents β that is, they simply call XtMalloc. fresh start energy. π±
XtFree π
XtFree returns storage and allows it to be reused. recycling! sustainability! if ptr is NULL, XtFree returns immediately. like, instantly. no drama. π«Ά
XtNew π
XtNew returns a pointer to the allocated storage. if there is insufficient memory to allocate the new block, XtNew calls XtErrorMsg. XtNew is a convenience macro that calls XtMalloc with the following arguments specified:
((type *) XtMalloc((unsigned) sizeof(type))
(keep that exactly as written, character for character, no "fixing" the parens, i'm begging π)
XtNewString π§
XtNewString returns a pointer to a new string which is a duplicate of string. if there is insufficient memory to allocate the new block, or the argument is NULL, XtNewString returns NULL. the memory can be freed with XtFree. dupe it, use it, free it, move on. π
XtAsprintf π
XtAsprintf allocates space for a string large enough to hold the string specified by the sprintf(3c) format pattern when used with the remaining arguments, and fills it with the formatted results.
the address of the allocated string is placed into the pointer passed as ret.
the length of the string (not including the terminating null byte) is returned.
if there is insufficient memory to allocate the new block, XtAsprintf calls XtErrorMsg.
β οΈ heads up: the pointer it writes into is described in the prose asret, while the argument list calls itnew_string. same energy, but keep an eye on which name you're reading. don't @ me, that's literally what the page says. π
π SEE ALSO π
- X Toolkit Intrinsics - C Language Interface
- Xlib - C Language X Interface
go read those if you want the full lore. iykyk. π
and that's the whole page, bestie!! π every function, every argument, every overflow warning, all present and accounted for β no cap. this one's from X Version 11, libXt 1.3.1, and it's dated with the classic XtMalloc(3) page footer. now go XtMalloc responsibly and always, always XtFree your blocks. slay πβ¨π
π¦ where this page comes from
- alpine 3.22: libxt-doc 1.3.1-r0 Β· MIT
- alpine 3.23: libxt-doc 1.3.1-r0 Β· MIT
- alpine 3.24: libxt-doc 1.3.1-r0 Β· MIT
- arch rolling: libxt 1.3.1-1 Β· MIT AND HPND-sell-variant AND SMLNJ AND MIT-open-group AND X11
- fedora 43: libXt-devel 1.3.1-3.fc43 Β· MIT AND HPND-sell-variant AND SMLNJ AND MIT-open-group AND X11
- fedora 43: libXt-devel 1.3.1-3.fc43 Β· MIT AND HPND-sell-variant AND SMLNJ AND MIT-open-group AND X11
- fedora 44: libXt-devel 1.3.1-4.fc44 Β· MIT AND HPND-sell-variant AND SMLNJ AND MIT-open-group AND X11
- fedora 44: libXt-devel 1.3.1-4.fc44 Β· MIT AND HPND-sell-variant AND SMLNJ AND MIT-open-group AND X11
- opensuse-leap 16.0: libXt-devel 1.3.1-160000.3.2 Β· MIT
- opensuse-leap 16.0: libXt-devel 1.3.1-160000.3.2 Β· MIT
- opensuse-leap 16.0: libXt-devel 1.3.1-160000.3.2 Β· MIT
- opensuse-leap 16.0: libXt-devel 1.3.1-160000.3.2 Β· MIT
- Copyright 1993 X Consortium
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms