I got a comment to my answer on this thread:
Malloc inside a function call appears to be getting freed on return?
In short I had code like this:
On the other hand, if you ever need to port the code to C++, it is much better to use the 'new' operator.