MEMORY(3)

πŸ“¦
version 0_git20260208 Β· 4 versions
, in alpine 3.24, arch rolling, nixos 26.05, nixpkgs unstable

πŸ’– MEMORY(3) β€” the memory operations glow-up πŸ’–

okay bestie, welcome to MEMORY(3) from the Library Functions Manual, aka the page where all six of these byte-pushing icons live. we've got memccpy, memchr, memcmp, memcpy, memmove, and memset β€” say them out loud, they're literally a friend group 🫢


✨ SYNOPSIS ✨

like, you gotta include these first or nothing works:

#include <u.h>
#include <libc.h>

and then the whole squad, one by one:

void* memccpy(void *s1, void *s2, int c, long n)

void* memchr(void *s, int c, long n)

int   memcmp(void *s1, void *s2, long n)

void* memcpy(void *s1, void *s2, long n)

void* memmove(void *s1, void *s2, long n)

void* memset(void *s, int c, long n)

no notes, they ate πŸ’…


🌸 DESCRIPTION 🌸

so these functions operate efficiently on memory areas β€” and yes that means arrays of bytes that are bounded by a count, not terminated by a zero byte. very much "we know our own length" main character energy. πŸŽ€

⚠️ big one: they do not check for the overflow of any receiving memory area. not a single vibe check. it's giving trust fall and nobody's catching you, so pls be careful with your counts.
  • memccpy β€” copies bytes from memory area s2 into s1, and stops after the first occurrence of byte c has been copied, or after n bytes have been copied, whichever comes first. it returns a pointer to the byte after the copy of c in s1, or zero if c was not found in the first n bytes of s2. lowkey a road trip that ends the second you spot your fave πŸ¦‹
  • memchr β€” returns a pointer to the first occurrence of byte c in the first n bytes of memory area s, or zero if c does not occur. it's giving "finding your friend in a crowd" and honestly, iconic πŸ’œ
  • memcmp β€” compares its arguments, looking at the first n bytes only, and returns an integer less than, equal to, or greater than 0, according as s1 is lexicographically less than, equal to, or greater than s2. and the comparison is bytewise unsigned β€” no sign shenanigans allowed 🧚
  • memcpy β€” copies n bytes from memory area s2 to s1. it returns s1. short, sweet, does exactly what it says ✨
  • memmove β€” works like memcpy, except that it is guaranteed to work if s1 and s2 overlap. the responsible friend of the group 🫢
  • memset β€” sets the first n bytes in memory area s to the value of byte c. it returns s. fill the whole thing with one byte and call it a day 🌈

⭐ SOURCE ⭐

all these routines have portable C implementations in /src/lib9. like, all six of them, no cap. whether you're on some wild exotic machine or your friend's laptop, they're there for you πŸ‘‘


πŸ¦‹ SEE ALSO πŸ¦‹

go read that one too bestie, it's in the same friend group iykyk


πŸ’€ BUGS πŸ’€

okay so, the caveats. pay attention, this part matters:

  • ANSI C does not require memcpy to handle overlapping source and destination. on Plan 9, it does, so memmove and memcpy behave identically. so if you're porting code around, don't assume the overlap tolerance travels with you β€” that's a Plan 9 specific slay, not a universal one 😭
  • if memcpy and memmove are handed a negative count, they abort. like, immediately. no gentle error, no little warning, they just exit the chat. so keep your counts non-negative fr fr πŸ™ˆ

and that's the whole page! no extra hidden sections, no secret last boss.

MEMORY(3) β€” Library Functions Manual, that's the footer bestie, that's literally it. no version number, no date, just the page name and the manual name and the vibes. periodt. πŸ’–πŸŽ€πŸ¦‹