开发者

How to assign a specific address to every struct inside a single struct?

开发者 https://www.devze.com 2022-12-07 17:48 出处:网络
Consider the struct: struct MainStruct { struct A a; struct B b; struct C c; }; Normally, a compiler is expected to 开发者_如何学Cplace a, b and c in memory successively with some alignment.

Consider the struct:

struct MainStruct {
    struct A a;
    struct B b;
    struct C c;
};

Normally, a compiler is expected to 开发者_如何学Cplace a, b and c in memory successively with some alignment.

But what if I would like to make variables of a, b and c always start at an exact offset inside mainStruct independent of their sizes? What is a convenient way to do that 'at compile time'?

As an example: mainStruct may be 256 bytes in total, while a always starts at &mainStruct, b always starts at &mainstruct + 100 and c always starts at &mainStruct + 179.

EDIT: The address of mainStruct may be variable. The important point here is that the offsets are always constant. Whenever I define a variable of type Struct MainStruct, that variable should always have the same memory layout with inner structs having constant offsets.

0

精彩评论

暂无评论...
验证码 换一张
取 消