question about inet_ntoa(3)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Anthony Roberts

    #1

    question about inet_ntoa(3)

    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
  • Keith Thompson

    #2
    Re: question about inet_ntoa(3)

    Anthony Roberts <acrobert-at-ucalgary-dot-ca@ucalgary.ca> writes:[color=blue]
    > 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.[/color]

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

    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.

    --
    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"

    Comment

    • Anthony Roberts

      #3
      Re: question about inet_ntoa(3)

      Keith Thompson wrote:[color=blue]
      >
      > The inet_ntoa() function is not defined by the C standard. You'll
      > probably get better information in comp.unix.progr ammer.[/color]

      I will try that.
      [color=blue]
      > 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.[/color]

      ewwww
      [color=blue]
      >(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.[/color]

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


      Thank you for the fast and helpful response. :)

      Comment

      • Keith Thompson

        #4
        Re: question about inet_ntoa(3)

        Anthony Roberts <acrobert-at-ucalgary-dot-ca@ucalgary.ca> writes:[color=blue]
        > Keith Thompson wrote:[color=green]
        > > The inet_ntoa() function is not defined by the C standard. You'll
        > > probably get better information in comp.unix.progr ammer.[/color]
        >
        > I will try that.
        >[color=green]
        > > 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.[/color]
        >
        > ewwww
        >[color=green]
        > >(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.[/color]
        >
        > If that were the case, you'd need to have a mutex to protect it.[/color]

        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.progr ammer is the place to ask about it
        if your documentation fails you <OT>man inet_ntoa</OT>.

        --
        Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
        San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
        Schroedinger does Shakespeare: "To be *and* not to be"

        Comment

        Working...