If you have a pointer that is not initialized and, by mistake, try to free it, is this going to result in undefined behavior?
Like:
int main(void){
Yes, it is undefined behavior.
The pointer passed to free
should be a pointer to a valid object allocated with malloc
, calloc
, realloc
or a null pointer.
From C99:
(7.20.3.2p2) "If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined."