File tree 1 file changed +27
-0
lines changed
1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 5517
5517
immediately prior to copying the sequence of characters to the destination.
5518
5518
Each of these functions returns a pointer to a suitable created object, if any,
5519
5519
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 }
5520
5547
5521
5548
\pnum
5522
5549
\begin {note }
You can’t perform that action at this time.
0 commit comments