0

在此处输入图像描述

有人可以向我解释为什么在第二个问题 proco2 中,x-> bi -> f[3]?

4

2 回答 2

3

实际上,在堆栈上,您只有一个指向struct s2(in 8(%ebp)) 的指针。因此之后

movl 8(%ebp), %eax

%eax你有一个地址struct s2

struct s2构成的第8-11f[0]个字节和第 12-15 个字节构成f[1],因此您有

return x->f[1]

在第二种情况下

movl 8(%ebp), %eax

%eax你有一个地址struct s1

的第 4-7 个字节struct s1构成b类型字段union u1。因此之后

movl 4(%eax), %eax

%eax你有union u1。因为它union同时%eax包含所有字段值(hij。所以

movl 20(%eax), %eax

实际上正在获取任何指针所在的第 20-23 个字节%eax(它不能是j字段,因为它不是指针)。它不能是h字段,因为它指向struct s1并且sizeof (struct s1)是 12,即 <20。因此,它必须是i场。的第 20-23 个字节struct s2f[3],因此您有:

return x->b.i->f[3]
于 2012-11-14T00:54:19.860 回答
0

mov 8(%ebp), %eax ;; eax = &x
mov 4(%eax), %eax ;; eax = x->b (or x->e)

More likely x->e would have had movsbl 4(%eax), %eax //; sign extension
or mov 4(%eax), %al with sign extension

Because char can't (shouldn't) be used as a pointer, the 4(%eax) was a ptr to struct. Which struct? 20%(eax) exists only for struct s2, unless originally there was an array os structs s1.

Offset 20 of s2 is f[3].

于 2012-11-14T05:33:35.887 回答