了解这种情况下的悬空指针行为 [英] Understanding dangling pointer behaviour in this case

查看:60
本文介绍了了解这种情况下的悬空指针行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指针,默认情况下它带有 NULL 然后它等待某个事件并在事件发生时获取一个值,稍后我正在释放其他地方的指针,但即使在释放指针后我没有将其设为 NULL 所以它仍然继续引用相同的内存位置,我知道下一个 malloc 调用可能将该内存块分配给其他内存请求!

I have a pointer and by default it carries NULL then it waits for some event and gets a value if the event happens, later I am freeing the pointer somewhere else but even after freeing the pointer I am not making it NULL so it still keeps referencing the same memory location and I know the next malloc call might allocate that memory chunk to some other memory request!

pointer_type *p = NULL;
while (process_get_wakeup(//some logic//)) { 
        while ((qelem = (void*)process_dequeue(//some logic//)) != NULL) {
           p = (pointer_type *)qelem;
        }
        .
        .
        //goes into a loop of calls where free(p) is also done!
        .
        .
        //Printing value of p as %p gives this : 0xFF00000000

我已经不知道我们应该怎么做,我不能指望保留与现在可能用于其他东西的价值相同的价值,但我想要什么要知道为什么我只能看到 p 的特定值!

EDIT : I already know it not how we are supposed to do it, and I can't expect to retain the same value as that might be used for something else now, but what I want to know is why only a particular value of p is seen by me!

这个值:0xFF00000000有什么特殊含义吗?

Does this value : 0xFF00000000 render any special meaning ?

推荐答案

相反 - 指针在 free保留其值.

On the contrary - the pointer does not retain its value after free.

C 标准规定,一旦对象freed,或者更一般地说,它的生命周期结束,指向该对象的所有指针的值都变得不确定,并且使用这种不确定的值可能会导致未定义的行为,即使它只是打印值.它碰巧看起来好像保留了它的原始值,这并不能保证.

The C standard says that as soon as the object is freed or, more generally, its lifetime ends, the values of all pointers pointing to the object become indeterminate, and using such an indeterminate value can lead to undefined behaviour even if it was just printing the value. That it happens to look as if it retains its original value is in no way guaranteed.

这允许 C 编译器在您的函数内进行优化.例如,如果它使用一个 CPU 寄存器来保留 p 的值,在 free(p) 调用之后,编译器知道寄存器现在可以用于其他用途,例如存储其他操作的中间计算结果,并且不需要存储其值,直到为其分配新值.

This allows the C compiler to do optimizations within your function. For example if it used one CPU register to retain the value of p, after the free(p) call, the compiler knows that the register can be now used for something else, for example to store results of intermediate calculations of other operations, and its value does not need to be stored, until a new value is assigned to it.

至于两个不同对象的内存地址相同——这是可能的,如果它们不是同时存在的话.单个对象将在其整个生命周期中拥有一个常量地址.在它的生命周期之后会发生什么是不确定的.malloc 通常被实现为一个空闲块列表,最近 freed 块可能会首先被重用.

As for the memory address of two distinct objects being the same - that is possible, if they are not alive at the same time. A single object will have a constant address for its entire lifetime. What happens after its lifetime is unspecified. malloc is often implemented as a list of free blocks, and the most-recently freed block is likely to be reused first.

这篇关于了解这种情况下的悬空指针行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆