I want to use SHA1 function from Linux libraries (documentation) 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.
Share
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 callfree()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():it looks to me like the return pointer is either going to be pointing to the array you’ve input via the
mdargument, or, if themdargument isNULL, 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.