question about inet_ntoa(3)

A

Anthony Roberts

It returns a char*... but where is the buffer? free() prints a debug
message saying it wasn't allocated when I try to free it. That implies
that there's a buffer somewhere managed by someone else. I'm okay with
that, but it would be a bit weird.

The man pages on OpenBSD 3.4 and MacOS 10.3 don't say anything about
it... and OpenBSD would have a paragraph on the issue if it was a
problem. But I'm still confused, so I'd be a happy man if anyone could
confirm that the buffer isn't my problem.

TIA for any help,
Anthony
 
K

Keith Thompson

Anthony Roberts said:
It returns a char*... but where is the buffer? free() prints a debug
message saying it wasn't allocated when I try to free it. That implies
that there's a buffer somewhere managed by someone else. I'm okay with
that, but it would be a bit weird.

The man pages on OpenBSD 3.4 and MacOS 10.3 don't say anything about
it... and OpenBSD would have a paragraph on the issue if it was a
problem. But I'm still confused, so I'd be a happy man if anyone could
confirm that the buffer isn't my problem.

The inet_ntoa() function is not defined by the C standard. You'll
probably get better information in comp.unix.programmer.

In general, it's fairly common for functions that return strings to
return a pointer to a static buffer; the buffer is overwritten by
subsequent calls. (This can cause difficulties for multi-threaded
programs, but the C standard doesn't concern itself with
multi-threading.) I don't know whether inet_ntoa() does this.
 
A

Anthony Roberts

Keith said:
The inet_ntoa() function is not defined by the C standard. You'll
probably get better information in comp.unix.programmer.

I will try that.
In general, it's fairly common for functions that return strings to
return a pointer to a static buffer; the buffer is overwritten by
subsequent calls.
ewwww

(This can cause difficulties for multi-threaded
programs, but the C standard doesn't concern itself with
multi-threading.) I don't know whether inet_ntoa() does this.

If that were the case, you'd need to have a mutex to protect it.


Thank you for the fast and helpful response. :)
 
K

Keith Thompson

Anthony Roberts said:
I will try that.


If that were the case, you'd need to have a mutex to protect it.

It's also common to provide reentrant versions of such functions in
addition to the older non-reentrant versions. The reentrant version
typically has a name ending in "_r", and takes additional arguments
specifying the base address and size of a user-supplied buffer. I
don't know whether there is (or needs to be) such a function for
inet_ntoa(); again comp.unix.programmer is the place to ask about it
if your documentation fails you <OT>man inet_ntoa</OT>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top