Yes. You are returning a pointer to an array with automatic storage duration. When the function returns the pointer is invalid.
Does this result in a dangling pointer?
-
30-06-2022 - |
Question
Does such a function result in a dangling pointer?
int *foo () {
int a[2] = {2, 3};
int *p = NULL;
p = a;
return p;
}
Solution
OTHER TIPS
Never return a pointer to an automatic local variable.
In your case variable a
doesn't exist once foo
returns, so the pointer to it will be invalid (invokes undefined behavior).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow