ustrzcpy(3alleg4)

πŸ’– ustrzcpy β€” bestie edition πŸ’–

ok so real talk, this one's from the Allegro game programming library, manual section 3alleg4, and it's the string-copying function that actually respects your boundaries. iconic behaviour tbh.


✨ SYNOPSIS ✨

#include <allegro.h>

char *ustrzcpy(char *dest, int size, const char *src);

like yes she's one line of C, she's lowkey doing the most πŸ’…


🌸 DESCRIPTION 🌸

okay so ustrzcpy copies src β€” and yes, including the terminating null character, she brings her whole self β€” into dest. and you tell her how many bytes dest has via size, and here's the slay: dest is guaranteed to be null-terminated even if src is bigger than size. no cap. she will not leave you hanging with a runaway string 🫢

⚠️ but bestie. listen. even for empty strings, your destination string has to have at least enough bytes to store that terminating null character, and your size parameter must reflect that. don't be cute about it.

and if you ignore me:

  • the debug version of Allegro will abort at an assertion 😭 (she's dramatic but she's honest)
  • the release version of Allegro will overrun the destination buffer πŸ’€ (that's the bad one, that's the undefined-behaviour one, do not)

πŸŽ€ RETURN VALUE πŸŽ€

returns the value of dest. that's it. she gives you back what you gave her. main character energy honestly πŸ‘‘


πŸ¦‹ SEE ALSO πŸ¦‹


mwah πŸ’œ that's the whole page. this was the Allegro manual, version 4.4.3, and yes she's old but she ate. go write some safe string copies, bestie, i'm obsessed with you πŸŒˆβœ¨πŸŽ€