---
name: "memset_s"
section: "3"
platform: "macos"
package: "Libc"
version: "1752.120.2"
release: "macOS 26.5"
platforms: ["freebsd", "macos"]
url: "https://new.carta.tech/memset_s"
---

# memset\_s(3)

copy a value to all bytes of a memory buffer

## NAME

**memset\_s** – copy a value to all bytes of a memory buffer

## LIBRARY

Standard C Library (libc, -lc)

## SYNOPSIS

**#define \_\_STDC\_WANT\_LIB\_EXT1\_\_ 1**

**#include \<string.h\>**

*errno\_t*\

**memset\_s**(*void \*s*, *rsize\_t smax*, *int c*, *rsize\_t n*);

## DESCRIPTION

The **memset\_s**() function copies the value *c* (converted to an unsigned char) into each of the first *n* bytes of the memory buffer whose starting address is given by *s*.

It is a runtime-constraints violation if *s* is a null pointer, or if either of *smax* or *n* is larger than RSIZE\_MAX, or if *smax* is smaller than *n*. If there is a runtime-constraints violation, and if *s* is not a null pointer, and if *smax* is not larger than RSIZE\_MAX, then, before reporting the runtime-constraints violation, **memset\_s**() copies *smax* bytes to the destination.

In contrast to the [memset(3)](/memset/3.md) function, calls to **memset\_s**() will never be “optimised away” by a compiler. This property is required by the following sentences in section K.3.7.4.1 of ISO/IEC 9899:2011 (“ISO C11”):

Unlike **memset**(), any call to the **memset\_s**() function shall be evaluated strictly according to the rules of the abstract machine as described in (5.1.2.3). That is, any call to the **memset\_s**() function shall assume that the memory indicated by *s* and *n* may be accessible in the future and thus must contain the values indicated by *c*.

## RETURN VALUES

The **memset\_s**() function returns zero for success, or a non-zero error code if there was a runtime-constraints violation.

## ERRORS

**memset\_s**() returns the following error codes. It does not store the error code in the global *errno* variable:

- \[EINVAL\]

  The *s* argument was a null pointer.

- \[E2BIG\]

  One or both of *smax* or *n* was larger than RSIZE\_MAX.

- \[EOVERFLOW\]

  *n* was larger than *smax*.

## SEE ALSO

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

## STANDARDS

The **memset\_s**() function conforms to ISO/IEC 9899:2011 (“ISO C11”), except that the **set\_constraint\_handler\_s**() interface is not supported.
