The midl_user_allocate Function

The midl_user_allocate function is a procedure that must be supplied by developers of RPC applications. It allocates memory for the RPC stubs and library routines. Your midl_user_allocate function must match the following prototype:

void __RPC_FAR * __RPC_USER midl_user_allocate (size_t cBytes);

The cBytes parameter specifies the number of bytes to allocate. Both client applications and server applications must implement the midl_user_allocate function unless you are compiling in OSF-compatibility (/osf) mode. Applications and generated stubs call midl_user_allocate directly or indirectly to manage allocated objects. For example:

  • The client and server applications call midl_user_allocate to allocate memory for the application, such as when creating a new node in a tree or linked list.
  • The server stub calls midl_user_allocate when unmarshaling data into the server address space.
  • The client stub calls midl_user_allocate when unmarshaling data from the server that is referenced by an [out] pointer. Note that for [in], [out], and [unique] pointers, the client stub calls midl_user_allocate only if the [unique] pointer value was null on input and changes to a non-null value during the call. If the [unique] pointer was non-null on input, the client stub writes the associated data into existing memory.

If midl_user_allocate fails to allocate memory, it should return a null pointer.

The midl_user_allocate function should return an 8-byte aligned pointer.

For example, the sample programs provided with the Platform Software Development Kit (SDK) implement midl_user_allocate in terms of the C function malloc:

void __RPC_FAR * __RPC_USER midl_user_allocate(size_t cBytes)
    return((void __RPC_FAR *) malloc(cBytes));


If the RpcSs package is enabled (for example, as the result of using the [ enable_allocate] attribute), use RpcSmAllocate to allocate memory on the server side. For additional information on [enable_allocate], see MIDL Reference.