Static vs Dynamic

https://ide.codingblocks.com/#/s/17057
Please look into code. I have commented out the problem.

shrey a is pointing toward a memory location in heap so when u return address b also points the same in heap(which is out of scope rule) so thats why they are showing same

But life of ‘a’ is finished just after function call ends. So as in the case of static allocation the value returned must be 0 and b should get value zero.

but it is returning the address before getting destroyed in the main
just take an example when u make a function of factorial and return it value to main then the variable in which factorial is stored is got destroyed but u got the value in main ,same case here u having a pointer pointer to a memory in heap and when u return the address return in main,sou got the same address
Hope this help

1 Like