When a third-party function C returns a pointer, do you have to free it yourself?

There are many functions (especially in the POSIX library) that return pointers to nearly-needed freshly distributed data. Their manpages do not say if you should free them, or if there is some obscure mechanism in the game (for example, returning a pointer to a static buffer or something in that direction).

For example, the inet_ntoa function returns char* , most likely from nowhere, but the manpage does not say how it was allocated. I ended up using inet_ntop because at least I knew where the selection came from.

What standard rule for C functions returns pointers? Who is responsible for freeing his memory?

+7
c memory-management return-value
source share
5 answers

In fact, there is no standard rule. Some functions require you to pass a pointer, and they fill data into this space (for example, sprintf ). Others return the address of a static data area (for example, many of the functions in <time.h> ). Others still allocate memory when necessary (e.g. setvbuf ).

What you can do is hope that the documentation tells you which pointers need to be freed. Normally, you should not try to free the pointers that it returns if the documentation doesn't tell you. If you do not pass the address of the buffer to use it, or it indicates that you need to free memory, you should usually assume that it uses a static data area. This means (among other things) that you should assume that the value will be changed using any subsequent calls to the same procedure. If you write multi-threaded code, you usually assume that the function is not thread safe - you have a common data area that needs synchronization, so you must acquire a lock, call the function, copy data from its data area, and only then release the lock.

+3
source share

You should read the documentation, there is no other way. My page for inet_ntoa reads:

The string is returned in a statically distributed buffer that subsequent calls will be overwritten.

So, in this case you should not try to free the returned pointer.

+12
source share

There is no standard rule. Ideally, a standard library function, such as inet_ntoa, comes with a man page that describes the "rules of interaction", that is, the interface of the function - the expected arguments, return values ​​if successful and error, as well as the semantics of working with allocated memory.

On the inet_ntoa man page:

The inet_ntoa () function converts the Internet host address specified in the network byte order to a string in IPv4 with decimal precision. the string is returned in a statically allocated buffer, which then calls will be overwritten.

+1
source share

At least on my machine (Mac OS X 10.6) the last sentence manpage, under BUGS :

The string returned by inet_ntoa () is in the static memory area.

0
source share

I think your idea that the "many" functions in POSIX return pointers is thus erroneous. Your example, inet_ntoa not in POSIX , and was deliberately excluded because it is deprecated and broken.

The number of standard functions that return pointers to allocated memory is actually quite small, and most of them provide a special additional function that you should use to free memory (for example, fopen and fclose , getaddrinfo and freeaddrinfo , or regcomp and regfree ). Just calling free on the returned pointer would be very bad; in the best case, you will encounter serious memory leaks, and in the worst case, it can lead to unexpected failures (for example, if the library stores objects that it places in a linked list).

Regardless of whether the function is part of a system library or a third-party library, it should document the expected use of any pointers that it returns (and regardless of whether they need to free them). For standard functions, the best reference to this question is POSIX itself . You can also check the manual pages for your specific system. If the code is part of a third-party library, it should contain documentation (possibly on the manual pages, in header files, or in the complete document on using the library). A well-written library will provide special functions for the free objects that it allocates to avoid introducing dependencies on how it is (currently) implemented for code that uses the library.

As for non-standard inet_ntoa and similar obsolete functions, they return pointers to internal static buffers. This makes them unsuitable for use with streams or in library code (which should take care not to destroy the state of the caller if it is not documented). Often, the documentation for such functions says that they are not required for thread safety , that they are not reentrant, or that they can return a pointer to an internal static buffer that can be overwritten by subsequent calls to the function. Many people, including myself, believe that such functions should not be used at all in modern code.

0
source share

All Articles