C 指针算术 sizeof(struct)
这是有问题的代码
#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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是,当您进行指针算术时,它会按数据类型大小的倍数递增。
因此,您实际上所做的就是加上
sizeof(struct test)
的平方。由于
sizeof(struct test) = 6
,您将地址增加6 * 6 = 36
。因此,为什么您会得到0x1024
和0xfdc
而不是0x1006
和0xffa
。 (您还切换了+
和-
,但这是一件小事。)相反,只需这样做:
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 by6 * 6 = 36
. Hence why you get0x1024
and0xfdc
instead of0x1006
and0xffa
. (You also switched the+
and-
, but that's a small thing.)Instead, just do this:
当您执行这样的指针算术时,您将向前或向后移动该数量的元素,就好像该变量位于数组中一样。因此,您确实只想使用
a + 1
和a - 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
anda - 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).
我认为您正在寻找
a + 1
和a - 1
。(a + x)
与&a[x]
相同。I think you are looking for
a + 1
anda - 1
.(a + x)
is the same is&a[x]
.你有一个类型指针。
因此,当您将其递增 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 meansa + 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.