MEMCPY(3P)

πŸ’– MEMCPY(3P) β€” bestie we're copying bytes πŸ’–

okay so this one's from the POSIX Programmer's Manual (the 3P is giving "POSIX section 3, library functions" ✨), and it's literally the glow-up moment for moving memory around. Sit down, hydrate, let's go 🫢


🌸 PROLOG 🌸

Real talk first, bestie: this manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ β€” so if you're on Linux, go check the corresponding Linux manual page for the actual tea on Linux behavior. Or, plot twist 😭, the interface may not be implemented on Linux at all. No cap. Read your platform's page. πŸ™ˆ

✨ NAME ✨

memcpy β€” copy bytes in memory πŸ’…


πŸŽ€ SYNOPSIS πŸŽ€

#include <string.h>

void *memcpy(void *restrict s1, const void *restrict s2, size_t n);

That's it. That's the whole vibe. <string.h> is where it lives, restrict is doing its little thing on both pointers, and n is how many bytes we're moving, periodt. πŸ‘‘


πŸ¦‹ DESCRIPTION πŸ¦‹

Okay so first, the fine print, because it matters: the functionality described on this reference page is aligned with the ISO C standard. Any conflict between the requirements described here and the ISO C standard is unintentional β€” it's an oopsie, not a statement. This volume of POSIX.1‐2017 defers to the ISO C standard. 🀝

Now the actual function, and it's so simple it's iconic:

The memcpy() function shall copy n bytes from the object pointed to by s2 into the object pointed to by s1.

🚨 THE BIG WARNING 🚨 If copying takes place between objects that overlap, the behavior is undefined. Like, not "weird", not "slightly off" β€” undefined. Do not. I am begging. If your regions overlap, memcpy() is not your girl, and that's the whole story, bestie. πŸ’€

🌈 RETURN VALUE 🌈

The memcpy() function shall return s1. That's it β€” it hands you back the destination pointer like a little gift so you can chain things. 🎁

And listen: no return value is reserved to indicate an error. None. There is no error signal hiding in the return value. So don't go checking it for one. 😭


πŸ’œ ERRORS πŸ’œ

No errors are defined. Zero. Zip. Nada. It's not going to tell you anything went wrong, because there's nothing for it to tell you. πŸ™ˆ

The following sections are informative β€” that means they're background reading, not requirements. Cute of them. πŸ“š


πŸ“ EXAMPLES πŸ“

None.

That's the whole section. The manpage said "no examples, figure it out" and honestly? Respect. πŸ’…


🧚 APPLICATION USAGE 🧚

The memcpy() function does not check for the overflow of the receiving memory area.

Read that again, bestie. It does not check. It will happily write right past the end of your destination buffer and there will be no warning, no error, no nothing. The size of s1 is your problem, your job, your responsibility. Pick n with intention, okay? Protect your memory. Protect yourself. 🫢

⭐ RATIONALE ⭐

None.

No reasoning was provided. The committee said "it copies bytes" and moved on, and honestly, main character energy. πŸ‘‘


🌟 FUTURE DIRECTIONS 🌟

None.

It's not going anywhere. It's not changing. It's eternal. Periodt. πŸ’…


πŸ”₯ SEE ALSO πŸ”₯

  • The Base Definitions volume of POSIX.1‐2017, <string.h>

That's the one link on the list, and it's the header file, because that's where all the receipts live. ✨


Gotta give credit where credit is due, bestie, this part is important:

Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group.

In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. (Translation: the real one wins, always. πŸ†)

The original Standard can be obtained online at http://www.opengroup.org/unix/online.html .

Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's the whole page, bestie 🫢 brought to you by IEEE / The Open Group, dated 2017 β€” thanks for coming to my TED talk, now go copy some bytes and for the love of everything do not let them overlap πŸ’–βœ¨πŸŽ€πŸ¦‹πŸŒΈπŸ’…

πŸ“¦ where this page comes from