Manage the allocation by smart pointer

101 views Asked by At

In C++ I want to define a function which performs the deep copy of a list. The function is like:

ListNode *copyList(ListNode *head) {
  // the deep copy of the list 
}

The declaration of the function is fixed, I can't change it. I have to new as many list nodes as the original ones, but I can't force the user to remember delete them, which leads me to the use of smart pointers.

However, the return type is the raw pointer, the smart pointers defined within the function scope will expire at the end of the function, or at least to my limited understanding.

Please help me with this scenario which seems awkward and subtle. Thank you.

0

There are 0 answers