Table Of Contents

Previous topic

Time Functions

Next topic

Byteswapping Functions

Memory Allocation

Macros for use in memory allocation and deallocation. These macros wrap the standard malloc, calloc, realloc and free functions and should always be preferred over these functions.

Also see Memory Allocation Example.

Summary

S_MALLOC Allocate memory to a pointer, given a defined type.
S_MALLOC_SIZE Allocate memory to a pointer, given a size in bytes.
S_CALLOC Allocate memory to a pointer and set it to zero, given a defined type.
S_CALLOC_SIZE Allocate memory to a pointer and set it to zero, given a size in bytes.
S_REALLOC Change the size of the memory block, given a defined type.
S_REALLOC_SIZE Change the size of the memory block, given a size in bytes.
S_FREE Free previously allocated memory.

Malloc

macro S_MALLOC(TYPE, NMEMB)

Allocate memory to a pointer, given a defined type.

Allocate NMEMB number of TYPE data type and return a pointer to the allocated memory.

Parameters:
  • TYPE

    The data type to allocate.

  • NMEMB

    The number of TYPE data type to allocate.

Return:

Pointer to the allocated memory or NULL if failed.

Note:

If (sizeof(TYPE) * NMEMB) = 0 then NULL is returned.

macro S_MALLOC_SIZE(SIZE)

Allocate memory to a pointer, given a size in bytes.

Allocate SIZE bytes and return a pointer to the allocated memory.

Parameters:
  • SIZE

    The number bytes to allocate.

Return:

Pointer to the allocated memory or NULL if failed.

Note:

If SIZE = 0 then NULL is returned.

Calloc

macro S_CALLOC(TYPE, NMEMB)

Allocate memory to a pointer and set it to zero, given a defined type.

Allocate NMEMB number of TYPE data type and returns a pointer to the allocated memory.

Parameters:
  • TYPE

    The data type to allocate.

  • NMEMB

    The number of TYPE data type to allocate.

Return:

Pointer to the allocated memory (set to zero) or NULL if failed.

Note:

If (sizeof(TYPE) * NMEMB) = 0 then NULL is returned.

macro S_CALLOC_SIZE(SIZE)

Allocate memory to a pointer and set it to zero, given a size in bytes.

Allocate SIZE bytes and returns a pointer to the allocated memory.

Parameters:
  • SIZE

    The number bytes to allocate.

Return:

Pointer to the allocated memory (set to zero) or NULL if failed.

Note:

If SIZE = 0 then NULL is returned.

Realloc

macro S_REALLOC(P, TYPE, NMEMB)

Change the size of the memory block, given a defined type.

Change the size of the memory block pointed to by P to NMEMB number of TYPE data type.

Parameters:
  • P

    The pointer to the memory of which to change the size.

  • TYPE

    The data type to allocate.

  • NMEMB

    The number of TYPE data type to allocate.

Return:

Pointer to the allocated memory or NULL if failed.

Note:

If (sizeof(TYPE) * NMEMB) = 0 then P is freed and NULL returned.

If memory allocation failed then P is freed.

macro S_REALLOC_SIZE(P, SIZE)

Change the size of the memory block, given a size in bytes.

Changes the size of the memory block pointed to by P to SIZE bytes.

Parameters:
  • P

    The pointer to the memory of which to change the size.

  • SIZE

    The number of bytes to allocate.

Return:

Pointer to the allocated memory or NULL if failed.

Note:

If SIZE = 0 then P is freed and NULL returned.

If memory allocation failed then P is freed.

Free

macro S_FREE(P)

Free previously allocated memory.

Free the memory space pointed to by P.

Parameters:
  • P

    The memory space to free.

Note:

Sets P to NULL.