Is segmentation fault actual undefined behavior when we refer to a non-static data-member

前端 未结 3 1398
悲哀的现实
悲哀的现实 2020-11-21 05:17

I had read the following rule and I\'ve been trying to write an example, which reflects one. The rule is from 3.8/5 N3797:

Before the lifetime of an o

3条回答
  •  伪装坚强ぢ
    2020-11-21 06:06

    Rule 3.8/5 is about the time outside of the construction/destruction of an object but inside the allocation/release of the memory in which the object resides. The following demonstrates the points outside of the lifetime of an object:

    void *buffer = malloc(sizeof(A));
    // outside of lifetime of a
    // a->b is undefined
    A* a = new (buffer) A();
    // within lifetime of a
    // a->b is valid
    a->~A();
    // outside of lifetime of a
    // a->b is undefined
    free(buffer);
    

    Technically, your post doesn't actually reflect rule 3.8/5, because you are not accessing the object outside of its lifetime. You are simply casting random memory as an instance.

提交回复
热议问题