Here are some reasons for having the caller supply a pointer:
- Symmetric ownership semantics. This is already explained by several other answers.
- Avoids mismatching the allocator and deallocator. As mentioned in Aesthete’s answer, if the DLL allocates a pointer and returns it, the caller must call the corresponding deallocator to free it. This is not necessarily trivial: the DLL might be statically linked against one version of, say,
malloc
/free
while the.exe
is linked against a different version ofmalloc
/free
. (For example, the DLL could be using release versions while the.exe
is using specialized debug versions.) - Flexibility. If the DLL is meant for general use, having the caller allocate the memory gives the caller more options. Suppose the caller doesn’t want to use
malloc
and instead wants memory to be allocated from some specific memory pool. Maybe it’s a case where the caller could provide a pointer to memory allocated on the stack. If the DLL allocated the memory itself, the caller does not have any of these options.
(The second and third points also mostly can be addressed by having the .exe
supply an allocator/deallocator for the DLL code to use.)