How can I determine dangling pointers in C? Here is example of this.
int *fun(void) {
int i = 2;
return &i;
}
How can I determine dangling pointers in C? Here is example of this.
int *fun(void) {
int i = 2;
return &i;
}
Here in below code block
int *fun(void) {
int i = 2;
return &i;
}
you are returning a address of a local variable that means you are returning a dangling pointer & it causes undefined behaviour, your compiler could have warned you like
error: function returns address of local variable [-Werror=return-local-addr]
return &i;
above if you could have compile with flags like gcc -Wall -Wstrict-prototypes -Werror test.c