Memory position of elements in C/C++ union

后端 未结 2 1613
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 17:48

I have a union in C like this:

union AUnion {
  struct CharBuf {
    char *buf;
    size_t len;
  } charbuf;
  uint8_t num;
  double fp_num;
};
2条回答
  •  猫巷女王i
    2020-12-09 18:40

    From my experience, I would say 'yes', though I've checked the C++14 standard and it even guarantees this. (c++11 will most likely have the same effects) Chapter 9.5 states: All non-static data members of a union object have the same address

    So, you can depend on this behavior.

提交回复
热议问题