MEMSET(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’… memset(3) โ€” write a byte to byte string ๐Ÿ’…

okay so this is the memset(3) page from the Library Functions Manual, and we are about to get into byte-writing, memory-clearing, and the very real difference between memset() and its safer siblings. stay with me bestie, itโ€™s giving โ€œlow-level but make it cuteโ€ ๐Ÿ’–


๐ŸŒธ NAME ๐ŸŒธ

memset โ€” write a byte to byte string


๐Ÿงš LIBRARY ๐Ÿงš

Standard C Library (libc, -lc) โ€” yes bestie, itโ€™s the standard C library, the one and only.


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

hereโ€™s the include and the three signatures, exactly as the page gives them:

#include <string.h>

void *
memset(void *dest, int c, size_t len);

void *
memset_explicit(void *dest, int c, size_t len);

#define __STDC_WANT_LIB_EXT1__ 1

errno_t
memset_s(void *dest, rsize_t destsz, int c, rsize_t len);

๐Ÿ’ซ DESCRIPTION ๐Ÿ’ซ

memset() is the classic โ€œwrite a byte, byte by byteโ€ girlie. It writes len bytes of value c (converted to an unsigned char) to the string dest.

โš ๏ธ Undefined behaviour from memset() โ€” the storage-overflow kind โ€” will occur if len is greater than the length of the dest buffer. The behaviour is also undefined if dest is an invalid pointer. This is not a drill, bestie ๐Ÿ’€

memset_explicit() behaves the same as memset(), but a compilerโ€™s dead store optimization pass will not remove it, making it useful for clearing sensitive memory such as a password.

memset_s() behaves the same as memset() except that an error is returned and the currently registered runtime-constraint handler is called if dest is a null pointer, destsz or len is greater than RSIZE_MAX, or len is greater than destsz (buffer overflow would occur). The runtime-constraint handler is called first and may not return. If it does return, an error is returned to the caller.

Like explicit_bzero(3), memset_s() is not removed through Dead Store Elimination (DSE), making it useful for clearing sensitive data. In contrast, the memset() function may be optimized away if the object modified by the function is not accessed again. To clear memory that will not subsequently be accessed, it is advised to use memset_s() instead of memset(). For instance, a buffer containing a password should be cleared with memset_s() before free(3).


๐Ÿ” RETURN VALUES ๐Ÿ”

  • memset() and memset_explicit() return their first argument.
  • memset_s() returns zero on success, non-zero on error.

๐Ÿ‘€ SEE ALSO ๐Ÿ‘€


๐Ÿ“œ STANDARDS ๐Ÿ“œ

  • memset() conforms to ISO/IEC 9899:1990 (โ€œISO C90โ€).
  • memset_s() conforms to ISO/IEC 9899:2011 (โ€œISO C11โ€) K.3.7.4.1.
  • memset_explicit() conforms to . ๐Ÿซ 

xoxo ๐Ÿ’– Debian โ€ข October 24, 2024 โ€ข memset(3)

๐Ÿ“ฆ where this page comes from
  • freebsd 15.1: base 15.1
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms