Refactor to avoid use of `malloc` in C++. To get a `char*`, use C++11 style `&buf[0]` rather than C++17 `buf.data()`.