C 指针算术 sizeof(struct)

发布于 2024-12-21 10:45:29 字数 594 浏览 6 评论 0原文

这是有问题的代码

#include <stdio.h>

struct test {
    unsigned char t;
    unsigned short u;
    unsigned char v;
};


int main ()
{
    struct test  * a = (void *) 0x1000;

    printf("%x %p %p\n",
           sizeof(struct test),
           a + sizeof(struct test),
           a - sizeof(struct test));

    return 0;
}

sizeof(struct test) 打印 6,所以我期望看到:

6 0xffa 0x1006

相反,我得到

6 0x1024 0xfdc

Last time I check, 0x24, 或36,不等于 6。它甚至与我所知的任何东西都不相符。我完全不知所措。

有人可以向我解释为什么我会得到这些值吗?

Here is the code in question

#include <stdio.h>

struct test {
    unsigned char t;
    unsigned short u;
    unsigned char v;
};


int main ()
{
    struct test  * a = (void *) 0x1000;

    printf("%x %p %p\n",
           sizeof(struct test),
           a + sizeof(struct test),
           a - sizeof(struct test));

    return 0;
}

The sizeof(struct test) prints 6, so I would expect to see:

6 0xffa 0x1006

Instead I get

6 0x1024 0xfdc

Last time I checked, 0x24, or 36, was not equal to 6. It's not even aligned to anything that I can tell. I am at a complete loss.

Can someone please explain to me why I'm getting these values?

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

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

发布评论

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

评论(4

七颜 2024-12-28 10:45:29

问题是,当您进行指针算术时,它会按数据类型大小的倍数递增。

因此,您实际上所做的就是加上 sizeof(struct test) 的平方。

由于 sizeof(struct test) = 6,您将地址增加 6 * 6 = 36。因此,为什么您会得到 0x10240xfdc 而不是 0x10060xffa。 (您还切换了 +-,但这是一件小事。)

相反,只需这样做:

printf("%x %p %p\n",
       sizeof(struct test),
       a + 1,
       a - 1);

The problem is that when you do pointer arithmetic, it increments by a multiple of the size of the datatype.

So what you're effectively doing is adding by the square of sizeof(struct test).

Since sizeof(struct test) = 6, you are incrementing the address by 6 * 6 = 36. Hence why you get 0x1024 and 0xfdc instead of 0x1006 and 0xffa. (You also switched the + and -, but that's a small thing.)

Instead, just do this:

printf("%x %p %p\n",
       sizeof(struct test),
       a + 1,
       a - 1);
梦在深巷 2024-12-28 10:45:29

当您执行这样的指针算术时,您将向前或向后移动该数量的元素,就好像该变量位于数组中一样。因此,您确实只想使用 a + 1a - 1,每次应前进 6 个字节。

重要提示:请记住,编译器可以在结构中添加填充以帮助对齐。不要仅仅因为您有两个一字节字符和一个两字节短字符而假设您的结构体大小为 4 个字节,但这里的情况并非如此。 (事实上​​,不要假设您知道 char 或 Short 的大小;我以前见过 2 字节字符)。

When you do pointer arithmetic like this, you move forward or back by that number of elements, as though that variable were in an array. So you really want to just use a + 1 and a - 1, which should advance by 6 bytes each time.

IMPORTANT: Keep in mind that the compiler can add padding in your struct to help with alignment. Don't just assume that because you have two one-byte chars and a two-byte short that your struct will be 4 bytes in size---this isn't the case here. (In fact, don't assume that you know the size of char or short; I've seen 2-byte chars before).

×眷恋的温暖 2024-12-28 10:45:29

我认为您正在寻找 a + 1a - 1

(a + x)&a[x] 相同。

I think you are looking for a + 1 and a - 1.

(a + x) is the same is &a[x].

内心荒芜 2024-12-28 10:45:29

你有一个类型指针。

因此,当您将其递增 1(即 a + 1)时,它意味着 a + sizeof(type)

所以 a + sizeof(type) = a + sizeof(type) * sizeof(type) = a + 6 * 6 (在你的情况下as sizeof(test) = 6)

这就是您获取 0x24 或 36 的地方。

You have a typed pointer.

So when you increment it my 1 (i.e. a + 1) it means a + sizeof(type).

So a + sizeof(type) = a + sizeof(type) * sizeof(type) = a + 6 * 6 (in your case as sizeof(test) = 6)

That's where you are getting 0x24 or 36 from.

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