Memory

These functions provide support for allocating and freeing memory.

Note

If any call to allocate memory fails, the application is terminated. This also means that there is no need to check if the call succeeded.

Note

It's important to match g_malloc() with g_free(), plain malloc() with free(), and (if you're using C++) new with delete and new[] with delete[]. Otherwise bad things can happen, since these allocators may use different memory pools (and new/delete call constructors and destructors). See also g_mem_set_vtable().

Members

Static functions

clearPointer
void clearPointer(void** pp, GDestroyNotify destroy)

Clears a reference to a variable. pp must not be NULL. If the reference is NULL then this function does nothing. Otherwise, the variable is destroyed using destroy and the pointer is set to NULL. This function is threadsafe and modifies the pointer atomically, using memory barriers where needed. A macro is also included that allows this function to be used without pointer casts. Since 2.34

free
void free(void* mem)

Frees the memory pointed to by mem. If mem is NULL it simply returns.

malloc
void* malloc(gsize nBytes)

Allocates n_bytes bytes of memory. If n_bytes is 0 it returns NULL.

malloc0
void* malloc0(gsize nBytes)

Allocates n_bytes bytes of memory, initialized to 0's. If n_bytes is 0 it returns NULL.

malloc0_N
void* malloc0_N(gsize nBlocks, gsize nBlockBytes)

This function is similar to g_malloc0(), allocating (n_blocks * n_block_bytes) bytes, but care is taken to detect possible overflow during multiplication. Since 2.24

mallocN
void* mallocN(gsize nBlocks, gsize nBlockBytes)

This function is similar to g_malloc(), allocating (n_blocks * n_block_bytes) bytes, but care is taken to detect possible overflow during multiplication. Since 2.24

memIsSystemMalloc
int memIsSystemMalloc()

Checks whether the allocator used by g_malloc() is the system's malloc implementation. If it returns TRUE memory allocated with malloc() can be used interchangeable with memory allocated using g_malloc(). This function is useful for avoiding an extra copy of allocated memory returned by a non-GLib-based API. A different allocator can be set using g_mem_set_vtable().

memProfile
void memProfile()

Outputs a summary of memory usage. It outputs the frequency of allocations of different sizes, the total number of bytes which have been allocated, the total number of bytes which have been freed, and the difference between the previous two values, i.e. the number of bytes still in use. Note that this function will not output anything unless you have previously installed the glib_mem_profiler_table with g_mem_set_vtable().

memSetVtable
void memSetVtable(GMemVTable* vtable)

Sets the GMemVTable to use for memory allocation. You can use this to provide custom memory allocation routines. This function must be called before using any other GLib functions. The vtable only needs to provide malloc(), realloc(), and free() functions; GLib can provide default implementations of the others. The malloc() and realloc() implementations should return NULL on failure, GLib will handle error-checking for you. vtable is copied, so need not persist after this function has been called.

memdup
void* memdup(void* mem, uint byteSize)

Allocates byte_size bytes of memory, and copies byte_size bytes into it from mem. If mem is NULL it returns NULL.

realloc
void* realloc(void* mem, gsize nBytes)

Reallocates the memory pointed to by mem, so that it now has space for n_bytes bytes of memory. It returns the new address of the memory, which may have been moved. mem may be NULL, in which case it's considered to have zero-length. n_bytes may be 0, in which case NULL will be returned and mem will be freed unless it is NULL.

reallocN
void* reallocN(void* mem, gsize nBlocks, gsize nBlockBytes)

This function is similar to g_realloc(), allocating (n_blocks * n_block_bytes) bytes, but care is taken to detect possible overflow during multiplication. Since 2.24

tryMalloc
void* tryMalloc(gsize nBytes)

Attempts to allocate n_bytes, and returns NULL on failure. Contrast with g_malloc(), which aborts the program on failure.

tryMalloc0
void* tryMalloc0(gsize nBytes)

Attempts to allocate n_bytes, initialized to 0's, and returns NULL on failure. Contrast with g_malloc0(), which aborts the program on failure. Since 2.8

tryMalloc0_N
void* tryMalloc0_N(gsize nBlocks, gsize nBlockBytes)

This function is similar to g_try_malloc0(), allocating (n_blocks * n_block_bytes) bytes, but care is taken to detect possible overflow during multiplication. Since 2.24

tryMallocN
void* tryMallocN(gsize nBlocks, gsize nBlockBytes)

This function is similar to g_try_malloc(), allocating (n_blocks * n_block_bytes) bytes, but care is taken to detect possible overflow during multiplication. Since 2.24

tryRealloc
void* tryRealloc(void* mem, gsize nBytes)

Attempts to realloc mem to a new size, n_bytes, and returns NULL on failure. Contrast with g_realloc(), which aborts the program on failure. If mem is NULL, behaves the same as g_try_malloc().

tryReallocN
void* tryReallocN(void* mem, gsize nBlocks, gsize nBlockBytes)

This function is similar to g_try_realloc(), allocating (n_blocks * n_block_bytes) bytes, but care is taken to detect possible overflow during multiplication. Since 2.24

Meta