问题
#include<stdio.h>
#include<string.h>
typedef struct aa{
char a;
int b;
char o[3];
} a;
int main(void) {
a test;
memset(&test, 0, (long)(((a *)NULL)->o));
}
测试
int main(void)
{
a test;
printf("%ld\n",(long)(((a *)NULL)->o));
printf("%ld\n",(long)&(((a *)NULL)->b));
printf("%ld",(long)(((a *)NULL)->a));
}
结果
8
4
segmentation fault
分析
首先我们先看这个结构体类型强制转换是什么意思呢?
结构体就是定义了一段内存空间,指定这段空间的内存布局,比如
struct c {
int a;
char b;
};
我们将其定义为4bytes+1bytes
NULL就是一个类型为void *,值为0的指针,或者说这个指针指向的地址为0。
那么经过(aa *),NULL被认为指向一段地址空间,它的分布是4bytes+4bytes+3bytes。
而(aa *)NULL->o,因为o是数组名(地址常量),所以此时得到是一个地址,这个地址应该是NULL+o在结构体中的偏移量(8)即0+8
而&((aa *)NULL->b)同理,只不过这里b是一个变量名,相当于对NULL+b在结构体偏移量(4)解引用,而后再次取地址,即0+4
而(aa *)NULL->a 和b一样,只是因为没有取地址,所以是解引用非法地址,所以出现了段错误。
验证
既然我们猜想是这样的,那么将NULL换成其他的常量指针也可以,并且计算结果相同。
int main(void) {
a test;
printf("%ld\n",(long)(((a *)1)->o));
printf("%ld\n",(long)&(((a *)2)->b));
printf("%ld\n",(long)&(((a *)3)->a));
}
同理 第一个 是1+8应该是9,第二个应该是2+4,第三个则是3+0
让我们运行一下,看看结果
9
6
3
和我们的预测一致