MEMSET_S(3) Library Functions Manual MEMSET_S(3)

memset_scopy a value to all bytes of a memory buffer

Standard C Library (libc, -lc)

#define __STDC_WANT_LIB_EXT1__ 1

#include <string.h>

errno_t
memset_s(void *s, rsize_t smax, int c, rsize_t n);

The () 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, () copies smax bytes to the destination.

In contrast to the memset(3) function, calls to () 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 (), 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.

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

memset_s() returns the following error codes. It does not store the error code in the global errno variable:

[]
The s argument was a null pointer.
[]
One or both of smax or n was larger than RSIZE_MAX.
[]
n was larger than smax.

memset(3).

The memset_s() function conforms to ISO/IEC 9899:2011 (“ISO C11”), except that the set_constraint_handler_s() interface is not supported.

February 21, 2012 Mac OS X 12