C++ 中的指针递增

发布于 2024-11-17 08:26:25 字数 401 浏览 1 评论 0原文

这意味着什么:指针增量指向指针的下一个基类型的地址?
例如:

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 技术交流群。

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

发布评论

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

评论(5

ヤ经典坏疍 2024-11-24 08:26:25

指针算术不关心指针对象的内容或有效性。它只会使用以下公式增加指针地址:(

new_value = reinterpret_cast<char*>(p) + sizeof(*p);

假设指向非 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:

new_value = reinterpret_cast<char*>(p) + sizeof(*p);

(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.

℉服软 2024-11-24 08:26:25

编译器会将 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.

薆情海 2024-11-24 08:26:25

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.

楠木可依 2024-11-24 08:26:25

如果 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 index n of an array of objects of type int (a non-array object counts as an array of length 1 for this purpose), then after p1++, p1 is either:

  • Pointing to the element of index n+1 if the array is of length greater than n+1.
  • The 'past-the-end' address of the array, if the array is of length exactly n+1.

p1++ causes undefined behavior if p1 is not pointing to an element of an array of objects of type int.

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).

烟花肆意 2024-11-24 08:26:25

指针算术以 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).

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