offsetof

How to set structure element at desired offset

╄→гoц情女王★ 提交于 2021-02-07 13:34:58
问题 In embedded programming when describing the hardware one often needs to place struct elements at known predefined positions as the HW engineer designed them. For example, let's define a structure FPGA, which has some 100 registers/areas as in the following simplified example: struct __attribute__ ((__packed__)) sFPGA { uchar Spare1[0x24]; ushort DiscreteInput; uchar Spare2[0x7A]; //CPLD_Version is required to be at offset 0xA0, so 0xA0-0x24-2=0x7A ushort CPLD_Version; }; Now, I am frustrated

How to set structure element at desired offset

可紊 提交于 2021-02-07 13:33:27
问题 In embedded programming when describing the hardware one often needs to place struct elements at known predefined positions as the HW engineer designed them. For example, let's define a structure FPGA, which has some 100 registers/areas as in the following simplified example: struct __attribute__ ((__packed__)) sFPGA { uchar Spare1[0x24]; ushort DiscreteInput; uchar Spare2[0x7A]; //CPLD_Version is required to be at offset 0xA0, so 0xA0-0x24-2=0x7A ushort CPLD_Version; }; Now, I am frustrated

How to set structure element at desired offset

风流意气都作罢 提交于 2021-02-07 13:32:35
问题 In embedded programming when describing the hardware one often needs to place struct elements at known predefined positions as the HW engineer designed them. For example, let's define a structure FPGA, which has some 100 registers/areas as in the following simplified example: struct __attribute__ ((__packed__)) sFPGA { uchar Spare1[0x24]; ushort DiscreteInput; uchar Spare2[0x7A]; //CPLD_Version is required to be at offset 0xA0, so 0xA0-0x24-2=0x7A ushort CPLD_Version; }; Now, I am frustrated

Does this implementation of offsetof invoke undefined behavior? [duplicate]

强颜欢笑 提交于 2021-02-04 06:28:47
问题 This question already has answers here : Why does this implementation of offsetof() work? (8 answers) Closed 1 year ago . offsetof is defined like this in stddef.h : #define offsetof(type, member) ((size_t)&((type *)0)->member) Does this invoke undefined behavior due to the dereference of a NULL pointer? If not, why? 回答1: In normal C code, the behavior of ((size_t)&((type *)0)->member) is not specified by the C standard: First, per C 2018 6.5.2.3 4, about -> , ((type *)0)->member designates

Does this implementation of offsetof invoke undefined behavior? [duplicate]

佐手、 提交于 2021-02-04 06:28:05
问题 This question already has answers here : Why does this implementation of offsetof() work? (8 answers) Closed 1 year ago . offsetof is defined like this in stddef.h : #define offsetof(type, member) ((size_t)&((type *)0)->member) Does this invoke undefined behavior due to the dereference of a NULL pointer? If not, why? 回答1: In normal C code, the behavior of ((size_t)&((type *)0)->member) is not specified by the C standard: First, per C 2018 6.5.2.3 4, about -> , ((type *)0)->member designates

Is it possible to get a pointer to one subobject via a pointer to a different, unreleated subobject?

浪尽此生 提交于 2020-06-22 11:05:34
问题 Look at this simple code: struct Point { int x; int y; }; void something(int *); int main() { Point p{1, 2}; something(&p.x); return p.y; } I expect, that main 's return value can be optimized to return 2; , as something doesn't have access to p.y , it only gets a pointer to p.x . But, none of the major compilers optimize the return value of main to 2 . Godbolt. Is there something in the standard, which allows something to modify p.y , if we only give access to p.x ? If yes, does this depend

C - Reference after dereference terminology

徘徊边缘 提交于 2020-01-24 07:42:25
问题 This question is about terminology. int main() { unsigned char array[10] = {0}; void *ptr = array; void *middle = &ptr[5]; // <== dereferencing ‘void *’ pointer } Gcc emits the warning Dereferencing void pointer . I understand the warning because the compiler needs to compute the actual offset, and it couldn't because void has no standard size. But I disagree with the error message. This is not a dereference. I can't find a dereference explanation where it is something else than taking value

C - Reference after dereference terminology

北慕城南 提交于 2020-01-24 07:41:00
问题 This question is about terminology. int main() { unsigned char array[10] = {0}; void *ptr = array; void *middle = &ptr[5]; // <== dereferencing ‘void *’ pointer } Gcc emits the warning Dereferencing void pointer . I understand the warning because the compiler needs to compute the actual offset, and it couldn't because void has no standard size. But I disagree with the error message. This is not a dereference. I can't find a dereference explanation where it is something else than taking value

Get address of a non-POD object from within a data member, which is a single-use nested class

吃可爱长大的小学妹 提交于 2019-12-22 00:43:04
问题 I'll start with some code: class myNonPODClass { public: virtual ~myNonPODClass() {} class { public: myNonPODClass* GetContainer() { return (myNonPODClass*)((int8_t*)(this) - offsetof(myNonPODClass, member)); } } member; }; Obviously, this is a contrived example. The code compiles fine, but I'm worried about the "Offset of on non-POD type 'myNonPODClass'". Is there a better way to do essentially the same thing WITHOUT having to pass the myNonPODClass pointer into the nested anonymous classes

Standard way to find base address of struct from a member

五迷三道 提交于 2019-12-20 02:57:27
问题 struct Data { int a; std::string b; float c; }; std::string* allocateDataAndGetString() { Data* dataPtr(someAllocator.allocate<Data>()); return &dataPtr.b; } Data* getBaseDataPtrFromString(std::string* mStringMember) { // ??? } int main() { std::string* stringPtr(allocateDataAndGetString()); Data* dataPtr(getBaseDataPtrFromString } I have a Data instance allocated on the heap, and a pointer to its std::string b; member. How do I get the base address of the Data instance the string is a member