This is documentation for Mathematica 6, which was
based on an earlier version of the Wolfram Language.
View current documentation (Version 11.2)
MathLink C Function


void MLSetAllocParameter(char* p, MLAllocator a, MLDeallocator d)
sets the memory allocator and deallocator specified by a and d in the MLParameters object p for later use with MLInitialize().
  • Use MLSetAllocParameter() to install special memory allocators in the MathLink library. The library will use the allocators specified by a and d for all dynamic memory allocations.
  • a and d must have the same call signature as the C memory allocator functions malloc() and free().
  • a and d must be thread-safe.
  • MLSetAllocParameter() is declared in the MathLink header file mathlink.h.
#include "mathlink.h"

void * Custom_Allocator(size_t size)
    /* ... */

void Custom_Deallocator(void *ptr)
    /* ... */

int main()
    MLEnvironment env;
    MLParameters p;
    unsigned long revision;

    revision = MLNewParameters((char *)&p, MLREVISION, MLAPIREVISION);
    if(! revision)
        { /* unable to initialize the MLParameters object */ }

    MLSetAllocParameter(&p, Custom_Allocator, Custom_Deallocator);

    /* Install the Custom* memory allocator in MathLink. */
    env = MLInitialize(&p);
    if(env (MLENV)0)
        { /* unable to initialize MathLink environment */ }

    /* ... */

    return 0;