Forums.ATC.no

Teknisk => Generelt teknisk => Emne startet av: Floyd-ATC på 29. ſeptember 2020, 11:48 am

Tittel: C++ unique_ptr wrapper for strdup()
Skrevet av: Floyd-ATC29. ſeptember 2020, 11:48 am
How to wrap a char* from strdup() in a smart pointer to guarantee it gets freed?

Kode: [Velg]
#include <memory>
typedef std::unique_ptr<char, decltype(&std::free)> char_ptr;

char_ptr make_char_ptr(const std::string str)
{
  return char_ptr(strdup(str.data()), std::free);
}

//...

char_ptr foo = make_char_ptr("bar");
foo.get(); // char* pointer to a copy of "bar"

// pointer automatically gets freed when 'foo' goes out of scope
// or can be freed manually with
foo.reset();
Tittel: The best thing about comics
Skrevet av: Beauvais07. November 2020, 14:35 pm
The best thing about comics is that a comic creator can control when and where the reader sees exciting things in the comic. These exciting moments in the comic are generally placed after the turn of a page To Know More (https://www.dccomics.com/talent/cameron-stewart).