简体   繁体   English

取消引用指针以键入下一个值时出错

[英]error while dereferencing pointer to type to the next value

This code should have brought me a printed value of 7. As i am have declared integer i=5 which allocates 4 bytes in memory and integer myInt which also allocates 4 bytes next to the int i . 该代码应该给我带来了7的打印值。因为我声明了整数i = 5 ,它在内存中分配了4个字节,而整数myInt也在int i旁边分配了4个字节 Then i declared a pointer to store the location/address of the int i . 然后,我声明了一个指针来存储int i的位置/地址。 But when I add 1 integer to the pointer or i's location which means 4bytes to pointer location of i and then dereference it. 但是,当我加1点的整数指针或我的位置,这意味着4字节到的指针位置,然后取消对它的引用。 I don't get the 7 which i stored in myInt rather i get some value as 243423(something like this) 我没有获得存储在myInt中7,而是获得了一些值,如243423(类似这样)

#include <stdio.h>
#include <stdlib.h>

void main()
{
int i=5;
int myInt=7;
int *pointer = &i;

printf("%i\n", *(pointer+1));
getch();
}

pointer is a different address location pointing to i , now when you add +1 a sizeof(int) it addresses next location of pointer but not second stack variable myInt . pointer是指向i的不同地址位置,现在当您将+1 sizeof(int)+1时,它将寻址pointer下一个位置,而不是第二个堆栈变量myInt

 pointer
+-------+               +------+
| 0x100 | -----> 0x100  |  5   |
+-------+               +------+
  0x300          0x104  |  7   |
                        +------+

A simple memory can be as above, both local variables i & myInt can store in stack in contiguous memory locations. 一个简单的存储器可以如上,局部变量imyInt都可以在堆栈中的连续存储器位置中存储。 But you made pointer to point to i and pointer itself stored in address location 0x300 . 但是您使pointer指向i并且pointer本身存储在地址位置0x300 Now when you dereference next memory location of pointer which doesn't point to myInt rather some other memory location which is Undefined Behavior. 现在,当取消引用pointer下一个内存位置时,该位置不指向myInt而是指向其他未定义行为的内存位置。

Hmm, memory allocation isnt always continuum. 嗯,内存分配并不总是连续的。 *(pointer+1) points to uninitialized memory. *(pointer + 1)指向未初始化的内存。

I can see where you're going, but you're getting tripped up by a misunderstanding. 我可以看到您要去的地方,但是您被误会绊倒了。

If you allocate an array, the elements of the array will be placed next to each other, and things will work the way you're expecting: 如果分配一个数组,则该数组的元素彼此相邻放置,事情将按照您期望的方式工作:

int foo[] = { 4, 3, 2, 1 };
int* i = foo;
printf("%d\n", *i);
printf("%d\n", *(i + 1));

Likewise, if you malloc a block of memory, and then put objects next to each other in that memory, you can use pointer arithmetic the way you're expecting. 同样,如果您malloc了一块内存,然后在该内存中将对象彼此相邻放置,则可以按期望的方式使用指针算术。

However, if you allocate two objects at about the same time, the language does not promise to put them next to each other in memory. 但是,如果您大约同时分配两个对象,则该语言不会保证在内存中将它们彼此相邻放置。 There is no reason for pointer arithmetic to work unless you tell the compiler "I want to allocate these objects next to each other." 除非您告诉编译器“我要彼此相邻分配这些对象”,否则没有理由使指针算法起作用。 You do that by creating an array, or allocating a block of memory. 您可以通过创建一个数组或分配一个内存块来实现。


A compiler is allowed to lay out memory as described in the video, but it's not required to. 如视频中所述,允许编译器布置内存,但并非必须如此。 Using the variables from the video and the original question, the compiler is allowed to notice that you never do anything with myInt and optimize it away. 使用视频中的变量和原始问题,允许编译器注意到您永远不会对myInt进行任何myInt并对其进行优化。 It's also allowed to notice that myInt never changes and output a constant which can be allocated in a different memory area. 还可以注意到myInt永不更改,并输出可以在不同内存区域中分配的常量。 It might decide to allocate all local variables contiguously on the stack, but in a different order than what appears in the source code (perhaps doing so allows for better use of RAM). 它可能决定在堆栈上连续分配所有局部变量,但是顺序与源代码中出现的顺序不同(也许这样做可以更好地使用RAM)。

Even in cases where it puts i and myInt next to each other, on some platforms i will have the lower address and on other platforms myInt will. 即使在imyInt彼此myInt下,在某些平台上, i的地址也会较低,而在其他平台上, myInt地址会较低。 The compiler has enough flexibility that if you want the variables allocated next to each other, you need to make that clear. 编译器具有足够的灵活性,如果您希望变量彼此相邻分配,则需要明确说明。

Declare "i" and and "myInt" as static and you will get your expected behaviour. 将“ i”和“ myInt”声明为静态,您将获得预期的行为。

Note 1: This kind of design is only useful for didactic purposes; 注1:这种设计仅用于教学目的。
Note 2: The correct way to store these values in order to access using pointers is to use an vector. 注2:为了使用指针进行访问,存储这些值的正确方法是使用向量。

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

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