Home

nmemb

nmemb is a parameter name used in several C standard library functions to represent the number of elements or members to process. It is typically paired with size, which denotes the size in bytes of each element. Together nmemb and size determine the total amount of data involved, via the product nmemb * size, and the functions usually return the number of elements successfully processed rather than bytes.

Common uses include fread, fwrite, and calloc. In fread(ptr, size, nmemb, stream), up to nmemb elements of

Care is required to avoid overflow when computing nmemb * size, since size_t is used for the count

The term nmemb is commonly found in C documentation and source, reflecting its longstanding role as a

data,
each
of
size
bytes,
are
read
from
stream
into
ptr.
The
function
returns
the
number
of
elements
actually
read,
which
can
be
less
than
nmemb
at
end-of-file
or
on
error.
Similarly,
fwrite(ptr,
size,
nmemb,
stream)
writes
nmemb
elements
from
ptr
to
stream
and
returns
the
number
of
elements
written.
calloc(nmemb,
size)
allocates
memory
for
nmemb
elements,
each
of
size
bytes,
and
initializes
the
memory
to
zero;
it
returns
a
pointer
or
NULL
on
failure.
and
total
byte
size.
If
the
product
cannot
be
represented
in
size_t,
the
operation
can
wrap
and
lead
to
errors.
Additionally,
the
behavior
for
nmemb
=
0
is
defined
differently
across
functions;
for
fread/fwrite
it
typically
yields
0
with
no
data
transfer,
while
calloc
may
have
implementation-defined
outcomes.
descriptive
name
for
a
count
of
elements
in
memory
and
I/O
operations.