Bug#453264: libc6-dev: fails to define ptrdiff_t in malloc.h
It's not that easy in the swi-prolog case. The two headers are included
in two separate headers, which are then included into the .c file. The
order cannot be switched around, because the former header includes
config.h, which is needed for the second header to work at all (IIRC).
Can't you just include <malloc.h> directly from the C file before including the header that indirectly includes <wchar.h> ?
Generally standard headers are designed so that once you have included them once including them again does nothing.