MEMORY(3)
π 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 areas2intos1, and stops after the first occurrence of bytechas been copied, or afternbytes have been copied, whichever comes first. it returns a pointer to the byte after the copy ofcins1, or zero ifcwas not found in the firstnbytes ofs2. lowkey a road trip that ends the second you spot your fave π¦memchrβ returns a pointer to the first occurrence of bytecin the firstnbytes of memory areas, or zero ifcdoes not occur. it's giving "finding your friend in a crowd" and honestly, iconic πmemcmpβ compares its arguments, looking at the firstnbytes only, and returns an integer less than, equal to, or greater than 0, according ass1is lexicographically less than, equal to, or greater thans2. and the comparison is bytewise unsigned β no sign shenanigans allowed π§memcpyβ copiesnbytes from memory areas2tos1. it returnss1. short, sweet, does exactly what it says β¨memmoveβ works likememcpy, except that it is guaranteed to work ifs1ands2overlap. the responsible friend of the group π«Άmemsetβ sets the firstnbytes in memory areasto the value of bytec. it returnss. 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
memcpyto handle overlapping source and destination. on Plan 9, it does, somemmoveandmemcpybehave 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
memcpyandmemmoveare 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. πππ¦
π¦ where this page comes from
- alpine 3.24: plan9port-doc 0_git20260208-r0 Β· LPL-1.02
- arch rolling: plan9port 20261021-2 Β· custom
- nixos 26.05: plan9port 0-unstable-2025-11-10
- nixos 26.05: plan9port-wayland 0-unstable-2025-11-26
- nixpkgs unstable: plan9port 0-unstable-2025-11-10
- nixpkgs unstable: plan9port-wayland 0-unstable-2025-11-26
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms