From f00022aa66f7b7c6d0825b8a45944e35d8cb0cf6 Mon Sep 17 00:00:00 2001 From: Thorbjørn Lindeijer Date: Tue, 12 Mar 2024 15:38:30 +0100 Subject: Removed an unnecessary extra allocation in strprintf We can use vsnprintf to write to the std::string directly. --- src/utils/stringutils.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/utils/stringutils.cpp b/src/utils/stringutils.cpp index a1637892..7a951ec6 100644 --- a/src/utils/stringutils.cpp +++ b/src/utils/stringutils.cpp @@ -94,13 +94,12 @@ std::string strprintf(char const *format, ...) return buf; } // The static size was not big enough, try again with a dynamic allocation. - ++nb; - char *buf2 = new char[nb]; + ++nb; // Add 1 for the null terminator. + + std::string res(nb, char()); va_start(args, format); - vsnprintf(buf2, nb, format, args); + vsnprintf(res.data(), nb, format, args); va_end(args); - std::string res(buf2); - delete [] buf2; return res; } -- cgit v1.2.3-70-g09d2