C中的悬空指针

Here you will learn about dangling reference and dangling pointer in C.

在这里,您将了解C中的悬空引用和悬空指针

In computer programming, memory is allocated for holding data object. After the work is done the memory is deallocated so that this memory can be used to store any other data object.

在计算机编程中,分配了内存以保存数据对象。 工作完成后,将内存重新分配,以便可以使用此内存存储任何其他数据对象。

Also Read: void pointer in C

另请阅读: C中的void指针

What is Dangling Pointer?

什么是悬空指针?

While programming, we use pointers that contain memory addresses of data objects. Dangling pointer is a pointer that points to the memory location even after its deallocation. Or we can say that it is pointer that does not point to a valid data object of the appropriate type. The memory location pointed by dangling pointer is known as dangling reference.

在编程时,我们使用包含数据对象的内存地址的指针。 悬空指针是即使在释放后仍指向内存位置的指针。 或者我们可以说是指针,它没有指向适当类型的有效数据对象。 悬空指针指向的存储位置称为悬空引用。

Now if we access the data stored at that memory location using the dangling pointer then it will result in program crash or an unpredictable behaviour.

现在,如果我们使用悬空指针访问存储在该内存位置的数据,则将导致程序崩溃或不可预测的行为。

Dangling Pointer in C

Let’s take an example to understand this.

让我们以一个例子来理解这一点。

C语言中指针悬空的原因 (Cause of Dangling Pointer in C)

void function(){
	int *ptr = (int *)malloc(SIZE);
	. . . . . .
	. . . . . . 
	free(ptr);	//ptr now becomes dangling pointer which is pointing to dangling reference
}

In above example we first allocated a memory and stored its address in ptr. After executing few statements we deallocated the memory. Now still ptr is pointing to same memory address so it becomes dangling pointer.

在上面的示例中,我们首先分配了一个内存,并将其地址存储在ptr中。 在执行了几条语句后,我们重新分配了内存。 现在,ptr仍指向相同的内存地址,因此它变成了悬空指针。

I have mentioned only one scenario of dangling pointer. There are many other situations that causes dangling pointer problem.

我只提到了悬空指针的一种情况。 还有许多其他情况会导致悬空指针问题。

To read more you can visit: https://en.wikipedia.org/wiki/Dangling_pointer

要了解更多信息,您可以访问: https : //en.wikipedia.org/wiki/Dangling_pointer

How to Solve Dangling Pointer Problem in C?

如何解决C语言中的悬空指针问题?

To solve this problem just assign NULL to the pointer after the deallocation of memory that it was pointing. It means now pointer is not pointing to any memory address.

要解决此问题,只需在指针所指向的内存解除分配之后将NULL分配给指针。 这意味着现在指针没有指向任何内存地址。

Let’s understand this by a code in C.

让我们通过C中的代码来理解这一点。

void function(){
	int *ptr = (int *)malloc(SIZE);
	. . . . . .
	. . . . . . 
	free(ptr);	//ptr now becomes dangling pointer which is pointing to dangling reference
	ptr=NULL;	//now ptr is not dangling pointer
}

If you have any doubts related to above dangling pointer in C tutorial then feel free to ask by commenting below.

如果您对以上C教程中的悬空指针有疑问,请随时在下面评论。

翻译自: https://www.thecrazyprogrammer.com/2016/03/dangling-pointer-in-c.html

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值