Question: I come from the python world, were the memory management is taken care of. Having recently started writing some serious C++ code, I am curious about the memory allocation practices. I know that K&R suggest to use `void` functions to do any sort of big-array manipulation. While that is a perfectly valid way of getting things done, I prefer that my function returns are a little more descriptive. 

Therefore, another solution is to have things like `int * function (int param) {}` that returns a memory block that has been `malloc`ed within the function. But there is no guarantee that whoever uses the function will `free` afterwards.

Here is the question then: "Is there a standard way to document memory-allocating functions?". In a way, I want to force the API user to `free`, based on my good faith that they will read the docs. 

Maybe there are some other ways to enforce simple reference counting `free`ing?

Before taking any answers, I am looking for `STL` answers, since we would like to avoid `Boost` as long as possible. 

Thanks all.

Answer: 
Modern C++ offers several so called [smart pointer](http://en.cppreference.com/w/cpp/memory) classes to help with memory management. It should normally not be necessary to use `malloc`/`free` or even `new`/`delete`. The most important smart pointer classes, which are standardized since C++11, are:

* [`std::unique_ptr<T>`](http://en.cppreference.com/w/cpp/memory/unique_ptr)
* [`std::shared_ptr<T>`](http://en.cppreference.com/w/cpp/memory/shared_ptr)
* [`std::weak_ptr<T>`](http://en.cppreference.com/w/cpp/memory/weak_ptr) (to break cycles with `std::shared_ptr<T>`)

To generate objects which are owned by a smart pointer, you should know about:

* [`std::make_shared`](http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared)
* [`std::make_unique`](http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique) (only since C++14, or [implement it yourself with C++11](https://stackoverflow.com/questions/7038357))

---

![Smart!](https://i.stack.imgur.com/n4qQ9.png)
I suggest declaring your function with a reference to, for example a vector as parameter, which ensures that the vector exists and is allocated by caller. therefore caller will be responsible for lifetime of object where you will just fill it inside callee.

<http://www.informit.com/articles/article.aspx?p=373338&seqNum=2>