struct members memory layout

前端 未结 6 2023
既然无缘
既然无缘 2021-01-04 14:00

If I have a struct like this:

struct S {
    ANY_TYPE a;
    ANY_TYPE b;
    ANY_TYPE c;
} s;

Can I safely assume that the following assump

6条回答
  •  难免孤独
    2021-01-04 15:04

    1. Yes (as long as sizeof(ANY_TYPE) is not 0. Some compilers allow it, which is non-standard -- see Can sizeof return 0 (zero)). You'd be safe with <= or just assume a standard compiler.

    2. Yes

    And in C++ too.

    Pointer comparison only makes sense inside of arrays and structs/classes, not generally.

提交回复
热议问题