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.
You should be using the [evp\_\* functions](http://linux.die.net/man/3/evp_digestinit) rather than those lower level functions that you referenced. There are functions there for destroying and creating the digests.