Correct, portable way to interpret buffer as a struct

前端 未结 2 722
一向
一向 2020-12-13 02:28

The context of my problem is in network programming. Say I want to send messages over the network between two programs. For simplicity, let\'s say messages look like this, a

相关标签:
2条回答
  • 2020-12-13 02:50

    The only correct way is, as you surmised, to copy the data from the char buffer into your structure. Your other alternatives violate the strict alias rules, or the one-member-of-union-active rule.

    I do want to take one more moment to remind you that even if you do this on a single host and byte order doesn't matter you still have to make sure that both ends of the connection arae built with the same options and that the struct is padded in the same way, the types are the same size, etc. I suggest taking at least a small amount of time considering a real serialization implementation so that if you ever need to support a wider array of conditions you don't have a big update in front of you then.

    0 讨论(0)
  • 2020-12-13 02:59

    I guess this is what I've been trying to avoid, but I finally went and took a look at the C99 standard myself. Here's what I've found (emphasis added):
    §6.3.2.2 void

    1 The (nonexistent) value of a void expression (an expression that has type void) shall not be used in any way, and implicit or explicit conversions (except to void) shall not be applied to such an expression. If an expression of any other type is evaluated as a void expression, its value or designator is discarded. (A void expression is evaluated for its side effects.)

    §6.3.2.3 Pointers

    1 A pointer to void may be converted to or from a pointer to any incomplete or object type. A pointer to any incomplete or object type may be converted to a pointer to void and back again; the result shall compare equal to the original pointer.

    And §3.14

    1 object
    region of data storage in the execution environment, the contents of which can represent values

    §6.5

    An object shall have its stored value accessed only by an lvalue expression that has one of the following types:
    a type compatible with the effective type of the object,
    — a qualified version of a type compatible with the effective type of the object,
    — a type that is the signed or unsigned type corresponding to the effective type of the object,
    — a type that is the signed or unsigned type corresponding to a qualified version of the effective type of the object,
    — an aggregate or union type that includes one of the aforementioned types among its
    members (including, recursively, a member of a subaggregate or contained union), or
    — a character type.

    §6.5

    The effective type of an object for an access to its stored value is the declared type of the
    object, if any. If a value is stored into an object having no declared type through an lvalue having a type that is not a character type, then the type of the lvalue becomes the effective type of the object for that access and for subsequent accesses that do not modify the stored value. If a value is copied into an object having no declared type using memcpy or memmove, or is copied as an array of character type, then the effective type of the modified object for that access and for subsequent accesses that do not modify the value is the effective type of the object from which the value is copied, if it has one. For all other accesses to an object having no declared type, the effective type of the object is simply the type of the lvalue used for the access.

    §J.2 Undefined Behavior

    — An attempt is made to use the value of a void expression, or an implicit or explicit conversion (except to void) is applied to a void expression (6.3.2.2).

    Conclusion

    It is ok (well-defined) to cast to-and-from a void*, but not ok to use a value of type void in C99. Therefore the "real world example" is not undefined behavior. Therefore, the explicit casting method can be used with the following modification, as long as alignment, padding, and byte-order is taken care of:

    void receive_message(void *bytes, size_t len) {
        assert(len >= sizeof(struct message);
        struct message *msg = (struct message*) bytes;
        /* And now use the message */
        if (msg->command == SELF_DESTRUCT)
            /* ... */
    }
    
    0 讨论(0)
提交回复
热议问题