MEMSET(3)
๐
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 frommemset()โ the storage-overflow kind โ will occur iflenis greater than the length of thedestbuffer. The behaviour is also undefined ifdestis 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()andmemset_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
- Copyright (c) 1990, 1991, 1993 The Regents of the University of California. All rights reserved
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms