等于两个指针而不使用&或*?

时间:2022-02-06 07:19:11

I'm trying to understand what this code does (and if it's even allowed):

我试着理解这段代码的作用(如果允许的话):

int * A;
int * B;
A = (int *)malloc( size_t somenumber );
B = A;

// bunch of stuff using B, B++, etc.

Everything I've read always shows equating things to pointers either using the reference operator (&) or the derefernce operator (*).

我读过的所有内容都显示了使用引用操作符(&)或derefernce操作符(*)将对象等同于指针。

What does this sort of equating do?

这类等式有什么作用?

And, when I ultimately free(A) what happens to B?

当我最终*(A) B发生了什么?

6 个解决方案

#1


6  

Pictures are always good when it comes to pointer confusion:

当涉及到指针混淆时,图片总是好的:

int * A; // create a pointer to an int named "A"
int * B; // create a pointer to an int named "B"
A = (int *)malloc( size_t somenumber ); // Allocate A some memory, now B is an 
                                        // uninitialized pointer; A is initialized,
                                        // but just to uninitialized memory

Conceptually:

从概念上讲:

等于两个指针而不使用&或*?

B = A; // Assign B to the value of A (The uninitialized memory)

等于两个指针而不使用&或*?

free(A);

等于两个指针而不使用&或*?

So after all that I think you can see what's happening. B is being assigned the value of A, which is the allocated and uninitialized memory chunk. So now you just have two pointers pointing to the same area.

所以在这些之后,我认为你们可以看到发生了什么。B被分配A的值,A是分配和未初始化的内存块。现在你有两个指向同一个区域的指针。

As to the free() question, as you can see when you call free(A); you're left with both A and B pointing to the same area, there's just nothing allocated to your program there anymore. This is why when calling free() it's good to set your pointer to NULL.

至于free()问题,您可以在调用free(A)时看到;剩下的A和B都指向同一个区域,这里已经没有分配给你的程序了。这就是为什么在调用free()时最好将指针设置为NULL。

Now way back to your initial question. If you wanted to check of two pointers were ==:

现在回到你最初的问题。如果您想检查两个指针,则==:

int * A; // create a pointer to an int named "A"
int * B; // create a pointer to an int named "B"
A = (int *)malloc( size_t somenumber ); // Allocate A some memory, now B is an 
                                        // uninitialized pointer; A is initialized,
                                        // but just to uninitialized memory

if(B == A){
   // The pointers are pointing to the same thing!
}
if(*B == *A){
   // The values these pointers are pointing to is the same!
}

UPDATE
So to answer your updated questions, We need to change the definition of B.

更新以便回答您更新的问题,我们需要更改B的定义。

int *A;  // A is a pointer to an int
int **B; // B is a pointer to a pointer to an int
B = &A;  // B is now pointing to A

So to illustrate that:

为了说明:

等于两个指针而不使用&或*?

For B=*A:

B = *:

int *A;
int B;
A = malloc(sizeof(int));
*A = 5;
B = *A;

This is a deference of A. So you're just taking whatever A is pointing to and assigning it to B, in this case 5

这是对a的尊重,所以你只要把a指向的东西赋给B,在这个例子中是5

#2


7  

This is not equating two pointers, it is a pointer assignment. Once

这并不等于两个指针,它是一个指针赋值。一次

B = A;

is executed, both pointers point to the same area of memory: accessing *B becomes exactly the same as accessing *A, B[i] becomes equivalent to A[i], and so on.

两个指针都指向相同的内存区域:访问*B与访问*A完全相同,B[i]等于A[i],依此类推。

Note that freeing A leaves B dangling, and vice versa. In other words, after calling

注意释放A会使B悬空,反之亦然。换句话说,打电话后

free(A);

accessing *B becomes undefined behavior.

访问*B变成了未定义的行为。

#3


2  

This is not "equating", whatever that means, this is simply assignment. The pointer A's value (the address of the memory block you just allocated) is copied into the pointer B.

这不是“等价”,不管那是什么意思,这只是一个赋值。指针A的值(您刚才分配的内存块的地址)被复制到指针B中。

When you free(A), B becomes what is known as a "dangling pointer", i.e. a pointer that is no longer valid to use.

当您释放(A)时,B变成了所谓的“悬浮指针”,即不再有效使用的指针。

#4


2  

You're just assigning one pointer to another. Now B will point to whatever A was pointing to. If you free A it will also free B (not sure how to phrase this, since only one free happens). As for the & you probably mean:

你只是把一个指针分配给另一个。现在B指向A指向的任何东西。如果你释放A,它也会释放B(不知道怎么表达,因为只有一个*发生)。至于&你可能是说:

int *A;
int B;
A = &B;

which means make the pointer A point at the integer B, or store B's address in A.

这意味着要使指针指向整数B,或者将B的地址存储在A中。

#5


2  

This simply sets the two pointers to point to the same memory address.

这只需将两个指针设置为指向相同的内存地址。

If one of them is later changed to point to another address, this does not affect the other -- although dereferencing and changing the value pointed to would of course be visible from the other pointer as well.

如果其中一个后来被更改为指向另一个地址,则不会影响另一个地址——尽管取消引用和更改指向的值当然也可以从另一个指针中看到。

If one of the pointers is freed the other copy would immediately be illegal to dereference (assuming it has kept the same value after the assignment).

如果释放了其中一个指针,则立即取消引用的另一个副本将是非法的(假设它在赋值后保持相同的值)。

A practical reason for writing such code might be that A is supposed to remain "fixed" at the start of the allocated memory while B will roam inside the allocated range during program execution. You need A to free the memory (and perhaps as a baseline), but it's more convenient to work with another "temporary" pointer into that memory for some reason.

编写此类代码的一个实际原因可能是,在分配的内存开始时,A应该保持“固定”,而B则在程序执行期间在分配的范围内漫游。您需要释放内存(可能作为基线),但是出于某种原因,使用另一个“临时”指针进入内存会更方便。

#6


2  

In the case above, when you free(A), the memory block pointed to by pointer A (pointer B also points to the same block) will be freed and resource returned to the system. The address stored in A or B will still be there (we call this dangling pointer - a pointer pointing to previous location of some memory block). Usually, we set it to NULL after calling free(), at least for modern OS will cause the system to signal SIGSEGV and core dump when we try to incorrectly reference the pointer later. Otherwise, the program might display strange behavior (sometimes crash, sometimes continue, buggy output) when the same address pointed to by the pointer is somehow again allocated to you.

在上面的例子中,当释放(A)时,指针A指向的内存块(指针B也指向相同的块)将被释放,资源将返回给系统。存储在A或B中的地址仍然存在(我们称之为悬浮指针——指向某个内存块先前位置的指针)。通常,我们在调用free()之后将它设置为NULL,至少对于现代操作系统来说,当我们稍后尝试不正确引用指针时,会导致系统向SIGSEGV和core dump发出信号。否则,程序可能会显示奇怪的行为(有时会崩溃,有时会继续,bug输出),当指针指向的地址再次被分配给你。

#1


6  

Pictures are always good when it comes to pointer confusion:

当涉及到指针混淆时,图片总是好的:

int * A; // create a pointer to an int named "A"
int * B; // create a pointer to an int named "B"
A = (int *)malloc( size_t somenumber ); // Allocate A some memory, now B is an 
                                        // uninitialized pointer; A is initialized,
                                        // but just to uninitialized memory

Conceptually:

从概念上讲:

等于两个指针而不使用&或*?

B = A; // Assign B to the value of A (The uninitialized memory)

等于两个指针而不使用&或*?

free(A);

等于两个指针而不使用&或*?

So after all that I think you can see what's happening. B is being assigned the value of A, which is the allocated and uninitialized memory chunk. So now you just have two pointers pointing to the same area.

所以在这些之后,我认为你们可以看到发生了什么。B被分配A的值,A是分配和未初始化的内存块。现在你有两个指向同一个区域的指针。

As to the free() question, as you can see when you call free(A); you're left with both A and B pointing to the same area, there's just nothing allocated to your program there anymore. This is why when calling free() it's good to set your pointer to NULL.

至于free()问题,您可以在调用free(A)时看到;剩下的A和B都指向同一个区域,这里已经没有分配给你的程序了。这就是为什么在调用free()时最好将指针设置为NULL。

Now way back to your initial question. If you wanted to check of two pointers were ==:

现在回到你最初的问题。如果您想检查两个指针,则==:

int * A; // create a pointer to an int named "A"
int * B; // create a pointer to an int named "B"
A = (int *)malloc( size_t somenumber ); // Allocate A some memory, now B is an 
                                        // uninitialized pointer; A is initialized,
                                        // but just to uninitialized memory

if(B == A){
   // The pointers are pointing to the same thing!
}
if(*B == *A){
   // The values these pointers are pointing to is the same!
}

UPDATE
So to answer your updated questions, We need to change the definition of B.

更新以便回答您更新的问题,我们需要更改B的定义。

int *A;  // A is a pointer to an int
int **B; // B is a pointer to a pointer to an int
B = &A;  // B is now pointing to A

So to illustrate that:

为了说明:

等于两个指针而不使用&或*?

For B=*A:

B = *:

int *A;
int B;
A = malloc(sizeof(int));
*A = 5;
B = *A;

This is a deference of A. So you're just taking whatever A is pointing to and assigning it to B, in this case 5

这是对a的尊重,所以你只要把a指向的东西赋给B,在这个例子中是5

#2


7  

This is not equating two pointers, it is a pointer assignment. Once

这并不等于两个指针,它是一个指针赋值。一次

B = A;

is executed, both pointers point to the same area of memory: accessing *B becomes exactly the same as accessing *A, B[i] becomes equivalent to A[i], and so on.

两个指针都指向相同的内存区域:访问*B与访问*A完全相同,B[i]等于A[i],依此类推。

Note that freeing A leaves B dangling, and vice versa. In other words, after calling

注意释放A会使B悬空,反之亦然。换句话说,打电话后

free(A);

accessing *B becomes undefined behavior.

访问*B变成了未定义的行为。

#3


2  

This is not "equating", whatever that means, this is simply assignment. The pointer A's value (the address of the memory block you just allocated) is copied into the pointer B.

这不是“等价”,不管那是什么意思,这只是一个赋值。指针A的值(您刚才分配的内存块的地址)被复制到指针B中。

When you free(A), B becomes what is known as a "dangling pointer", i.e. a pointer that is no longer valid to use.

当您释放(A)时,B变成了所谓的“悬浮指针”,即不再有效使用的指针。

#4


2  

You're just assigning one pointer to another. Now B will point to whatever A was pointing to. If you free A it will also free B (not sure how to phrase this, since only one free happens). As for the & you probably mean:

你只是把一个指针分配给另一个。现在B指向A指向的任何东西。如果你释放A,它也会释放B(不知道怎么表达,因为只有一个*发生)。至于&你可能是说:

int *A;
int B;
A = &B;

which means make the pointer A point at the integer B, or store B's address in A.

这意味着要使指针指向整数B,或者将B的地址存储在A中。

#5


2  

This simply sets the two pointers to point to the same memory address.

这只需将两个指针设置为指向相同的内存地址。

If one of them is later changed to point to another address, this does not affect the other -- although dereferencing and changing the value pointed to would of course be visible from the other pointer as well.

如果其中一个后来被更改为指向另一个地址,则不会影响另一个地址——尽管取消引用和更改指向的值当然也可以从另一个指针中看到。

If one of the pointers is freed the other copy would immediately be illegal to dereference (assuming it has kept the same value after the assignment).

如果释放了其中一个指针,则立即取消引用的另一个副本将是非法的(假设它在赋值后保持相同的值)。

A practical reason for writing such code might be that A is supposed to remain "fixed" at the start of the allocated memory while B will roam inside the allocated range during program execution. You need A to free the memory (and perhaps as a baseline), but it's more convenient to work with another "temporary" pointer into that memory for some reason.

编写此类代码的一个实际原因可能是,在分配的内存开始时,A应该保持“固定”,而B则在程序执行期间在分配的范围内漫游。您需要释放内存(可能作为基线),但是出于某种原因,使用另一个“临时”指针进入内存会更方便。

#6


2  

In the case above, when you free(A), the memory block pointed to by pointer A (pointer B also points to the same block) will be freed and resource returned to the system. The address stored in A or B will still be there (we call this dangling pointer - a pointer pointing to previous location of some memory block). Usually, we set it to NULL after calling free(), at least for modern OS will cause the system to signal SIGSEGV and core dump when we try to incorrectly reference the pointer later. Otherwise, the program might display strange behavior (sometimes crash, sometimes continue, buggy output) when the same address pointed to by the pointer is somehow again allocated to you.

在上面的例子中,当释放(A)时,指针A指向的内存块(指针B也指向相同的块)将被释放,资源将返回给系统。存储在A或B中的地址仍然存在(我们称之为悬浮指针——指向某个内存块先前位置的指针)。通常,我们在调用free()之后将它设置为NULL,至少对于现代操作系统来说,当我们稍后尝试不正确引用指针时,会导致系统向SIGSEGV和core dump发出信号。否则,程序可能会显示奇怪的行为(有时会崩溃,有时会继续,bug输出),当指针指向的地址再次被分配给你。