当我们将指针作为参数传递时,C扩展关键字typeof如何工作?

时间:2013-04-02 01:39:21

标签: c++ c assembly x86

我使用GNU编译器并找到类型为typeof(ptr + 1)而不是typeof(ptr)的类型的用法,这很有意思,并推动我找出typeof的用途。

我写测试代码:

struct haha
{
    int a;
    int b;
    void *c;
};

struct haha myhaha;

int main(void)

{

typeof(&myhaha + 1) _tmp = &myhaha;
return 0;
}

并获取汇编代码:

 80483e4:   55                      push   %ebp
 80483e5:   89 e5                   mov    %esp,%ebp
 80483e7:   83 e4 f0                and    $0xfffffff0,%esp
 80483ea:   83 ec 20                sub    $0x20,%esp
 80483ed:   c7 44 24 1c 1c a0 04    movl   $0x804a01c,0x1c(%esp)
 80483f4:   08 
 80483f5:   b8 e0 84 04 08          mov    $0x80484e0,%eax
 80483fa:   c7 44 24 04 1c a0 04    movl   $0x804a01c,0x4(%esp)

我可以找到行80483ed移动myhaha的地址。 typeof(ptr + 1)和typeof(ptr)具有相同的汇编代码。 typeof如何在契约中获取参数的类型?

0 个答案:

没有答案