Why strdup

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • lovecreatesbea...@gmail.com

    Why strdup

    I am trying the FREE net-snmp library things. Like the FREE libxml++
    library, it lacks of a reasonable document on its API. Its very
    begining example code compiles and runs. It says "blah blah .." when I
    substitute the host address for a snmp equipped machine
    ``192.168.5.10' ' within our LAN like this:

    session.peernam e = strdup("192.168 .5.10");

    Is strdup still in use? Is there anything prevents the code from
    becoming these:

    char *p = "192.168.5. 10";
    char a[] = 192.168.5.10";

    Thank you for your time.
  • rahul

    #2
    Re: Why strdup

    On Jun 25, 3:02 pm, "lovecreatesbea ...@gmail.com"
    <lovecreatesbea ...@gmail.comwr ote:
    I am trying the FREE net-snmp library things. Like the FREE libxml++
    library, it lacks of a reasonable document on its API. Its very
    begining example code compiles and runs. It says "blah blah .." when I
    substitute the host address for a snmp equipped machine
    ``192.168.5.10' ' within our LAN like this:
    >
    session.peernam e = strdup("192.168 .5.10");
    strdup is not conforming to ISO C. It is conforming to POSIX.
    Is strdup still in use? Is there anything prevents the code from
    becoming these:
    >
    char *p = "192.168.5. 10";
    strdup returns a pointer to character. So the call is the same as you
    mention.
    char a[] = 192.168.5.10";
    Here a is an array ( p is a pointer in the previous case) and this can
    be done in the initialization.
    char a[10];
    a = "abc"; /* will generate an error */

    From what you have posted, peername appears to be char *. so the call
    is char *p = "192.168.5. 10"

    There is one subtle difference though. String literals may be stored
    in read-only memory but strdup does the allocation using malloc.

    Comment

    • Chris Dollin

      #3
      Re: Why strdup

      lovecreatesbea. ..@gmail.com wrote:
      I am trying the FREE net-snmp library things. Like the FREE libxml++
      library, it lacks of a reasonable document on its API. Its very
      begining example code compiles and runs. It says "blah blah .." when I
      substitute the host address for a snmp equipped machine
      ``192.168.5.10' ' within our LAN like this:
      >
      session.peernam e = strdup("192.168 .5.10");
      >
      Is strdup still in use? Is there anything prevents the code from
      becoming these:
      >
      char *p = "192.168.5. 10";
      char a[] = 192.168.5.10";
      >
      Thank you for your time.
      If the code that handles the session object expects to be able
      to free or realloc the peername, then copying the string literal
      to mallocated store is essential.

      --
      "It's a hat as long as I /say/ it's a hat." /A College of Magics/

      Hewlett-Packard Limited registered office: Cain Road, Bracknell,
      registered no: 690597 England Berks RG12 1HN

      Comment

      • Richard Tobin

        #4
        Re: Why strdup

        In article <1bce78e0-2d2b-4621-8382-8f9e9c79c0dc@z3 2g2000prh.googl egroups.com>,
        lovecreatesbea. ..@gmail.com <lovecreatesbea uty@gmail.comwr ote:
        >Is strdup still in use? Is there anything prevents the code from
        >becoming these:
        >
        > char *p = "192.168.5. 10";
        > char a[] = 192.168.5.10";
        Quite likely the copied string is passed or returned to functions which
        normally get malloc()ed strings. If these strings were not strdup()ed,
        the code would have to keep track of which strings were static and
        which needed to be free()ed.

        -- Richard
        --
        In the selection of the two characters immediately succeeding the numeral 9,
        consideration shall be given to their replacement by the graphics 10 and 11 to
        facilitate the adoption of the code in the sterling monetary area. (X3.4-1963)

        Comment

        • lovecreatesbea...@gmail.com

          #5
          Re: Why strdup

          On Jun 25, 6:21 pm, rahul <rahulsin...@gm ail.comwrote:
          On Jun 25, 3:02 pm, "lovecreatesbea ...@gmail.com"
          >
          <lovecreatesbea ...@gmail.comwr ote:
          I am trying the FREE net-snmp library things. Like the FREE libxml++
          library, it lacks of a reasonable document on its API. Its very
          begining example code compiles and runs. It says "blah blah .." when I
          substitute the host address for a snmp equipped machine
          ``192.168.5.10' ' within our LAN like this:
          >
          session.peernam e = strdup("192.168 .5.10");
          >
          strdup is not conforming to ISO C. It is conforming to POSIX.
          >
          Is strdup still in use? Is there anything prevents the code from
          becoming these:
          >
          char *p = "192.168.5. 10";
          >
          strdup returns a pointer to character. So the call is the same as you
          mention.
          >
          char a[] = 192.168.5.10";
          >
          Sorry for the missed quote mark,

          char a[] = "192.168.5. 10";
          Here a is an array ( p is a pointer in the previous case) and this can
          be done in the initialization.
          char a[10];
          a = "abc"; /* will generate an error */
          >
          Yes. I would copy or assign the data to array elements.
          From what you have posted, peername appears to be char *. so the call
          is char *p = "192.168.5. 10"
          >
          There is one subtle difference though. String literals may be stored
          in read-only memory but strdup does the allocation using malloc.
          Yes, and the caller needs to remember to call an extra free() on it
          somewhere. I think the need of strdup() in program indicates design
          flaw.

          Comment

          • lovecreatesbea...@gmail.com

            #6
            Re: Why strdup

            On Jun 25, 10:07 pm, "Joachim Schmitz" <nospam.j...@sc hmitz-
            digital.dewrote :
            lovecreatesbea. ..@gmail.com wrote:
            On Jun 25, 6:35 pm, pete <pfil...@mindsp ring.comwrote:
            lovecreatesbea. ..@gmail.com wrote:
            > session.peernam e = strdup("192.168 .5.10");
            ...
            I only know strdup as a code example from K&R2,
            section 6.5, self-referential structures.
            If you want to use that version of it,
            1 remove the cast, because it doesn't help
            ...
            and it can prevent a C89 compiler
            from diagnosing undefined behavior
            in the event that <stdlib.his not #included.
            >
            The -ansi option gives out a warning on below the code without that
            cast. Why ansi is not ansi?
            $ gcc -ansi -pedantic a.c
            a.c: In function ‘main’:
            a.c:7: warning: initialization makes pointer from integer without a
            cast
            $ cat a.c
            char *s = strdup("192.168 .5.10"); /*LINE 7*/
            Probably because with -ansi -pedantic there's no prototype for the
            non-standard strdup in scope henve it defaults to be assumed to return int.
            Adding -Wall (and maybe more) should have warned about an implicit
            declaration.
            Thank you. I see the change.

            I find the only declaration surrounded by the preprocessing directive.
            It may be this one that have been skipped with -ansi -pedantic
            options.

            ...

            #if defined __USE_SVID || defined __USE_BSD || defined
            __USE_XOPEN_EXT ENDED
            /* Duplicate S, returning an identical malloc'd string. */
            extern char *strdup (__const char *__s)
            __THROW __attribute_mal loc__ __nonnull ((1));
            #endif

            ...
            "/usr/include/string.h" [readonly] 428 lines --19%--

            Comment

            Working...