I need to have int**
to pass to API function call written in C. Based on the description of the API, it uses malloc
to allocate memory, and it requires the user to free the memory after it is returned.
How to create a std::unique_ptr<int*>
? If I have one, does .get()
function return a int**
. I was thinking if I could wrap my pointer in a unique_ptr
and make sure it will be freed when out of scope. I am not sure if the following implementation ensures that memory is freed after allocation? Is there a better way of achieving this?
int ** get_double_ptr()
{
int ** ids;
return ids;
}
int main(int argc, char **argv)
{
std::unique_ptr<int*> i_ptr(get_double_ptr());
int ** idx = i_ptr.get();
// allocate ids using malloc ....
}
The C function is declared as below:
int MeshToNodal(int *ne, int *nn, int *eptr, int *eind, int *numflag,
int **xadj, int **adjncy)