---
name: "memset_s"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd", "macos"]
url: "https://new.carta.tech/memset"
---

# memset\_s(3)

set bytes in memory

Part of [memset(3)](/memset/3.md).

## NAME

**memset** – set bytes in memory

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#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

The **memset**() function writes *len* bytes of value *c* (converted to an *unsigned char*) to the object *dest*. Undefined behaviour from **memset**(), resulting from storage overflow, will occur if *len* is greater than the length of the *dest* buffer. The behaviour is also undefined if *dest* is an invalid pointer.

The **memset\_explicit**() function behaves the same as **memset,**(*but*, *will*, *not*, *be*, *removed*, *by*, *a*, *compiler's*, *dead*, *store*) optimization pass, making it useful for clearing sensitive memory such as a password.

The **memset\_s**() function 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)](/explicit_bzero/3.md), **memset\_s**() is not removed through Dead Store Elimination (DSE), making it useful for clearing sensitive data. In contrast **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)](/free/3.md).

## RETURN VALUES

The **memset**() and **memset\_explicit**() functions return their first argument. The **memset\_s**() function returns zero on success, non-zero on error.

## SEE ALSO

[bzero(3)](/bzero/3.md), [explicit\_bzero(3)](/explicit_bzero/3.md), [set\_constraint\_handler\_s(3)](/set_constraint_handler_s/3.md), [swab(3)](/swab/3.md), [wmemset(3)](/wmemset/3.md)

## STANDARDS

The **memset**() function 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 ISO/IEC 9899:2024 (“ISO C23”).
