最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++20 - memmove overwriting objects and their lifetimes - Stack Overflow

programmeradmin2浏览0评论

Memmove allows the source and destination to have an overlapping region. And if you're using it with trivially copyable types, the objects copied by memmove will be implicitly created and their lifetimes will start (no undefined behavior).

But I'm confused about what happens to objects overwritten by memmove. Consider this example:

#include <array>
#include <type_traits>
#include <iostream>

struct obj_t {
    int a, b, c, d;
};

static_assert(std::is_trivially_copyable_v<obj_t> == true,
    "objects must be trivially copyable to be implicitly created by memmove");

int main() {

    std::array<obj_t, 8> objs{};

    std::cout << "before:\n";
    int i = 0;
    for (auto& obj : objs) {
        obj.a = i++;
        std::cout << obj.a << "\n";
    }

    //move 4 objects from position 2 to position 1 in the array:
    obj_t* src  = &objs[2];
    obj_t* dest = &objs[1];
    memmove(dest, src, 4 * sizeof(obj_t));

    std::cout << "after:\n";
    for (auto& obj : objs) std::cout << obj.a << "\n";
}

Here is the program output:

before:
0
1
2
3
4
5
6
7
after:
0
2
3
4
5
5
6
7

The structs at positions 1 to 4 were overwritten (this includes some structs in the overlapping and non-overlapping regions).

What happens to the lifetime of objects that are overwritten by memmove? Are their lifetimes ended properly, before the new objects are implicitly created in their place (is there undefined behavior here)?

What about duplicate copies left at the source (i.e. the struct at position 5)? Is the old copy still a live object?

Memmove allows the source and destination to have an overlapping region. And if you're using it with trivially copyable types, the objects copied by memmove will be implicitly created and their lifetimes will start (no undefined behavior).

But I'm confused about what happens to objects overwritten by memmove. Consider this example:

#include <array>
#include <type_traits>
#include <iostream>

struct obj_t {
    int a, b, c, d;
};

static_assert(std::is_trivially_copyable_v<obj_t> == true,
    "objects must be trivially copyable to be implicitly created by memmove");

int main() {

    std::array<obj_t, 8> objs{};

    std::cout << "before:\n";
    int i = 0;
    for (auto& obj : objs) {
        obj.a = i++;
        std::cout << obj.a << "\n";
    }

    //move 4 objects from position 2 to position 1 in the array:
    obj_t* src  = &objs[2];
    obj_t* dest = &objs[1];
    memmove(dest, src, 4 * sizeof(obj_t));

    std::cout << "after:\n";
    for (auto& obj : objs) std::cout << obj.a << "\n";
}

Here is the program output:

before:
0
1
2
3
4
5
6
7
after:
0
2
3
4
5
5
6
7

The structs at positions 1 to 4 were overwritten (this includes some structs in the overlapping and non-overlapping regions).

What happens to the lifetime of objects that are overwritten by memmove? Are their lifetimes ended properly, before the new objects are implicitly created in their place (is there undefined behavior here)?

What about duplicate copies left at the source (i.e. the struct at position 5)? Is the old copy still a live object?

Share Improve this question asked 19 hours ago greenlagoongreenlagoon 1211 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I think that your example can be interpreted as a sequence of assignment operations that is equivalent to the following pseudo-code. More specifically, you don't need to create or destroy any objects.

objs[1] = objs[2];
objs[2] = objs[3];
objs[3] = objs[4];
objs[4] = objs[5];

If you also assume that obj_t is trivially destructible, another interpretation could be:

  • The lifetime of each object is ended. (Note 1)
  • A new object of type obj_t is created.
  • The new object is initialized by copy-construction.

Note 1: from the cppreference.com page about lifetime under "Storage reuse":

A program is not required to call the destructor of an object to end its lifetime if the object is trivially-destructible

发布评论

评论列表(0)

  1. 暂无评论