When you pass 0 as an argument to malloc , then the free allocated memory is for the pointer that malloc returns.
The result is determined by implementation.
C11: 7.22.3 Memory management functions:
[...] If the size of the requested space is zero, the behavior is determined by the implementation : a null pointer is returned or the behavior looks as if the size was a non-zero value, except that the returned pointer should not be used to access the object.
The standard also says:
The free function releases the space indicated by ptr , that is, available for further allocation. If ptr is a null pointer, no action occurs .
Thus, in any case of the implementation defined during the implementation, the release will not cause undefined behavior.
Now move on to another part of the question.
7.1.4 Using library functions:
If the function argument has an invalid value (for example, a value outside the function area or a pointer outside the program address space, or a null pointer or a pointer to unmodifiable storage when the corresponding parameter is not constant) or a type (after promotion) not expected by a function with a variable number of arguments , undefined behavior .
C11: 7.24.1 p (2):
If the argument declared as size_t n indicates the length of the array for function n, it may have a null value when this function is called. Unless explicitly indicated otherwise in the description of a particular function in this subclause, the arguments of the pointer pointer to such a call shall have valid values, as described in 7.1.4 . With this call, the function that finds the character does not find any occurrence, the function that compares the two sequences of characters returns zero, and the function that copies the characters copies the zero characters.
source share