Understanding dangling pointer behaviour in this case

一世执手 提交于 2019-12-11 17:37:10

问题


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

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!

Does this value : 0xFF00000000 render any special meaning ?


回答1:


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

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.

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.


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.




回答2:


later I am freeing the pointer somewhere else ? Make sure you free p only when its gets assigned with dynamic memory address, otherwise it leads to undefined behavior.

p = NULL; /* now it's points to NULL */
p = malloc(SIZE); /* malloc() may give same previews memory location or may not */

From C standard, section 6.2.4

The lifetime of an object is the portion of program execution during which storage is guaranteed to be reserved for it. An object exists, has a constant address, and retains its last-stored value throughout its lifetime. If an object is referred to outside of its lifetime, the behavior is undefined. The value of a pointer becomes indeterminate when the object it points to (or just past) reaches the end of its lifetime.

And

I can't expect to retain the same value as that might be used for something else now?

you can't force malloc() to not to return the same old memory address after freed. Freed memory doesn't belong to your program anymore, next time when your process tries to allocate memory again, malloc() may return same address(not value) or may not.



来源:https://stackoverflow.com/questions/52268868/understanding-dangling-pointer-behaviour-in-this-case

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!