BSON_STRNDUP(3)

πŸ’… BSON_STRNDUP(3) β€” the libbson one, bestie

Okay so real talk, this is a page straight out of libbson, and it's honestly such a mood: it's the "copy just a little bit of a string" girlie. Like strndup(3) but make it BSON. Let's get into it ✨


πŸŽ€ SYNOPSIS πŸŽ€

char *
bson_strndup (const char *str, size_t n_bytes);

Look at her. Two args, returns a char *. Clean, minimal, main character energy πŸ‘‘


🌸 PARAMETERS 🌸

  • str β€” a string to copy. That's it, that's the vibe. It's the source string you're duplicating 🫢
  • n_bytes β€” a size_t. Yes bestie, the type is literally size_t, that's the whole description, we love an unbothered queen πŸ’…

πŸ¦‹ DESCRIPTION πŸ¦‹

So what she does is: allocates a new string and copies up to n_bytes from str into it. Up to. As in, if n_bytes is smaller than the string, you're getting the first n_bytes bytes and nothing more, no cap.

And the iconic detail? A trailing \0 is always set. Always. Even when you didn't ask. Even when you're being weird about it. She terminates that string fr fr πŸ˜­πŸ’–


⭐ RETURNS ⭐

A newly allocated string that should be freed with bson_free().

🚨 Bestie listen to me. That's newly allocated memory. It's yours now. You own it. So you have to bson_free() it when you're done or it's a leak and the leak is on you πŸ™ˆπŸ’€

🧚 AUTHOR 🧚

MongoDB, Inc 🌈


2009-present, MongoDB, Inc. β€” iconic run honestly, staying power πŸ’œ


And that's the page!! Brought to you by libbson 2.5.6, dated Oct 09, 2026 β€” mwah, go free your strings, ily, bye πŸŽ€πŸ’–βœ¨

πŸ“¦ where this page comes from