In c++, how take ownership of char* in a unique_ptr ?
- User
In c++, how take ownership of char* in a unique_ptr ?
Assistant
To take ownership of a
char*in aunique_ptrin C++:// For a single character char* single_char = new char('A'); std::unique_ptr<char> ptr1(single_char); // For a character array char* char_array = new char[100]; std::unique_ptr<char[]> ptr2(char_array); // If char* comes from a C function using malloc/strdup char* c_string = strdup("Hello"); std::unique_ptr<char, decltype(free)*> ptr3(c_string, free); // Direct creation (better approach) auto ptr4 = std::make_unique<char[]>(100); // C++14 and laterWould you like me to explain or break down this code?
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.