Home

zeroterminated

Zero-terminated, also known as null-terminated, refers to a method of representing strings as a sequence of characters that ends with a terminator character whose value is zero. In C and many C-family languages, strings are stored as arrays of char terminated by the null character ('\0'). The terminator marks the end of the string; the length is not stored explicitly, and the content may be scanned to determine its length.

How it works: standard string operations rely on the terminator to determine where the string ends. For

Advantages and risks: Advantages include simple memory model and compatibility with C APIs, as well as cheap

Variations: In wide-character strings, the terminator is the corresponding wide null character (L'\0'). In modern C++

example,
the
C
standard
library
functions
strlen,
strcpy,
and
strcat
scan
memory
until
they
encounter
'\0'.
The
terminator
is
considered
part
of
the
storage
but
not
part
of
the
string's
logical
length.
This
representation
is
efficient
for
small,
frequently
modified
strings
but
can
be
unsafe
if
a
terminator
is
missing
or
corrupted,
potentially
causing
memory
access
beyond
the
end.
storage
for
short
strings
and
straightforward
pointer
arithmetic.
Risks
include
buffer
overruns
when
writing
without
ensuring
a
terminator,
difficulty
handling
binary
data
that
may
contain
0
bytes,
and
the
need
for
careful
bounds
checks
in
low-level
code.
Some
languages
and
data
structures
opt
for
length-prefixed
or
length-scoped
strings
instead
of
termination-based
strings.
programming,
std::string
stores
a
length
explicitly
and
does
not
rely
on
a
trailing
null
for
most
operations,
though
it
is
still
null-terminated
for
compatibility
with
C
APIs.
In
other
languages,
string
representations
are
length-based
or
encoded
with
explicit
size
fields,
making
zero
termination
unnecessary.