class Dog(Structure):
_fields_ = [('name', c_char_p), ('weight', c_int)]
Dog.name.offset
# 0
Dog.weight.offset
# 4 (on my 32-bit system)
The task of turning this into a method is left to the reader :)
Answer from Sven Marnach on Stack Overflowclass Dog(Structure):
_fields_ = [('name', c_char_p), ('weight', c_int)]
Dog.name.offset
# 0
Dog.weight.offset
# 4 (on my 32-bit system)
The task of turning this into a method is left to the reader :)
The trouble is that structure members are sometimes returned as plain python types. For example
class Test(Structure):
_fields_ = [('f1', c_char), ('f2', c_char * 0)]
type(Test().f1) is type(Test().f2) is str
Videos
class Dog(Structure):
_fields_ = [('name', c_char_p), ('weight', c_int)]
Dog.name.offset
# 0
Dog.weight.offset
# 4 (on my 32-bit system)
The task of turning this into a method is left to the reader :)
Answer from Sven Marnach on Stack OverflowThe approach you've outlined is roughly correct, although you should use offsetof instead of attempting to figure out the offset on your own. I'm not sure why you mention memset -- it sets the contents of a block to a specified value, which seems quite unrelated to the question at hand.
Here's some code to demonstrate how it works:
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
typedef struct x {
int member_a;
int member_b;
} x;
int main() {
x *s = malloc(sizeof(x));
char *base;
size_t offset;
int *b;
// initialize both members to known values
s->member_a = 1;
s->member_b = 2;
// get base address
base = (char *)s;
// and the offset to member_b
offset = offsetof(x, member_b);
// Compute address of member_b
b = (int *)(base+offset);
// write to member_b via our pointer
*b = 10;
// print out via name, to show it was changed to new value.
printf("%d\n", s->member_b);
return 0;
}
The full technique:
Get the offset using offsetof:
b_offset = offsetof(struct mystruct, member_b);
Get the address of your structure as a char * pointer.
char *sc = (char *)s;
Add the add the offset to the structure address, cast the value to a pointer to the appropriate type and dereference:
*(int *)(sc + b_offset)
R.. is correct in his answer to the second part of your question: this code is not advised when using a modern C compiler.
But to answer the first part of your question, what this is actually doing is:
(
(int)( // 4.
&( ( // 3.
(a*)(0) // 1.
)->b ) // 2.
)
)
Working from the inside out, this is ...
- Casting the value zero to the struct pointer type
a* - Getting the struct field b of this (illegally placed) struct object
- Getting the address of this
bfield - Casting the address to an
int
Conceptually this is placing a struct object at memory address zero and then finding out at what the address of a particular field is. This could allow you to figure out the offsets in memory of each field in a struct so you could write your own serializers and deserializers to convert structs to and from byte arrays.
Of course if you would actually dereference a zero pointer your program would crash, but actually everything happens in the compiler and no actual zero pointer is dereferenced at runtime.
In most of the original systems that C ran on the size of an int was 32 bits and was the same as a pointer, so this actually worked.
It has no advantages and should not be used, since it invokes undefined behavior (and uses the wrong type - int instead of size_t).
The C standard defines an offsetof macro in stddef.h which actually works, for cases where you need the offset of an element in a structure, such as:
#include <stddef.h>
struct foo {
int a;
int b;
char *c;
};
struct struct_desc {
const char *name;
int type;
size_t off;
};
static const struct struct_desc foo_desc[] = {
{ "a", INT, offsetof(struct foo, a) },
{ "b", INT, offsetof(struct foo, b) },
{ "c", CHARPTR, offsetof(struct foo, c) },
};
which would let you programmatically fill the fields of a struct foo by name, e.g. when reading a JSON file.
The offsetof() macro takes two arguments. The C99 standard says (in §7.17 <stddef.h>):
offsetof(type, member-designator)which expands to an integer constant expression that has type
size_t, the value of which is the offset in bytes, to the structure member (designated by member-designator), from the beginning of its structure (designated by type). The type and member designator shall be such that givenstatic type t;then the expression
&(t.member-designator)evaluates to an address constant.
So, you need to write:
offsetof(struct mystruct1, u_line.line);
However, we can observe that the answer will be zero since mystruct1 contains a union as the first (and only) member, and the line part of it is one element of the union, so it will be at offset 0.
Your struct mystruct1 has 1 member named u_line. You can see the offset of that member
offsetof(struct mystruct1, u_line); // should be 0
or of members down the line if you specify each "level of parenthood"
offsetof(struct mystruct1, u_line.line);
offsetof(struct mystruct1, u_line.line.buf);
offsetof(struct mystruct1, u_line.logo);