what does strdup return

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

    what does strdup return

    Hi,

    It's known that strdup() returns a char* type, but sometimes the
    following code generates warning messages.

    char *x = ...
    char *p = strdup(x);

    You must change it to

    char *p = (char*) strdup(x);

    why?
  • Nick Keighley

    #2
    Re: what does strdup return

    On 2 Oct, 10:56, thomas <FreshTho...@gm ail.comwrote:
    It's known that strdup() returns a char* type,
    strdup() isn't mentioned in the C Standard and it's in a reserved
    namespace to boot; so you can't define it yourself.
    So we don't really know anything about it.

    but sometimes the
    following code generates warning messages.
    >
    char *x = ...
    char *p = strdup(x);
    >
    You must change it to
    >
    char *p = (char*) strdup(x);
    >
    why?
    I suspect there is no prototype in scope for strdup().
    string.h may not declare it (I'm not sure it's allowed to declare
    it, in conmforming mode).

    You need to ask the question of your implementation for more
    certain guidance.


    --
    Nick Keighley

    Server rooms should be unfriendly! They should look dangerous,
    they should be uncomfortably cold, they should be inexplicably noisy.
    If you can arrange for the smell of burning transistors, that's good
    too.
    If anything, I'd rather see rackmounted servers designed in dark
    foreboding
    colors with lots of metal spikes sticking out of them.
    Mike Sphar (on news//alt.sysadmin.re covery)

    Comment

    • Richard Tobin

      #3
      Re: what does strdup return

      In article <c2e4f684-8204-4814-bdc4-7a074b1d5baa@25 g2000hsk.google groups.com>,
      thomas <FreshThomas@gm ail.comwrote:
      >It's known that strdup() returns a char* type, but sometimes the
      >following code generates warning messages.
      >
      >char *x = ...
      >char *p = strdup(x);
      >
      >You must change it to
      >
      >char *p = (char*) strdup(x);
      >
      >why?
      Because you have not declared strdup(), and not included a header that
      declares it. You have also apparently not run your compiler in a mode
      where it complains about undeclared functions.

      "Fixing" the problem by adding a cast won't work reliably on systems
      where pointers are longer than integers, or where the return
      conventions for pointers and integers are different.

      -- Richard


      --
      Please remember to mention me / in tapes you leave behind.

      Comment

      • vippstar@gmail.com

        #4
        Re: what does strdup return

        On Oct 2, 1:05 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
        wrote:
        On 2 Oct, 10:56, thomas <FreshTho...@gm ail.comwrote:
        >
        It's known that strdup() returns a char* type,
        >
        strdup() isn't mentioned in the C Standard and it's in a reserved
        namespace to boot; so you can't define it yourself.
        You can if you don't include <string.h>
        but sometimes the
        following code generates warning messages.
        >
        char *x = ...
        char *p = strdup(x);
        >
        You must change it to
        >
        char *p = (char*) strdup(x);
        >
        why?
        >
        I suspect there is no prototype in scope for strdup().
        string.h may not declare it (I'm not sure it's allowed to declare
        it, in conmforming mode).
        It is allowed, since as you said, strdup is implementation namespace.

        Comment

        • pete

          #5
          Re: what does strdup return

          vippstar@gmail. com wrote:
          On Oct 2, 1:05 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
          wrote:
          >On 2 Oct, 10:56, thomas <FreshTho...@gm ail.comwrote:
          >>
          >>It's known that strdup() returns a char* type,
          >strdup() isn't mentioned in the C Standard and it's in a reserved
          >namespace to boot; so you can't define it yourself.
          >
          You can if you don't include <string.h>
          No.
          N869
          7.26 Future library directions
          [#1] The following names are grouped under individual
          headers for convenience. All external names described below
          are reserved no matter what headers are included by the
          program.

          7.26.10 General utilities <stdlib.h>
          [#1] Function names that begin with str and a lowercase
          letter


          --
          pete

          Comment

          • vippstar@gmail.com

            #6
            Re: what does strdup return

            On Oct 2, 3:14 pm, pete <pfil...@mindsp ring.comwrote:
            vipps...@gmail. com wrote:
            [about using str* identifiers]
            You can if you don't include <string.h>
            >
            No.
            N869
            7.26 Future library directions
            [#1] The following names are grouped under individual
            headers for convenience. All external names described below
            are reserved no matter what headers are included by the
            program.
            >
            7.26.10 General utilities <stdlib.h>
            [#1] Function names that begin with str and a lowercase
            letter
            Ah, sorry for this. I remember reading what I've said in a previous
            discussion...
            I think it was you who said it? I don't remember very well.

            Comment

            • Harald van =?UTF-8?b?RMSzaw==?=

              #7
              Re: what does strdup return

              On Thu, 02 Oct 2008 07:06:06 -0700, vippstar wrote:
              On Oct 2, 3:14 pm, pete <pfil...@mindsp ring.comwrote:
              >vipps...@gmail .com wrote:
              [about using str* identifiers]
              You can if you don't include <string.h>
              >>
              >No.
              >N869
              > 7.26 Future library directions
              > [#1] The following names are grouped under individual
              > headers for convenience. All external names described below
              > are reserved no matter what headers are included by the
              > program.
              >>
              > 7.26.10 General utilities <stdlib.h[#1] Function names that
              > begin with str and a lowercase letter
              >
              Ah, sorry for this. I remember reading what I've said in a previous
              discussion...
              I think it was you who said it? I don't remember very well.
              You had it half right the first time. You can define str* functions, but
              you can't define str* external names. This is a correct program:

              /* no headers */
              static int strdup(void) { return 0; }
              int main(void) { return strdup(); }

              Comment

              • Antoninus Twink

                #8
                Re: what does strdup return

                On 2 Oct 2008 at 10:05, Nick Keighley wrote:
                On 2 Oct, 10:56, thomas <FreshTho...@gm ail.comwrote:
                >It's known that strdup() returns a char* type,
                >
                strdup() isn't mentioned in the C Standard and it's in a reserved
                namespace to boot; so you can't define it yourself.
                What makes you think he's defining it himself?

                strdup() *is* mentioned in the POSIX standard, and is declared in
                string.h. It returns a pointer to a new string which is a duplicate of
                the string s. Memory for the new string is obtained with malloc(), and
                can be freed with free().

                Comment

                • Peter Nilsson

                  #9
                  Re: what does strdup return

                  Nick Keighley <nick_keighley_ nos...@hotmail. comwrote:
                  thomas <FreshTho...@gm ail.comwrote:
                  It's known that strdup() returns a char* type,
                  >
                  strdup() isn't mentioned in the C Standard and it's
                  in a reserved namespace to boot;
                  It's precisely because it's in a reserved namespace that
                  implementations can (and many do) offer it as an extension.

                  --
                  Peter

                  Comment

                  • CBFalconer

                    #10
                    Re: what does strdup return

                    Peter Nilsson wrote:
                    Nick Keighley <nick_keighley_ nos...@hotmail. comwrote:
                    >thomas <FreshTho...@gm ail.comwrote:
                    >>
                    >>It's known that strdup() returns a char* type,
                    >>
                    >strdup() isn't mentioned in the C Standard and it's
                    >in a reserved namespace to boot;
                    >
                    It's precisely because it's in a reserved namespace that
                    implementations can (and many do) offer it as an extension.
                    See the C standard. Note the last sentence of #1.

                    7.26 Future library directions

                    [#1] The following names are grouped under individual
                    headers for convenience. All external names described below
                    are reserved no matter what headers are included by the
                    program.

                    ... snip ...

                    7.26.10 General utilities <stdlib.h>

                    [#1] Function names that begin with str and a lowercase
                    letter (possibly followed by any combination of digits,
                    letters, and underscore) may be added to the declarations in
                    the <stdlib.hheader .

                    7.26.11 String handling <string.h>

                    [#1] Function names that begin with str, mem, or wcs and a
                    lowercase letter (possibly followed by any combination of
                    digits, letters, and underscore) may be added to the
                    declarations in the <string.hheader .

                    --
                    [mail]: Chuck F (cbfalconer at maineline dot net)
                    [page]: <http://cbfalconer.home .att.net>
                    Try the download section.

                    Comment

                    • Default User

                      #11
                      Re: what does strdup return

                      thomas wrote:

                      If it's not included, the warning says that "don't try to assign an
                      integer to a pointer" something like that.
                      That would have been a very useful thing to include in the original
                      problem description.




                      Brian

                      Comment

                      Working...