Per C 2018 7.22.3.4, malloc
returns a void *
. Per 7.22.3.3, the parameter to free
is a void *
, and an argument to it is converted to void *
, per 6.5.2.2 7. So the parameter to free
is never a different type than the original pointer to the allocated space; it starts and ends as void *
.
There would only be a problem if the value were different. The rules for pointer conversions are in 6.3.2.3. Effectively, they allow conversions through pointers to different object types as long as the pointer is correctly aligned for the referenced type. Per 7.22.3 1, the value returned by malloc
is suitable for any fundamental alignment, which includes all the basic, enumerated, and pointer types and all pointers to arrays, structures, or unions whose elements or members have fundamental alignment requirements and all complete object types defined for the standard library. So, as long as you do not convert a pointer from malloc
to some implementation-defined extended type with a greater-than-fundamental alignment requirement or to a type that uses _Alignas
to specify a greater alignment, the conversions will effectively preserve the original value of the pointer returned from malloc
.