if i call to function like inet_ntoa that return char*.
Who is responsible to free the allocated memory?
My method look like this:
char* inet_aton_(unsigned int atonIp){
in_addr sin_addr;
sin_addr.s_addr = atonIp;
return inet_ntoa(sin_addr);
}
You should check the manual for that.
$man inet_ntoa
This means in your application you’ll have a statically allocated buffer, and you are responsible for what’s in there. Each call to inet_ntoa() in your application will override this area. If you need to keep for example two addresses you have to copy this data to another buffer. You are also responsible for using in a correct way in a threaded application, for example if you have two threads calling inet_ntoa at the same time you’ll either get wrong results or two seperate calls to inet_ntoa with different parameters will return the same result.
You should also look into source code when you have this kind of questions. For example if you search for “inet_ntoa source”. You may encounter some examples; freebsd or ms. One example implementation is below (from freebsd).
As you can see it has a statically buffer allocated only for this purpose. This makes usage of inet_ntoa very easy since you don’t need to manage the buffer but also makes it not thread safe.
Another thing, inet_ntoa is not a system call. It can be called a convenience function provided by inet library.