---
name: "freebsd::malloc_up"
section: "3"
platform: "freebsd"
package: "base"
version: "16.0-CURRENT-20261027-61c70dc16a79"
release: "FreeBSD 16.0-CURRENT (2026-10-27)"
platforms: ["freebsd"]
url: "https://new.carta.tech/freebsd::malloc_up"
---

# 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)](/free/3.md) instead of **delete**() when an object is disposed. While explicit calls to [malloc(3)](/malloc/3.md) should be avoided in C++ code, this class can be useful to manage an object allocated by an existing API which uses [malloc(3)](/malloc/3.md) internally such as [scandir(3)](/scandir/3.md). 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)](/strdup/3.md) for simplicity, but new C++ code should generally not use [strdup(3)](/strdup/3.md):

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

## SEE ALSO

[free(3)](/free/3.md), [malloc(3)](/malloc/3.md), [scandir(3)](/scandir/3.md), [strdup(3)](/strdup/3.md)
