Yes, I know perfectly well you should not do that. If we have this code:
int *foo() {
int a = 42;
return &a;
}
As most C coders know, this is undefined behavior: Using pointer after free()
int *p = foo();
printf("%d\n", *p);
Just so that future readers don't take this as the truth. Everything below until the question was based on a false assumption from me. It's not UB. It's just bad.
As some C coders know, but less than for above, that this also is UB, even though no dereferencing is done: (Was trying to find a good question about this, but did not find anyone)
int *p = foo();
printf("%p\n", p); // Should be printf("%p\n", (void*) p);
// Eric P clarified this in his answer, but this missing cast
// is not a part of the primary question
And for the same reason, also this is UB, because what happens, is that the pointer become indeterminate, just like an uninitialized variable. A so called dangling pointer.
int *p = foo();
int *q = p;
And somewhat surprising to some, even this is not ok:
free(ptr);
if(ptr == NULL) // Just as bad as if ptr is not initialized
The question
What I do wonder, is if also this single line invokes UB:
int *p = foo();
Or maybe even this?
foo();
In other words, does p
become a dangling pointer, or does it get assigned to a dangling pointer?
I don't know if there's any practical use for this, except deeper understanding for the C language. Well one good use case would be to figure out which refactorings that are urgent and which can wait.