Quantcast
Channel: Why would the behavior of std::memcpy be undefined for objects that are not TriviallyCopyable? - Stack Overflow
Browsing all 11 articles
Browse latest View live

Answer by cmaster - reinstate monica for Why would the behavior of...

Ok, lets try your code with a little example:#include <iostream>#include <string>#include <string.h>void swapMemory(std::string* ePtr1, std::string* ePtr2) { static const int size =...

View Article



Answer by curiousguy for Why would the behavior of std::memcpy be undefined...

First, note that it is unquestionable that all memory for mutable C/C++ objects has to be un-typed, un-specialized, usable for any mutable object. (I guess the memory for global const variables could...

View Article

Answer by Martin Ba for Why would the behavior of std::memcpy be undefined...

What I can perceive here is that -- for some practical applications -- the C++ Standard may be to restrictive, or rather, not permittive enough.As shown in other answers memcpy breaks down quickly for...

View Article

Answer by dyp for Why would the behavior of std::memcpy be undefined for...

C++ does not guarantee for all types that their objects occupy contiguous bytes of storage [intro.object]/5An object of trivially copyable or standard-layout type (3.9) shall occupy contiguous bytes of...

View Article

Answer by gnasher729 for Why would the behavior of std::memcpy be undefined...

memcpy will copy all the bytes, or in your case swap all the bytes, just fine. An overzealous compiler could take the "undefined behaviour" as an excuse to to all kinds of mischief, but most compilers...

View Article


Answer by CAdaker for Why would the behavior of std::memcpy be undefined for...

Another reason that memcpy is UB (apart from what has been mentioned in the other answers - it might break invariants later on) is that it is very hard for the standard to say exactly what would...

View Article

Answer by CAdaker for Why would the behavior of std::memcpy be undefined for...

Many of these answers mention that memcpy could break invariants in the class, which would cause undefined behaviour later (and which in most cases should be reason enough not to risk it), but that...

View Article

Answer by Maxim Egorushkin for Why would the behavior of std::memcpy be...

It is easy enough to construct a class where that memcpy-based swap breaks:struct X { int x; int* px; // invariant: always points to x X() : x(), px(&x) {} X(X const& b) : x(b.x), px(&x) {}...

View Article


Answer by Columbo for Why would the behavior of std::memcpy be undefined for...

Why would the behavior of std::memcpy itself be undefined when used with non-TriviallyCopyable objects?It's not! However, once you copy the underlying bytes of one object of a non-trivially copyable...

View Article


Answer by Yakk - Adam Nevraumont for Why would the behavior of std::memcpy be...

Because the standard says so.Compilers may assume that non-TriviallyCopyable types are only copied via their copy/move constructors/assignment operators. This could be for optimization purposes (if...

View Article

Why would the behavior of std::memcpy be undefined for objects that are not...

From http://en.cppreference.com/w/cpp/string/byte/memcpy:If the objects are not TriviallyCopyable (e.g. scalars, arrays, C-compatible structs), the behavior is undefined.At my work, we have used...

View Article
Browsing all 11 articles
Browse latest View live




Latest Images