Dangling Pointer in C

穿精又带淫゛_ 提交于 2019-11-28 02:20:57

Undefined Behavior means anything can happen, including it'll do as you expect. Your stack variables weren't overwritten in this case.

void func3() {
  int a=0, b=1, c=2;
}

If you include a call to func3() in between func1 and printf you'll get a different result.

EDIT: What actually happens on some platforms.

int *func(void)
{  
    int num;  
    num = 100;  
    return #  
}

Let's assume, for simplicity, that the stack pointer is 10 before you call this function, and that the stack grows upwards.

When you call the function, the return address is pushed on stack (at position 10) and the stack pointer is incremented to 14 (yes, very simplified). The variable num is then created on stack at position 14, and the stack pointer is incremented to 18.

When you return, you return a pointer to address 14 - return address is popped from stack and the stack pointer is back to 10.

void func2() {
    int y = 1;
}

Here, the same thing happens. Return address pushed at position, y created at position 14, you assign 1 to y (writes to address 14), you return and stack pointer's back to position 10.

Now, your old int * returned from func points to address 14, and the last modification made to that address was func2's local variable assignment. So, you have a dangling pointer (nothing above position 10 in stack is valid) that points to a left-over value from the call to func2

It's because of the way the memory gets allocated.

After calling func and returning a dangling pointer, the part of the stack where num was stored still has the value 100 (which is what you are seeing afterwards). We can reach that conclusion based on the observed behavior.

After the change, it looks like what happens is that the func1 call overwrites the memory location that a points to with the result of the addition inside func1 (the stack space previously used for func is reused now by func1), so that's why you see 200.

Of course, all of this is undefined behavior so while this might be a good philosophical question, answering it doesn't really buy you anything.

It's undefined behavior. It could work correctly on your computer right now, 20 minutes from now, might crash in an hour, etc. Once another object takes the same place on the stack as num, you will be doomed!

Dangling pointers (pointers to locations that have been disassociated) induce undefined behavior, i.e. anything can happen.

In particular, the memory locations get reused by chance* in func1. The result depends on the stack layout, compiler optimization, architecture, calling conventions and stack security mechanisms.

With dangling pointers, the result of a program is undefined. It depends on how the stack and the registers are used. With different compilers, different compiler versions and different optimization settings, you'll get a different behavior.

Returning a pointer to a local variable yields undefined behaviour, which means that anything the program does (anything at all) is valid. If you are getting the expected result, that's just dumb luck.

Avradeep

Please study functions from basic C. Your concept is flawed...main should be

int main(void)  
{  
    int *a = func();  
    int b;

    b = func1();  
    printf("%d\n%d",*a,func1());  
    return 0;  
}

This will output 100 200

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