为什么int指针'++'增加4而不是1?

时间:2020-12-16 16:51:45

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指针的值在int指针增加1之后增加了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("0x%X\n", ptr);
ptr++;
printf("0x%X\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?

另一个问题——如何访问一个整数占用的4个字节?

5 个解决方案

#1


92  

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.

当你增加一个T*时,它会移动sizeof(T)字节。这是因为移动其他值是没有意义的:如果我指向一个大小为4字节的整数,例如,在小于4的情况下会增加多少?与其他数据混合的部分整数:荒谬的。


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 really want to move one byte, increment a char*: the size of of char is always one:

如果你真的想移动一个字节,增加一个字符*:字符的大小总是1:

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.

必然的结果是你不能增加void*,因为void是一个不完整的类型。

#2


8  

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* by 1将使其值增加4。

If a short is 2 bytes, incrementing a short* by 1 will increase its value by 2.

如果短是2字节,将短*增加1将使其值增加2。

This is standard behavior for C pointer arithmetic.

这是C指针算法的标准行为。

#3


2  

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,指针++ +将增加指针1,如果指针指向1234字节结构,指针++ +将增加指针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)

这可能会让你第一次遇到它时感到困惑,但实际上它很有意义,这不是一个特殊的处理器特性,但是编译器会在编译过程中计算它,所以当你编写指针+1时编译器会将它编译成指针+ sizeof(*指针)

#4


1  

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型。由于ints宽4字节,所以增加了4字节。一般来说,一个指向T类型的指针会通过sizeof(T)递增

#5


1  

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

正如您所说的,int指针指向int. int通常占用4字节,因此,当您增加指针时,它指向内存中的“下一个”int—即。,增加了4个字节。它以这种方式对任何类型进行操作。如果您有一个指向a的指针,那么递增a *,它会递增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个字节,那么它就会指向一个中间值,我想不出一个合适的机会。

This behavior is very comfortable when iterating over an array, for example.

例如,当迭代数组时,这种行为非常舒适。

#1


92  

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.

当你增加一个T*时,它会移动sizeof(T)字节。这是因为移动其他值是没有意义的:如果我指向一个大小为4字节的整数,例如,在小于4的情况下会增加多少?与其他数据混合的部分整数:荒谬的。


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 really want to move one byte, increment a char*: the size of of char is always one:

如果你真的想移动一个字节,增加一个字符*:字符的大小总是1:

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.

必然的结果是你不能增加void*,因为void是一个不完整的类型。

#2


8  

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* by 1将使其值增加4。

If a short is 2 bytes, incrementing a short* by 1 will increase its value by 2.

如果短是2字节,将短*增加1将使其值增加2。

This is standard behavior for C pointer arithmetic.

这是C指针算法的标准行为。

#3


2  

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,指针++ +将增加指针1,如果指针指向1234字节结构,指针++ +将增加指针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)

这可能会让你第一次遇到它时感到困惑,但实际上它很有意义,这不是一个特殊的处理器特性,但是编译器会在编译过程中计算它,所以当你编写指针+1时编译器会将它编译成指针+ sizeof(*指针)

#4


1  

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型。由于ints宽4字节,所以增加了4字节。一般来说,一个指向T类型的指针会通过sizeof(T)递增

#5


1  

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

正如您所说的,int指针指向int. int通常占用4字节,因此,当您增加指针时,它指向内存中的“下一个”int—即。,增加了4个字节。它以这种方式对任何类型进行操作。如果您有一个指向a的指针,那么递增a *,它会递增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个字节,那么它就会指向一个中间值,我想不出一个合适的机会。

This behavior is very comfortable when iterating over an array, for example.

例如,当迭代数组时,这种行为非常舒适。