Question: I want to use SHA1 function from Linux libraries ([documentation](http://linux.die.net/man/3/sha)) which returns unsigned char pointer. As I understand it creates a new array but doesn't it mean I have to free the memory used for it? And how do I know whether I should use free or delete[]? I guess this particular functions uses C way of allocating memory but how do I know in general? Some C++ functions return char arrays rather than std::string.

Answer: 
Keep in mind while it might have allocated the memory on the heap using `malloc`, it could also be returning a pointer to a static fixed-length array private to the scope of the function. That's not untypical for library functions on Linux, especially those that are not re-entrant. So if you call `free()` on a pointer that is pointing to a static array, you're going to get some type of undefined behavior.

For instance, from this segment of the documentation for `SHA1()`:

> 
> SHA1 () computes the SHA-1 message digest of the n bytes at d and
>  places it in md (which must have space for SHA\_DIGEST\_LENGTH == 20
>  bytes of output). If md is NULL , the digest is placed in a static
>  array.
> 
> 
> 

it looks to me like the return pointer is either going to be pointing to the array you've input via the `md` argument, or, if the `md` argument is `NULL`, the return pointer is pointing to a static array private to the function. Nowhere do I see it mentioned that you must explicitly free the pointer returned by the function.
The only general and correct answer: Read the documentation. Every foreign function that you use must tell you in its documentation what sort of input it expects and what it returns.

If it says that it returns a pointer to memory allocated with `malloc` (or equivalent), you must `free` it yourself.

(Practically, you might sometimes perhaps be able to run `valgrind` and base an educated guess on the report, but that's not a "solution" in as much as it is guesswork.)