9.4.6.1 Memory Allocation

The standard C library memory allocation functions (malloc, calloc, realloc, and free) are available in foreign code, but cannot reuse any free memory that SICStus Prolog's memory manager may have available, and so may contribute to memory fragmentation.

The following functions provide the same services via SICStus Prolog's memory manager.

void * SP_malloc(size_t size)
Returns a pointer to a block of at least size bytes.
void * SP_calloc(size_t nmemb, size_t size)
Returns a pointer to a block of at least size * nemb. The first size * nmemb bytes are set to zero.
void * SP_realloc(void *ptr, size_t size)
Changes the size of the block referenced by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. The block referenced by ptr must have been obtained by a call to SP_malloc() or SP_realloc(), and must not have been released by a call to SP_free() or SP_realloc().
void SP_free(void *ptr)
Releases the block referenced by ptr, which must have been obtained by a call to SP_malloc() or SP_realloc(), and must not have been released by a call to SP_free() or SP_realloc().
char * SP_strdup(const char *str)
Returns a pointer to a new string, which is a duplicate of the string pointer to by str. The memory for the new string is allocated using SP_malloc().