MEMCACHED_CLONE(3)

πŸ’– MEMCACHED_CLONE πŸ’–

okay bestie, sit down, we're talking libmemcached and specifically the whole clone / create / free situation. this is memcached_clone(3) from libmemcached-awesome, and yes it's giving ✨foundational library lore✨. if you're writing C that talks to memcached, this is your girl. let's go 🫢


🌸 SYNOPSIS 🌸

#include <libmemcached/memcached.h>

Compile and link with -lmemcached (no cap, that's the whole vibe).

typedef struct memcached_st memcached_st

✨ memcached_create

memcached_st *memcached_create(memcached_st *ptr)
  • Parameters
    • ptr β€” pointer to a user-allocated memcached_st struct, or a null pointer
  • Returns β€” pointer to the initialized memcached_st struct

✨ memcached_free

void memcached_free(memcached_st *ptr)
  • Parameters
    • ptr β€” pointer to an initialized memcached_st struct that you want destroyed and possibly freed

✨ memcached_clone

memcached_st *memcached_clone(memcached_st *destination, memcached_st *source)
  • Parameters
    • destination β€” pointer to a user-allocated memcached_st struct, or a null pointer
    • source β€” pointer to an initialized memcached_st struct to copy from
  • Returns β€” pointer to the newly initialized destination, copied from source

✨ memcached_servers_reset

void memcached_servers_reset(memcached_st *ptr)
  • Parameters
    • ptr β€” pointer to an initialized memcached_st struct

πŸ¦‹ DESCRIPTION πŸ¦‹

so memcached_create() is the one that makes you a memcached_st structure β€” that's the lil object every other libmemcached function uses to actually talk to the server. you've got two options, and honestly both are valid: hand it a statically declared memcached_st, or hand it a NULL. and if you pass that NULL? it just allocates a whole structure for you. main character energy, honestly πŸ’…

not me about to say this: when you write a new application, use memcached() over memcached_create(). that's literally what the docs say. iykyk.

now memcached_clone() is like memcached_create()'s cooler sibling β€” same energy, but it also copies the defaults and the list of servers from the source memcached_st pointer. and here's the cute part: if you pass a null as the argument for the source to clone, it's literally the same as calling memcached_create(). if the destination argument is NULL, a memcached_st will be allocated for you. easy ✨

memcached_servers_reset() lets you zero out the list of servers that the memcached_st has. bye servers, it's not you it's me πŸŽ€

and to clean up the memory tied to a memcached_st structure, you pass it to memcached_free() when you're done with it. important: memcached_free() is the only way to make sure all the memory is deallocated when you finish using the structure. don't skip it, bestie, that's a leak πŸ’€

real talk: you may want to avoid using memcached_create() or memcached_clone() with a stack based allocation. the most common issues related to ABI safety involve heap allocated structures. so like, heap it. please. πŸ™ˆ

🌈 RETURN VALUE 🌈

  • memcached_create() returns a pointer to the memcached_st that was created (or initialized). on an allocation failure, it returns NULL.
  • memcached_clone() returns a pointer to the memcached_st that was created (or initialized). on an allocation failure, it returns NULL.

so yeah, check for NULL before you go using the thing. no cap.


πŸ‘‘ SEE ALSO πŸ‘‘


and that's the whole page, bestie πŸ“ this was version 1.1, dated Jul 24, 2025, courtesy of libmemcached-awesome. go forth and memcached_free() your structs, mwah πŸ’œβœ¨

πŸ“¦ where this page comes from