SDL_malloc(3)

πŸ’– SDL_malloc β€” the "gimme some memory bestie" function πŸ’–

okay so real talk, this is the <SDL3/SDL_stdinc.h> girlie from the SDL3 FUNCTIONS squad, and she's here to hand you a chunk of raw, uninitialized memory. no vibes pre-installed, no zeros, just ✨space✨. let's get into it.


✨ NAME ✨

SDL_malloc β€” allocate uninitialized memory. that's it, that's the whole job πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <SDL3/SDL_stdinc.h>

void * SDL_malloc(size_t size);

🌸 DESCRIPTION 🌸

so you call this, you get memory. it's giving "here's a blank canvas, do literally whatever." πŸ¦‹

the one rule you cannot skip: the allocated memory this function gives back must be freed with SDL_free(). no cap. if you don't free it, that's a leak and the leak is not cute. 😭

if size is 0, it will be set to 1. yes fr, zero becomes one. iconic behavior honestly.

if the allocation is successful, the returned pointer is guaranteed to be aligned to either the fundamental alignment (alignof(max_align_t) in C11 and later) or 2 * sizeof(void *), whichever is smaller. ⭐

lowkey important: if you need memory aligned to something greater than that guarantee, do not try to make this function do it. use SDL_aligned_alloc() instead. she's the specialist. πŸ‘‘

πŸ’œ FUNCTION PARAMETERS πŸ’œ

  • size β€” the size to allocate. that's the whole parameter. the one and only. 🫢

🌈 RETURN VALUE 🌈

returns a pointer to the allocated memory, or NULL if the allocation failed. if you get NULL, that's not a vibe, that's a failure β€” handle it. πŸ™ˆ


πŸ”₯ THREAD SAFETY πŸ”₯

it is safe to call this function from any thread. go off, multithreaded queen. 🧚


πŸ‘‘ AVAILABILITY πŸ‘‘

this function is available since SDL 3.2.0. so if you're on something older… bestie, why. 😭


πŸ’€ SEE ALSO πŸ’€

iykyk, those four are the whole memory-management friend group and you will be seeing them again. πŸ“


and that's the tea, bestie. this page came to you courtesy of Simple Directmedia Layer, running SDL 3.4.18, dated SDL_malloc(3) β€” mwah, slay, go free your memory. πŸ’–βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from