Skip to content

Commit 7999aad

Browse files
committed
[cstring.syn] Add example of implicit object creation in memcpy and memmove
1 parent f79a0f6 commit 7999aad

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

source/strings.tex

+27
Original file line numberDiff line numberDiff line change
@@ -5517,6 +5517,33 @@
55175517
immediately prior to copying the sequence of characters to the destination.
55185518
Each of these functions returns a pointer to a suitable created object, if any,
55195519
otherwise the value of the first parameter.
5520+
\begin{example}
5521+
\begin{codeblock}
5522+
int x = 0;
5523+
memmove(&x, &x, sizeof(int));
5524+
assert(x == 0); // Passes.
5525+
5526+
unsigned y;
5527+
memcpy(&y, &x, sizeof(int));
5528+
assert(y == 0); // Passes.
5529+
5530+
unsigned char bytes[4] = {
5531+
0xff, 0xff, 0xff, 0xff
5532+
};
5533+
memcpy(&y, bytes, 4);
5534+
assert(y == 0xffffffff); // Passes, assuming that \tcode{sizeof(unsigned)} equals \tcode{4},
5535+
// and that \tcode{CHAR_BIT} equals \tcode{8}.
5536+
\end{codeblock}
5537+
Both \tcode{x} and \tcode{y} are transparently replaced\iref{basic.life}
5538+
with implicitly created objects in the storage of the old objects,
5539+
and refer to the new respective objects.
5540+
In the call to \tcode{memmove},
5541+
the implicit object creation takes place after copying the bytes of \tcode{x}
5542+
into a temporary array,
5543+
and immediately before copying them into the implicitly created object.
5544+
Since \tcode{int} is trivially copyable\iref{basic.types.general},
5545+
\tcode{x} retains the value \tcode{0}.
5546+
\end{example}
55205547

55215548
\pnum
55225549
\begin{note}

0 commit comments

Comments
 (0)