carta
freebsd::malloc_up(3)

freebsd::malloc_up3

Std::unique_ptr specialization for objects allocated via malloc

Usage

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

Library

library “libutil++”

Overview

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

From base 16.0-CURRENT-20261027-61c70dc16a79 in FreeBSD 16.0-CURRENT (2026-10-27). Parsed by carta from the page's mdoc source.