al_cstr(3alleg5)
π al_cstr β the Allegro 5 API, bestie edition π
okay so this is al_cstr from the Allegro reference manual, and it lives in section 3alleg5 of the manual pages. it's short, it's sweet, it's lowkey iconic, and it has opinions about NUL terminators. let's get into it π«Ά
β¨ NAME β¨
al_cstr β Allegro 5 API
πΈ SYNOPSIS πΈ
#include <allegro5/allegro.h>
const char *al_cstr(const ALLEGRO_USTR *us)
π¦ DESCRIPTION π¦
so the whole vibe here is: you hand it an ALLEGRO_USTR * and it gives you back a const char * pointer to the data inside that string. slay. but β and this is the part where you need to pay attention, bestie β that pointer is only valid while the ALLEGRO_USTR(3alleg5) object is not modified and not destroyed. it's giving "borrowed, not given". it is not yours. do not get attached. π
you can pass that pointer to functions that expect C-style strings, but there are caveats, and they are not optional reading:
- ALLEGRO_USTRs are allowed to contain embedded NUL (
'\0') bytes. that meansal_ustr_size(u)andstrlen(al_cstr(u))may not agree. like, they can genuinely just disagree with each other. not a bug, that's the design. π - an
ALLEGRO_USTRmay be created in such a way that it is not NUL terminated. a string which is dynamically allocated will always be NUL terminated, but a string which references the middle of another string or region of memory will not be NUL terminated. so likeβ¦ read the room before youstrlenit. π - if the
ALLEGRO_USTRreferences another string, the returned C string will point into the referenced string. and again β no NUL terminator will be added to the referenced string. it doesn't sneak one in. it doesn't help you out. it just points. periodt.
π real talk: the pointer is only valid while the object is unmodified and undestroyed. modify it or destroy it and your pointer is giving ghost. don't be out here using stale pointers, bestie, that's not main character energy, that's undefined behaviour energy.
π SEE ALSO π
okay that's the whole page, you ate it, go forth and be normal about NUL terminators π β¨
with love, the Allegro reference manual β al_cstr(3alleg5) π¦π
π¦ where this page comes from
- debian trixie: allegro5-doc 2:5.2.10.1+dfsg-1 Β· Zlib
- ubuntu questing: allegro5-doc 2:5.2.10.1+dfsg-1build1
- ubuntu resolute: allegro5-doc 2:5.2.11.3+dfsg-1 Β· Zlib
- ubuntu stonking: allegro5-doc 2:5.2.11.3+dfsg-1 Β· Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms