简体   繁体   English

指针递增查询

[英]Pointer incrementing query

I have been looking at this piece of code, and it is not doing what I expect. 我一直在看这段代码,但没有达到我的期望。

I have 3 globals. 我有3个全局变量。

int x, y, *pointer, z;

Inside of main I declare them. 在main内部,我声明了它们。

x = 10;

y = 25;

pointer = &x;

now at this point 现在在这一点上

&x is 0x004A144  
&y is 0x004A138  

pointer is pointing to 0x004A144 指针指向0x004A144

Now when I increment: 现在,当我增加时:

y = *++pointer;

it points to 0x004A148, this is the address y should be at shouldn't it? 它指向0x004A148,这是地址y,不是吗?

The idea is that incrementing the pointer to 'x' should increment it to point 这个想法是将指针增加到“ x”应该将其增加到指向
at y, but it doesn't seem to want to declare them in in order like I expect. 在y处,但似乎不想按我期望的顺序声明它们。
If this a VS2005 / 2008 problem? 如果这是VS2005 / 2008问题? Or maybe an Express problem? 还是快递问题?
This isn't really homework, as I have done it a couple of years ago but I was revising on my pointer stuff and I tried this again. 这不是真正的作业,因为我几年前就做过,但是我正在修改指针的内容,然后再次尝试了。 But this time I am getting unexpected results. 但是这次我得到了意想不到的结果。 Does anyone have opinions on this? 有人对此有意见吗?

*UPDATE *更新
sorry should be more clear, 'thought' on declaration 'y' should be at 148, and that incrementing the pointer pointing to x should increment 'pointer' to 148 (which it does), but that isn't where y is. 对不起,应该更清楚一点,声明“ y”上的“思想”应该在148,并且递增指向x的指针应该将“ pointer”递增到148(确实如此),但这不是y在哪里。 Why isn't y declaring where it should be. 为什么不声明应该在哪里。

It is really a problem with the whole idea. 整个想法确实存在问题。 You can't meaningfully use a pointer to "jump" from one variable to another by incrementing and decrementing it. 您不能通过递增和递减将指针从一个变量“跳转”到另一个变量来有意义地使用指针。 It is a weird, ugly and meaningless hack, for which the language gives you no guarantees whatsoever. 这是一个奇怪,丑陋且毫无意义的hack,对于该语言,您无法保证。

Pointer arithmetic in C/C++ is only defined within an array . C / C ++中的指针算术仅在数组中定义。 So, if you want something like that, instead of x and y declare an array of 2 ints 因此,如果您想要这样的话,请声明2个整数的数组,而不是xy

int xy[2] = { 10, 25 };

initialize the pointer 初始化指针

int *pointer = &xy[0];

and then you can jump between xy[0] and xy[1] as much as you want by incrementing and decrementing your pointer . 然后您可以通过递增和递减pointer来在xy[0]xy[1]之间随意跳。

No. The compiler is free to order your varibles x, y, pointer and z in any way that it likes. 不会。编译器可以随意按其喜欢的方式对变量x, y, pointer and z进行排序。 ++pointer is not guaranteed to end up pointing to y, and in my experience is unlikely to in practice. 不能保证++ pointer最终指向y,根据我的经验,在实践中不太可能。 laying out variables in an order different from their declaration seems to be quite common. 以不同于声明的顺序排列变量似乎很常见。

dereferencing ++pointer is undefined behavior. 取消引用++pointer是未定义的行为。

If you need x and y to be sequential in memory, then you should declare an array of 2 ints instead of separate variables. 如果需要x和y在内存中是连续的,则应声明一个2个整数的数组,而不是单独的变量。

If your compiler supports some sort of #pragma pack to control structure packing, then I would suggest that you convert your loose global variables to a struct to guarantee ordering. 如果您的编译器支持某种#pragma包来控制结构打包,那么我建议您将松散的全局变量转换为结构以保证排序。

it points to 0x004A148, this is the address y should be at shouldn't it? 它指向0x004A148,这是地址y,不是吗?

No. Address of y is still 0x...138. y地址仍为0x ... 138。 The operation y = *ptr copies the content pointed by ptr to y . 操作y = *ptr 复制由指向的内容ptry No addresses are changed. 地址未更改。

(Also, the address of x is still 144, because the ++ only affects pointer which only happens to have a value of 144 at initialization.) (此外, x的地址仍然是144,因为++仅影响pointer ,而该pointer在初始化时恰好具有144的值。)

Graphically, at initialization, 以图形方式,在初始化时,

 138 (y): 25
 13c      -1
 140      -2
 144 (x): 10  <- pointer
 148      -3

After the ++pointer , ++pointer

 138 (y): 25
 13c      -1
 140      -2
 144 (x): 10
 148      -3  <- pointer

Then you dereference ( * ) it: 然后您取消引用( * ):

 138 (y): 25
 13c      -1
 140      -2
 144 (x): 10
 148      -3  <- pointer (*pointer = -3)

and then copy this value into y ( y = ... ): 然后将此值复制yy = ... ):

 138 (y): -3  <====== copy ========.
 13c      -1                        \
 140      -2                         |
 144 (x): 10                         |
 148      -3  <- pointer (*pointer = -3)

Now &x is still 144, &y is still 138, and pointer is 148, although the value y containing is changed from 25 to some unknown value (-3). 现在&x仍为144, &y仍为138, pointer为148,尽管包含的y值从25更改为某个未知值(-3)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM