First, we need to correct a familiar notion that pointers are more than "addresses", and pointers have a very important feature, which is "type".
When the pointer is initialized, theright operand of "=" must be the address of the in-memory data, cannot be a variable, and cannot be directly used with an integer address value (but int *p = 0; except that the statement indicates that the pointer is empty );
So int *p = ten; such code is not allowed. In C + + is directly error, even in some C compiler in the form of warning hint, but warning sometimes very serious. So don't use this kind of stuff. There is no hermit conversion from const int to int*.
The correct way to use it is int *p = (int*) , so that the address of 10 is incremented by one type and then assigned to int *p. Look at the compilation of this sentence:
00401048 mov dword ptr [ebp-4],0ah
is to put 10 in the corresponding memory space p.
You can see that you can add a type to an address so that it forms a pointer, but the pointer is like a constant, like 10, which is the int type by default, does not produce code, so (int*) 10 does not produce assembly code. So there's no way to access the protected area of memory this happens.
so let's start with the macro definition to find the structure member offset.
When we write the C program, we sometimes need to get the address of the struct from the address of the struct member variable, especially when we want to use C to realize the inheritance of C + +.
Our analysis of the problem is as follows:
Input: A struct defines the type, the name of a member variable in the struct member and its address PTR output: The address of the struct containing this member variable
To facilitate the analysis, we give an example to illustrate
struct father_t {int a;char *b;double C;} F;char *ptr = & (F.B);//rather than PTR = f.b; Here ptr is the address of B, not the address it points to. Based on the C language's storage characteristics for struct types, we can draw a diagram like this:
Note: f.b is a pointer to the CHAR*B first character,& (F.B) is the address of this pointer.
By analyzing the diagram, we can see that we just need to make the address of the currently known member variable PTR, minus its relative offset of 4 in the struct to the address of the struct (ptr-4).
In Linux there is a good macro to use, called container_of, placed in the Linux/kernel.h . It is defined as follows:
#define OFFSETOF (Type, MEMBER) ((size_t) & ((TYPE *) 0)->member)
Note: The input of this macro definition is the type of struct, a member variable of the struct body.
Macro function: Gets the offset of a struct variable member in this struct body. 1. ((TYPE *) 0) transforms 0 to type pointer; 2. ((TYPE *) 0)->member access to data members in the structure; 3. & ((TYPE *) 0)->member The address of the data member, which is the offset from 0, which is the 4. (size_t) (& (((type*) 0)->member) The result conversion type, size_t should eventually be the unsigned int type. The trick of this macro is to convert 0 to (type*) so that the address of the member in the struct is the offset in this struct.
This is just using the 0 address so that it has the type and does not have access to the data in the memory space corresponding to the 0 address, so the read and write Protected Storage area does not occur.
#define OFFSET (type, a) ((unsigned int) & ((type*) 0)->a) struct _test_{int a;char x;double D;char];}; int main () {cout << offset (struct _test_, ch) << Endl;return 0;}
The result is exactly 16 according to the way the body is aligned in the structure.
Reference article: http://www.cnblogs.com/youxin/p/3348227.html
Direct assignment of pointers to integers and the use of macro definitions to find the structure member offsets