Please bear with me, I am a learner…<\/p>\n
I wrote a program in C having dangling pointer:<\/p>\n
#include<stdio.h>\n\nint *func(void)\n{\n int num;\n num = 100;\n return #\n}\n\nint func1(void)\n{\n int x,y,z;\n scanf(\"%d %d\",&y,&z);\n x=y+z;\n return x;\n}\n\nint main(void)\n{\n int *a = func();\n int b;\n b = func1();\n printf(\"%d\\n\",*a);\n return 0;\n}\n<\/code><\/pre>\n
Advertisement
I am getting the output as 100<\/strong> even though the pointer is dangling.<\/p>\nI made a one-time change in the above function func1(). Instead of taking the value of y and z from standard input as in the above program, now I am assigning the value during compile time.<\/p>\n
I redefined the func1() as follows:<\/p>\n
int func1(void)\n{\n int x,y,z;\n y=100;\n z=100;\n x=y+z;\n return x;\n}\n<\/code><\/pre>\nNow the output is 200. I am confused!<\/p>\n
Can somebody please explain to me the reason for the above two outputs?<\/p>","upvoteCount":4,"answerCount":2,"datePublished":"2022-03-03T18:47:03.000Z","author":{"@type":"Person","name":"spiceuser-kas7c","url":"https://community.spiceworks.com/u/spiceuser-kas7c"},"suggestedAnswer":[{"@type":"Answer","text":"
Please bear with me, I am a learner…<\/p>\n
I wrote a program in C having dangling pointer:<\/p>\n
#include<stdio.h>\n\nint *func(void)\n{\n int num;\n num = 100;\n return #\n}\n\nint func1(void)\n{\n int x,y,z;\n scanf(\"%d %d\",&y,&z);\n x=y+z;\n return x;\n}\n\nint main(void)\n{\n int *a = func();\n int b;\n b = func1();\n printf(\"%d\\n\",*a);\n return 0;\n}\n<\/code><\/pre>\nI am getting the output as 100<\/strong> even though the pointer is dangling.<\/p>\nI made a one-time change in the above function func1(). Instead of taking the value of y and z from standard input as in the above program, now I am assigning the value during compile time.<\/p>\n
I redefined the func1() as follows:<\/p>\n
int func1(void)\n{\n int x,y,z;\n y=100;\n z=100;\n x=y+z;\n return x;\n}\n<\/code><\/pre>\nNow the output is 200. I am confused!<\/p>\n
Can somebody please explain to me the reason for the above two outputs?<\/p>","upvoteCount":4,"datePublished":"2022-03-03T18:47:03.000Z","url":"https://community.spiceworks.com/t/dangling-pointer-in-c/826708/1","author":{"@type":"Person","name":"spiceuser-kas7c","url":"https://community.spiceworks.com/u/spiceuser-kas7c"}},{"@type":"Answer","text":"
It’s because of the way the memory gets allocated.<\/p>\n
After calling func<\/strong> and returning a dangling pointer, the part of the stack where num was stored still has the value 100<\/strong> (which is what you are seeing afterward). We can reach that conclusion based on the observed behavior.<\/p>\nAfter the change, it looks like what happens is that the func1<\/strong> call overwrites the memory location that “a” points to, with the result of the addition inside func1<\/strong> (the stack space previously used for func<\/strong> is reused now by func1<\/strong>), so that’s why you see 200<\/strong>.<\/p>\nOf course, all of this is undefined behavior so while this might be a good philosophical question, answering it doesn’t really buy you anything. Read this<\/a> resource for clearing all the concepts about the topic.<\/p>","upvoteCount":2,"datePublished":"2022-03-04T05:34:55.000Z","url":"https://community.spiceworks.com/t/dangling-pointer-in-c/826708/2","author":{"@type":"Person","name":"baeconandeggz","url":"https://community.spiceworks.com/u/baeconandeggz"}}]}}