Shared memory buffers in C++ without violating strict aliasing rules

后端 未结 3 669
遇见更好的自我
遇见更好的自我 2020-12-16 23:39

I am struggling with implementing a shared memory buffer without breaking C99\'s strict aliasing rules.

Suppose I have some code that processes some data and needs t

3条回答
  •  隐瞒了意图╮
    2020-12-17 00:30

    Actually, what you have written is not a strict aliasing violation.

    C++11 spec 3.10.10 says:

    If a program attempts to access the stored value of an object through a glvalue of other than one of the following types the behavior is undefined

    So the thing that causes the undefined behavior is accessing the stored value, not just creating a pointer to it. Your example does not violate anything. It would need to do the next step: float badValue = smem[0]. smem[0] gets the stored value from the shared buffer, creating an aliasing violation.

    Of course, you aren't about to just grab smem[0] before setting it. You are going to write to it first. Assigning to the same memory does not access the stored value, so no ailiasing However, it IS illegal to write over the top of an object while it is still alive. To prove that we are safe, we need object lifespans from 3.8.4:

    A program may end the lifetime of any object by reusing the storage which the object occupies or by explicitly calling the destructor for an object of a class type with a non-trivial destructor. For an object of a class type with a non-trivial destructor, the program is not required to call the destructor explicitly before the storage which the object occupies is reused or released; ... [continues on regarding consequences of not calling destructors]

    You have a POD type, so trivial destructor, so you can simply declare verbally "the int objects are all at the end of their lifespan, I'm using the space for floats." You then reuse the space for floats, and no aliasing violation occurs.

提交回复
热议问题