C++ 中的指针递增
这意味着什么:指针增量指向指针的下一个基类型的地址?
例如:
p1++; // p1 is a pointer to an int
此语句是否意味着 p1
指向的地址应该更改为下一个 int
的地址,或者应该只增加 2(假设 p1
>int 是 2 个字节),在这种情况下,特定地址可能不包含 int
?
我的意思是,如果 p1
是 0x442012,那么 p1++
会是 0x442014 (这可能是 double 地址的一部分)还是指向下一个 int
位于 0x44201F 这样的地址中?
谢谢
What does this mean: that a pointer increment points to the address of the next base type of the pointer?
For example:
p1++; // p1 is a pointer to an int
Does this statement mean that the address pointed to by p1
should change to the address of the next int
or it should just be incremented by 2 (assuming an int
is 2 bytes), in which case the particular address may not contain an int
?
I mean, if p1
is, say, 0x442012, will p1++
be 0x442014 (which may be part of the address of a double) or will it point to the next int
which is in an address like 0x44201F?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
指针算术不关心指针对象的内容或有效性。它只会使用以下公式增加指针地址:(
假设指向非 const 的指针 - 否则转换将不起作用。)
也就是说,它将增加指针的量 < code>sizeof(*p) 字节,无论指针值和内存对齐等。
Pointer arithmetic doesn’t care about the content – or validity – of the pointee. It will simply increment the pointer address using the following formula:
(Assuming a pointer to non-
const
– otherwise the cast wouldn’t work.)That is, it will increment the pointer by an amount of
sizeof(*p)
bytes, regardless of things like pointee value and memory alignment.编译器会将
sizeof(int)
(通常为 4)添加到指针的数值上。如果 p1 在增量之前为 0x442012,则增量之后将为 0x442012 + 4 = 0x442016。请注意,0x442012 不是 4 的倍数,因此它不太可能是有效的四字节 int 的地址,尽管它对于两字节 int 来说没问题。
它肯定不会去寻找下一个整数。这需要魔法。
The compiler will add
sizeof(int)
(usually 4) to the numeric value of the pointer. If p1 is 0x442012 before the increment, then after the increment it will be 0x442012 + 4 = 0x442016.Mind you, 0x442012 is not a multiple of 4, so it is unlikely to be the address of a valid four-byte int, though it would be fine for your two-byte ints.
It certainly won't go looking for the next integer. That would require magic.
p1++ 产生了汇编语言指令,这些指令将 p1 增加它所指向的内容的大小。所以你得到
(char *)p1 = (char *)p1 + sizeof (object point to by p1)
(当这个问题被回答时)通常一个 int 是 4 个字节,所以它会增加 4 ,但这取决于您机器上的 sizeof() 。
它不会转到“下一个int”。
示例:假设有 4 字节地址且 p1 = 0x20424(其中 p1 是 int*)。然后
p1++
会将p1的新值设置为0x20428。不是 0x20425。
p1++ gives rise to assembly language instructions which increment p1 by the size of what it points to. So you get
(char *)p1 = (char *)p1 + sizeof (object pointed to by p1)
(When this question was answered) Typically an int is 4 bytes, so it would increment by 4, but it depends on the sizeof() on your machine.
It does not go to "the next int".
An example: assume a 4 byte address and p1 = 0x20424 (where p1 is an int*). Then
p1++
would set the new value of p1 to 0x20428. NOT 0x20425.
如果
p1
指向int
类型的对象数组的索引n
的元素(非数组对象算作以下数组)为此目的,长度为 1),然后在p1++
之后,p1
是:n+1
的元素(如果数组为)长度大于n+1
。n+1
,则为数组的“最后”地址。如果
p1
未指向int
类型的对象数组的元素,p1++
会导致未定义的行为。C 和 C++ 语言赋予“地址”概念的唯一含义是指针对象的值。
C/C++ 的地址概念与您在汇编语言中考虑的数字地址概念之间的任何关系都纯粹是实现细节(尽管是极其常见的实现细节)。
If
p1
is pointing into the element of indexn
of an array of objects of typeint
(a non-array object counts as an array of length 1 for this purpose), then afterp1++
,p1
is either:n+1
if the array is of length greater thann+1
.n+1
.p1++
causes undefined behavior ifp1
is not pointing to an element of an array of objects of typeint
.The only meaning that the C and C++ languages give to the notion of "address" is the value of a pointer object.
Any relationship that C/C++'s notion of address has to the notion of a numeric addresses you'd consider in assembly language is purely an implementation detail (albeit, an extremely common implementation detail).
指针算术以
sizoeof(*pointer)
倍数完成 - 也就是说,对于指向 int 的指针,增量将前进到下一个整数(或 32 位整数的 4 个字节)。Pointer arithmetic are done in
sizoeof(*pointer)
multiples - that is, for a pointer to int, increment will advance to the next integer (or 4 bytes for 32 bit integers).