0 0 votes #include<stdio.h> #include<stdlib.h> #include<conio.h> int * show(); int main() { int *p=show(); //clrscr(); printf("%d",*p); } int * show() { int x=10; /*int *p; p=(int *)malloc(4); *p=x;*/ return &x; } Here as the $show()$ ends variable $x$ gets destroyed but still I am able to access its value why is it so? Programming in C programming-in-c programming output pointers + – kd..... 2.1k views answer comment Share Follow Print See all 2 Comments 2 2 Comments reply ankitgupta.1729 commented Apr 28, 2018 reply Follow flag which compiler u r using ? ..on gcc , it is showing segmentation fault which is correct 0 0 replyShare anonymous commented Apr 28, 2018 reply Follow flag No bro it's working no error Tried on gcc Linux and code blocks both its give only warning 0 0 replyShare Please log in or register to add a comment.
Best answer 0 0 votes #include<stdio.h> int * show(); int main() { int *p=show(); printf("%d",*p); //Referring x that is de-allocated now but it's value is still exist on memory(in stack) printf("%d",*p); //The memory (stack space) which is earlier allocated to show() is overwritten now by above printf so it will print a garbage value return 0; } int * show() { int x=10; return &x; } //e Although scope and lifetime of variable x end as point e but its value is still there on the stack that's why 1st printf gives correct output. But after that, the memory is overwritten so 2nd printf will return a garbage value. NOTE - GCC compiler returns 10 -2(this is a garbage value) with a Warning: function returns address of local variable. Soumya29 answered Apr 28, 2018 • edited Apr 28, 2018 by Soumya29 Soumya29 comment Share Follow See 1 comment 1 1 comment reply ankitgupta.1729 commented Apr 28, 2018 reply Follow flag @Soumya ,local variable x is stored in stack frame show(). So when it is deleted then why in main() , 1st printf() will give correct output. I am getting segmentation fault because it is accessing memory which does not belongs to this process. 0 0 replyShare Please log in or register to add a comment.
0 0 votes The code doesn't work and gives error in GCC, but works with a warning in ZAPCC. So the compiler you used to run this code is optimised to run through such errors by giving warning(s). Try your code here on different compilers, like here: https://www.jdoodle.com/c-online-compiler Akhilesh Singla answered Apr 27, 2018 Akhilesh Singla comment Share Follow See 1 comment 1 1 comment reply anonymous commented Apr 28, 2018 reply Follow flag the variables are destroyed as the function ends but the value are constants and so it remains at that address only and we are accessing it am I true? 0 0 replyShare Please log in or register to add a comment.
0 0 votes @ankit I tired on code blocks and it is working fine there. Just giving a warning. Although this is the case of undefined behaviour. It's not always true that it will return the correct value. Check this modified code. #include<stdio.h> int * show(); int main() { int *p=show(); printf("hello"); printf("%d",*p); \\memory is overwritten already. So it will print garbage value return 0; } int * show() { int x=10; return &x; } //e Soumya29 answered Apr 28, 2018 Soumya29 comment Share Follow See all 6 Comments 6 6 Comments reply Show 3 previous comments ankitgupta.1729 commented Apr 29, 2018 reply Follow flag yes, but I don't know why it is showing segmentation fault on my machine. I have tried online gdb compiler, there it is also showing segmentation fault. I don't know what is the problem. 1 1 replyShare Soumya29 commented Apr 29, 2018 reply Follow flag Because on your machine after the show() is destroyed, that memory is reused and allocated to printf() function that's why it is giving segmentation fault. But on my machine, although that memory is no longer belong to show() function, it is not cleaned up by the time printf() is executed so I am able to get its correct value. So it's completely machine dependent. 1 1 replyShare ankitgupta.1729 commented Apr 29, 2018 reply Follow flag ok got it..thank you ! 0 0 replyShare Please log in or register to add a comment.