I'm in the process of refactoring some old code. There's a C style function that works like this: (Obviously I've simplified it here)
int LoadData(char** buf1, int* buf1Len, char** buf2, int* buf2Len) {
*buf1Len = DetermineLength1();
*buf1 = (char*)malloc(*buf1Len);
// Fill buf1
*buf2Len = DetermineLength2();
*buf2 = (char*)malloc(*buf2Len);
// Fill buf2
int result = 0; // Or some other INT depending of result
return result;
}
Now, I'd like to update this code to somehow return a unique_ptr or equivalent, so the pointer will be automatically managed by the caller, and the caller won't ever forget to free the memory.
I couldn't find a good solution, so currently I've changed the code to the following:
int LoadData(std::unique_ptr<char[]>* ubuf1, int* buf1Len, std::unique_ptr<char[]>* ubuf2, int* buf2Len) {
// same code as above, and finally:
ubuf1->reset(buf1);
ubuf2->reset(buf2);
return result;
}
This doesn't look good, so I'm looking to see if there's a better solution. Since I'm returning two buffers, using the unique_ptr
as the return value is not an option.
Is there any better way for this?