Reference

freebsd::malloc_up(3)

std::unique_ptr specialization for objects allocated via malloc

NAME

freebsd::malloc_up – std::unique_ptr specialization for objects allocated via malloc

LIBRARY

library “libutil++”

SYNOPSIS

#include <libutil++.hh>

using malloc_up = std::unique_ptr<T, free_deleter<T>>;

DESCRIPTION

This class is a specialization of std::unique_ptr which invokes free(3) instead of delete() when an object is disposed. While explicit calls to malloc(3) should be avoided in C++ code, this class can be useful to manage an object allocated by an existing API which uses malloc(3) internally such as scandir(3). Note that the type of the underlying object must be used as the first template argument similar to std::unique_ptr.

EXAMPLES

This example uses strdup(3) for simplicity, but new C++ code should generally not use strdup(3):

freebsd::malloc_up<char> my_string(strdup("foo"));
// `mystring' is implicitly freed on destruction

SEE ALSO

free(3), malloc(3), scandir(3), strdup(3)