I am reorganizing the old code. There is a C style function that works as follows: (Obviously, I have simplified this here)
int LoadData(char** buf1, int* buf1Len, char** buf2, int* buf2Len) {
*buf1Len = DetermineLength1();
*buf1 = (char*)malloc(*buf1Len);
*buf2Len = DetermineLength2();
*buf2 = (char*)malloc(*buf2Len);
int result = 0;
return result;
}
Now, I would like to update this code to somehow return a unique_ptr or equivalent, so the pointer will be automatically controlled by the caller and the caller will never forget to free up memory.
I did not find a good solution, so currently I have changed the code to the following:
int LoadData(std::unique_ptr<char[]>* ubuf1, int* buf1Len, std::unique_ptr<char[]>* ubuf2, int* buf2Len) {
ubuf1->reset(buf1);
ubuf2->reset(buf2);
return result;
}
This does not look good, so I am looking to see if there is a better solution. Since I am returning two buffers, use unique_ptras the return value is not an option.
Is there a better way to do this?