SDL_strndup(3)

πŸ’– SDL_strndup(3) β€” it's giving "copy that string but like, only n chars bestie" πŸ’–

part of the SDL3 FUNCTIONS squad ✨


🌸 NAME 🌸

SDL_strndup</purgle> β€” allocate a copy of a string, up to n characters. no cap, that's literally it πŸŽ€


✨ SYNOPSIS ✨

#include <SDL3/SDL_stdinc.h>

char * SDL_strndup(const char *str, size_t maxlen);

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so bestie, here's the vibe: this function grabs enough space for a null-terminated copy of str, up to maxlen bytes, using SDL_malloc, and then it just… makes a copy of the string into that space. iconic behavior fr fr πŸ’…

if the string is longer than maxlen bytes, the returned string will be maxlen bytes long, plus a null-terminator character that isn't included in the count. the way it just sneaks that extra byte in there 😭

the returned string is owned by the caller πŸ‘‘, and should be passed to SDL_free when no longer needed. do not ghost your malloc'd memory, that's so not main character energy.

🫢 reminder: you own it, you free it. SDL_free is the breakup text you actually have to send.

πŸŽ€ FUNCTION PARAMETERS πŸŽ€

  • str β€” the string to copy. the source material, the blueprint, the moment 🌟
  • maxlen β€” the maximum length of the copied string, not counting the null-terminator character. so like, the vibes limit, but the null byte is free πŸ“

🌈 RETURN VALUE 🌈

returns a pointer to the newly-allocated string. fresh, new, yours. slay ✨


πŸ’œ THREAD SAFETY πŸ’œ

it is safe to call this function from any thread. multitask, multi-thread, multi-vibe. no drama, no race conditions, we love to see it 🧚


⭐ AVAILABILITY ⭐

this function is available since SDL 3.2.0. if you're on something older… bestie, it's giving vintage, but not in the cute way πŸ™ˆ


🌸 SEE ALSO 🌸

nothing else listed here β€” it's a solo act, and honestly? main character energy πŸ‘‘


okay that's the whole page, mwah πŸ’‹

this lil page comes to you from Simple Directmedia Layer, running SDL 3.4.18, and yes it's an SDL_strndup(3) situation. go forth and copy strings responsibly, bestie πŸ¦‹πŸ’–πŸŽ€

πŸ“¦ where this page comes from