---
name: "mem_open"
section: "3debug"
platform: "linux"
package: "libdebug0-dev"
version: "0.5.3-8+b1"
release: "Debian 13 (Trixie)"
platforms: ["linux"]
url: "https://new.carta.tech/mem_open"
---

# mem\_open(3debug)

memory allocation routines for debugging

## NAME

mem\_open, mem\_check, mem\_alloc, mem\_realloc, mem\_free, mem\_close - memory allocation routines for debugging

## LIBRARIES

Debug Library (-ldebug)

## SYNOPSIS

```
#include <debug/memory.h>

void mem_open(void (*fail)(const char *fmt, ...));
void mem_check(void);
void *mem_alloc(size_t size);
void *mem_realloc(void *ptr, size_t size);
void mem_free(void *ptr);
void mem_close(void);
```

## DESCRIPTION

**mem\_open()** initializes the memory debugging system. It should be called before any of the other routines. You can specify a callback function which should be called whenever something bad happens, or NULL in which case the default error handler will be used. The default error handler logs error messages using the debug logging routines and exit.

**mem\_check()** check all the allocated memory areas. This is called every time memory is allocated or freed. You can also call it anytime you think memory might be corrupted.

**mem\_alloc()** allocates *size* bytes and returns a pointer to the allocated memory. The memory is not cleared.

**mem\_realloc()** changes the size of the memory block pointed to by *ptr* to *size* bytes. The contents will be unchanged to the minimum of the old and new sizes; newly allocated memory will be uninitialized. If *ptr* is **NULL**, the call is equivalent to **mem\_alloc(size)**; if size is equal to zero, the call is equivalent to **mem\_free(ptr)**. Unless *ptr* is **NULL**, it must have been returned by an earlier call to **mem\_alloc()** or **mem\_realloc()**.

**mem\_free()** frees the memory space pointed to by *ptr*, which must have been returned by a previous call to **mem\_alloc()** or **mem\_realloc()**. If *ptr* is **NULL**, no operation is performed.

**mem\_close()** checks for leaks and possible memory corruptions.

## RETURN VALUE

For **mem\_alloc()**, the value returned is a pointer to the allocated memory, which is suitably aligned for any kind of variable, or **NULL** if the request fails.

**mem\_realloc()** returns a pointer to the newly allocated memory, which is suitably aligned for any kind of variable and may be different from *ptr*, or **NULL** if the request fails or if size was equal to 0. If **mem\_realloc()** fails the original block is left untouched - it is not freed or moved.

All other functions returns no value.

## NOTES

If the default fail callback is used or if this routines are combined with the log routines, care should be taken to call open and close functions in the right order. The correct order is as follows:

```
mem_open (NULL);
log_open (NULL,LOG_NORMAL,LOG_HAVE_COLORS | LOG_PRINT_FUNCTION);
atexit (mem_close);
atexit (log_close);
```

Of course, [**atexit**(3)](/atexit/3.md) should only be used if the program will not forked.

None of the libdebug routines are thread-safe. I'm not planning to change this either! For more information, please see http://threading.2038bug.com/

## SEE ALSO

[log\_open(3)](/log_open/3.md), [atexit(3)](/atexit/3.md)

## AUTHOR

Written by Abraham vd Merwe \<abz@blio.com\>
