零指针和结构体偏移量
来源:互联网 发布:什么是程序员考试 编辑:程序博客网 时间:2024/06/02 23:40
零指针和结构体偏移量
转自:http://hi.baidu.com/debugtime/item/42ce11ca5d063d24ef4665c6
定义一个结构体
struct A
{
...
int a;
char b;
short c;
...
}
其中已知int a=100;
取得a的地址 int* address=&a
把0强制转换为A类型的指针(A*)0,则得到一个理论上的以基址0开始的A结构体,
则可以通过&((A*)0->a)的到这个假想结构体的a的地址,由于基址为0,也就是
得到了a元素在结构体中的偏移量,结合之前取得的a的地址int* address;
address-&((A*)0->a),即得到该结构体的基址。强制转换为A类型即可,如下:
(A*)(address-&((A*)0->a)),
----------------------------------------------------------------------------------------------------
DDK中有这样一个宏:
#define CONTAINING_RECORD(address,type,field) ((type*)((PCHAR)(address)-(ULONG_PTR)(&((type*)0)->field)))
就是根据这个原理来获取结构实例基地址的。
CONTAINING_RECORD 宏返回一个结构实例的基地址,该结构的类型和结构所包含的一个域(成员)地址已知。
PCHAR CONTAINING_RECORD(
IN PCHAR Address,
IN TYPE Type,
IN PCHAR Field
);
Parameters
参数
Address
Pointer to a field in an instance of a structure of type Type.
指向Type类型结构实例中某域(成员)的指针。
Type
The name of the type of the structure whose base address is to be returned. For example, type IRP.
需要得到基地址的结构实例的结构类型名。
Field
The name of the field pointed to by Address and which is contained in a structure of type Type.
Type类型结构包含的域(成员)的名称。
Return Value
返回值
Returns the address of the base of the structure containing Field.
返回包含Field域(成员)的结构体的基地址
- 零指针和结构体偏移量
- 结构体偏移量
- 结构体偏移量
- 指针用法之一:给定结构体求某变量的偏移量,给出某变量偏移量求结构体首地址
- 结构体的偏移量
- 结构体的偏移量
- 结构体成员偏移量
- 计算结构体偏移量
- 结构体的偏移量
- C指针偏移量
- 关于结构体的偏移量
- 计算结构体成员地址偏移量...
- 求结构体成员的偏移量
- 结构体偏移量函数offsetof
- 结构体(struct)中的偏移量计算
- 结构体成员偏移量的计算
- 结构体中成员变量偏移量
- 求结构体元素的偏移量
- 如何修改Tomcat服务器Server Locations
- spring mvc+freemarker 简单环境的搭建
- 已经 ship s-off 和 root 的机器,刷非官方 rom 教程。
- C++静态成员函数小结(转)
- webveiw总结(二)
- 零指针和结构体偏移量
- 算法导论 习题 15.4.4
- vs2008+vss2005实现代码管理
- 计算机二级C 想过必看
- 重刷 rom 前的清理准备,RUU 重置问题。
- UNIX文件系统概述
- Hibernate xml配置方法之OneToMany(单向关联关系)
- MFC设置开机启动
- 已经 ship s-off 和 root 的机器,刷非官方 rom 教程。