Re:Catching use-after-move C++ bugs with Clang's consumed an...
by namirez on Wednesday July 10, 2019 @12:47PM
(#20403738)
Move semantics is the language support for shallow copies. If you never use shallow copies, fine! But a lot of code bases use shallow copies, and it's a valuable tool.
[ Parent
| on HN ]
Re:Catching use-after-move C++ bugs with Clang's consumed an...
by Someone on Wednesday July 10, 2019 @02:48PM
(#20405104)
Move semantics moves objects, it doesn’t copy them.
For example, if you have a class C containing a pointer P to some data, where C’s destructor frees that pointer:
- a shallow copy of object O would return an object O2, containing that same pointer P, and leave O unmodified.
- a move of O to O2 would (1) make O2 contain P, but also would update O to no longer have that pointer P (it has to make that change, as destructing O at a later time shouldn’t free P anymore)
(1) yes, an implementation could also copy the data or increase a reference count, or, possibly, a zillion different things without running into problems.
Re:Catching use-after-move C++ bugs with Clang's consumed an...
by namirez on Thursday July 11, 2019 @12:09AM
(#20409043)
Shallow copy is the general concept. Move semantics is a special case where the unique ownership is enforced. Here is simple example of the idea:
Widget::Widget(Widget&& w) {
this->ptr = w.ptr; // shallow copy
w.ptr = nullptr; // nullify the source
}
Re:Catching use-after-move C++ bugs with Clang's consumed an...
by clappski on Thursday July 11, 2019 @04:20AM
(#20409969)
You could use;
auto* p2 = std::exchange( p1, nullptr );