Legit Uses of the offsetof Macro in C / C++

前端 未结 5 1657
囚心锁ツ
囚心锁ツ 2021-01-02 13:45

There is this macro offsetof in C/C++ which allows you to get the address offset of a member in a POD structure. For an example from the C FAQ:

struct foo {         


        
5条回答
  •  一整个雨季
    2021-01-02 14:12

    One legitimate use of offsetof() is to determine the alignment of a type:

    #define ALIGNMENT_OF( t ) offsetof( struct { char x; t test; }, test )
    

    It may be a bit low-level to need the alignment of an object, but in any case I'd consider this a legitimate use.

提交回复
热议问题