这里发生了什么:v5 = *(_Byte *)(this + 4)?

发布于 2024-10-06 14:35:29 字数 199 浏览 7 评论 0原文

我正在查看 IDA pro 的代码转储。有一个函数如下布局:

garbled_name(int this...
    unsigned int v5 ;
    v5 = *(_Byte *)(this + 4);
    ...

我真正好奇的是“+ 4”到底在做什么?这是补充还是其他什么?

谢谢

I am looking at a code dump from IDA pro. There is a function which as this layout:

garbled_name(int this...
    unsigned int v5 ;
    v5 = *(_Byte *)(this + 4);
    ...

What I am really curious about is what exactly the '+ 4' is doing? Is this an addition or something else?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

怪异←思 2024-10-13 14:35:29

该代码采用整数“this”,加 4,将其转换为指向字节的指针,然后将“v5”设置为该地址处的字节值。

The code takes the integer 'this', adds 4 to it, casts it to a pointer to a byte, and then sets 'v5' to the value of the byte at that address.

花开柳相依 2024-10-13 14:35:29

它只是 C++ 类的成员函数,this 是指向对象的指针。该对象的签名可能是:

class some_class {
    int i;    // int, void*, short, anything with sizeof() <= 4, and it's not char. 
              // It also can be absent if it's a virtual class (AFAIK it's compiler dependend)
    unsigned char c; // or c[N]
    ...
};

有问题的代码是:

some_class::some_fn(...){
    unsigned int v5 = c; // or c[0]
    ...
};

It's just a member function of a C++ class, this being pointer to the object. This signature of the object is probably:

class some_class {
    int i;    // int, void*, short, anything with sizeof() <= 4, and it's not char. 
              // It also can be absent if it's a virtual class (AFAIK it's compiler dependend)
    unsigned char c; // or c[N]
    ...
};

The code in question is:

some_class::some_fn(...){
    unsigned int v5 = c; // or c[0]
    ...
};
烟花肆意 2024-10-13 14:35:29

它是对对象开头的第五个字节的引用。根据生成该代码的编译器,它很可能是类顺序中位于对象实例中第五个字节的项。

It is a reference to the fifth byte from the beginning of the object. Depending on what compiler generated that code, it is most likely the item in class order which is at the fifth byte in the object instance.

多像笑话 2024-10-13 14:35:29

编辑:叹息,我错过了“IDA Pro”部分。我将其留在这里只是为了娱乐价值,以防有人想知道“this+4”在普通 C++ 代码中的作用。

<罢工>
“this+4”将当前的 this 指针向前移动四倍。然后它将其转换为字节指针并读取它。

考虑一下:

struct A {
    void foo();

    int x;
    int y;
};

在 32 位系统上,sizeof(A) 最有可能是 8 个字节。

A myArray[8];
A *pA = myArray;

现在 pA 指向 &myArray[0]。

pA++;

现在 pA 指向 &myArray[1],即它向前移动了 8 个字节。

void A::foo() {
    A *pA = this + 4;
}

如果您在 &myArray[0] 上调用此函数,它将指向 &myArray[4],即接下来的 32 个字节。

EDIT: Sigh, I missed the "IDA Pro" part. I'll just leave this here for entertainment value, in case someone is wondering what "this+4" does in normal C++ code.


"this+4" takes your current this pointer, moves forward four times its size. Then it casts that to a byte pointer and reads it.

Consider this:

struct A {
    void foo();

    int x;
    int y;
};

sizeof(A), on a 32-bit system, is most likely 8 bytes.

A myArray[8];
A *pA = myArray;

Now pA points to &myArray[0].

pA++;

Now pA points to &myArray[1], i.e. it moved 8 bytes forward.

void A::foo() {
    A *pA = this + 4;
}

If you call this on &myArray[0], it will point to &myArray[4], i.e. 32 bytes further down the road.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文