Skip to main content
Expanded my answer.
Source Link
JRobert
  • 15.4k
  • 3
  • 25
  • 53

InI'd write such a function, I take to expect the destinationcalling function to allocate the array and pass it and its size as an argumentarguments. That leaves the memory useallocation and disposaldeallocation up to theeach library user, who canmay want/need (for other reasons) to use a static array, borrow from the heap memory, or allocate an automatic array, as suits their purpose and allows them to de-allocate it (or not) when it istheir application no longer neededneeds it. Coding these details into your library would restrict its usability.

In such a function, I take the destination array as an argument. That leaves the memory use and disposal up to the library user, who can use a static array, borrow from the heap, or allocate an automatic array, as suits their purpose and allows them to de-allocate it when it is no longer needed.

I'd write such a function to expect the calling function to allocate the array and pass it and its size as arguments. That leaves the memory allocation and deallocation up to each library user, who may want/need (for other reasons) to use a static array, heap memory, or an automatic array and allows them to de-allocate it (or not) when their application no longer needs it. Coding these details into your library would restrict its usability.

Source Link
JRobert
  • 15.4k
  • 3
  • 25
  • 53

In such a function, I take the destination array as an argument. That leaves the memory use and disposal up to the library user, who can use a static array, borrow from the heap, or allocate an automatic array, as suits their purpose and allows them to de-allocate it when it is no longer needed.