C++ 为什么 int 指针 '++' 递增 4 而不是 1?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5610298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does int pointer '++' increment by 4 rather than 1?
提问by Jason
Value of a pointer is address of a variable. Why value of an int pointer
increased by 4-bytes after the int pointer increased by 1.
指针的值是变量的地址。为什么int pointer
在 int 指针增加 1 后 a 的值增加了 4 个字节。
In my opinion, I think value of pointer(address of variable) only increase by 1-byte after pointer increment.
在我看来,我认为指针(变量地址)的值在指针增加后只会增加 1 个字节。
Test code:
测试代码:
int a = 1, *ptr;
ptr = &a;
printf("%p\n", ptr);
ptr++;
printf("%p\n", ptr);
Expected output:
预期输出:
0xBF8D63B8
0xBF8D63B9
Actually output:
实际输出:
0xBF8D63B8
0xBF8D63BC
EDIT:
编辑:
Another question - How to visit the 4 bytes an int
occupies one by one?
另一个问题-如何访问一个一个int
占用的4个字节?
回答by GManNickG
When you increment a T*
, it moves sizeof(T)
bytes.?This is because it doesn't make sense to move any other value: if I'm pointing at an int
that's 4 bytes in size, for example, what would incrementing less than 4 leave me with? A partial int
mixed with some other data: nonsensical.
当您增加 a 时T*
,它会移动sizeof(T)
字节。? 这是因为移动任何其他值都没有意义:例如,如果我指向一个int
大小为 4 个字节的值,那么增量小于 4 会给我留下什么?部分int
与其他一些数据混合:无意义。
Consider this in memory:
在内存中考虑这一点:
[↓ ]
[...|0 1 2 3|0 1 2 3|...]
[...|int |int |...]
Which makes more sense when I increment that pointer? This:
当我增加那个指针时哪个更有意义?这个:
[↓ ]
[...|0 1 2 3|0 1 2 3|...]
[...|int |int |...]
Or this:
或这个:
[↓ ]
[...|0 1 2 3|0 1 2 3|...]
[...|int |int |...]
The last doesn't actually point an any sort of int
. (Technically, then, using that pointer is UB.)
最后一个实际上并没有指向任何类型的int
. (从技术上讲,使用该指针是UB。)
If you reallywant to move one byte, increment a char*
: the size of of char
is always one:
如果你真的想移动一个字节,增加 a char*
:的大小char
总是一:
int i = 0;
int* p = &i;
char* c = (char*)p;
char x = c[1]; // one byte into an int
?A corollary of this is that you cannot increment void*
, because void
is an incomplete type.
? 一个推论是你不能 increment void*
,因为void
是一个不完整的类型。
回答by Ken Rockot
Pointer increment is based on the size of the type pointed to. If an int is 4 bytes, incrementing an int* by 1 will increase its value by 4.
指针增量基于指向的类型的大小。如果 int 为 4 个字节,则将 int* 增加 1 会将其值增加 4。
If a short is 2 bytes, incrementing a short* by 1 will increase its value by 2.
如果 short 是 2 个字节,则将 short* 增加 1 会将其值增加 2。
This is standard behavior for C pointer arithmetic.
这是 C 指针算法的标准行为。
回答by MByD
Pointers are increased by the size of the type they point to, if the pointer points to char, pointer++
will increment pointer by 1, if it points to a 1234 bytes struct, pointer++
will increment the pointer by 1234.
指针随着它们指向的类型的大小而增加,如果指针指向char,pointer++
则将pointer加1,如果指向1234字节的struct,pointer++
则将指针加1234。
This may be confusing first time you meet it, but actually it make a lot of sense, this is not a special processor feature, but the compiler calculates it during compilation, so when you write pointer+1
the compiler compiles it as pointer + sizeof(*pointer)
第一次遇到这个可能会很迷惑,但其实很有道理,这不是一个特殊的处理器特性,而是编译器在编译的时候计算的,所以当你写pointer+1
的时候编译器会编译成pointer + sizeof(*pointer)
回答by Pablo
The idea is that after incrementing, the pointer points to the next int in memory. Since ints are 4 bytes wide, it is incremented by 4 bytes. In general, a pointer to type T will increment by sizeof(T)
这个想法是在递增后,指针指向内存中的下一个 int。由于整数是 4 个字节宽,因此它增加了 4 个字节。通常,指向类型 T 的指针将按 sizeof(T) 递增
回答by Amir Rachum
As you said, an int pointer
points to an int
. An int
usually takes up 4 bytes and therefore, when you increment the pointer, it points to the "next" int
in the memory - i.e., increased by 4 bytes. It acts this way for any size of type. If you have a pointer to type A
, then incrementing a A*
it will increment by sizeof(A)
.
正如你所说, anint pointer
指向 an int
。Anint
通常占用 4 个字节,因此,当您增加指针时,它指向int
内存中的“下一个” ——即增加 4 个字节。对于任何大小的类型,它都以这种方式起作用。如果您有一个指向 type 的指针A
,则递增 aA*
它将递增sizeof(A)
。
Think about it - if you only increment the pointer by 1 byte, than it will point to a middle of an int
and I can't think of an opportunity where this is desired.
想一想 - 如果你只将指针增加 1 个字节,那么它会指向 an 的中间,int
我想不出有机会这样做。
This behavior is very comfortable when iterating over an array, for example.
例如,在迭代数组时,这种行为非常舒适。
回答by Paolo
A pointer points at the BEGINNING of something in memory. An INT occupies 4 bytes (32bit) and a DOUBLE occupies 8 bytes (64bit) in memory. So if you have a DOUBLE number stored, and you wish at a very low level pointing to the next available memory location, the pointer wooud be increased by 8 bytes. If for some reason you pointed at +4bytes from the start of a DOUBLE value, you would corrupt it's value. Memory is a very large flat field that has no conscience of itself, so it's up to the software to divides it properly and to "respect the borders" of items located in that field.
一个指针指向内存中某物的 BEGINNING。一个INT在内存中占用4个字节(32bit),一个DOUBLE占用8个字节(64bit)。因此,如果您存储了一个 DOUBLE 数字,并且您希望在一个非常低的级别指向下一个可用的内存位置,则该指针将增加 8 个字节。如果出于某种原因您从 DOUBLE 值的开头指向 +4bytes,您将破坏它的值。内存是一个非常大的平坦领域,它本身没有良心,因此软件可以正确划分它并“尊重位于该领域的项目的边界”。