strdup in c++

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

    strdup in c++

    Hi there,

    I have a program written in c++ and I wish to use a similar function to
    strdup().
    The reason I have problems is that the char array requires freeing to avoid
    a memory leak, but I get problems using delete[];

    e.g.

    char *NewCharArray;

    NewCharArray = strdup("Some text");

    delete []NewCharArray;


    Nybody have any ideas?
    Thanks
    Allan


  • Ron Natalie

    #2
    Re: strdup in c++


    "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote in message news:bkcbc5$hr6 $1@news.freedom 2surf.net...[color=blue]
    > Hi there,
    >
    > I have a program written in c++ and I wish to use a similar function to
    > strdup().
    > The reason I have problems is that the char array requires freeing to avoid
    > a memory leak, but I get problems using delete[];
    >
    > e.g.
    >
    > char *NewCharArray;
    >
    > NewCharArray = strdup("Some text");
    >
    > delete []NewCharArray;
    >
    >
    > Nybody have any ideas?[/color]

    std::string NewString;

    NewString = "SomeText";

    // delete not required.


    Comment

    • Attila Feher

      #3
      Re: strdup in c++

      Allan Bruce wrote:[color=blue]
      > Hi there,
      >
      > I have a program written in c++ and I wish to use a similar function
      > to strdup().
      > The reason I have problems is that the char array requires freeing to
      > avoid a memory leak, but I get problems using delete[];
      >
      > e.g.
      >
      > char *NewCharArray;
      >
      > NewCharArray = strdup("Some text");
      >
      > delete []NewCharArray;
      >
      >
      > Nybody have any ideas?[/color]

      Nybody must be some Scandinavian guy :-)

      Use std::string instead of fiddling with those arrays.

      --
      Attila aka WW


      Comment

      • Buster

        #4
        Re: strdup in c++

        "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote
        [color=blue]
        > I have a program written in c++ and I wish to use a similar function to
        > strdup().
        > The reason I have problems is that the char array requires freeing to avoid
        > a memory leak, but I get problems using delete[];[/color]

        You have to use free:

        #include <cstdlib>
        #include <cstring>
        // ...
        char * NewCharArray = strdup ("Hi there");
        // std::strdup doesn't compile on my machine
        // (macro?). Good job I never use it.
        std::free (NewCharArray);

        But don't do that. Use the standard string class.

        Regards,
        Buster


        Comment

        • John Ericson

          #5
          Re: strdup in c++

          "Buster" <noone@nowhere. com> wrote in message
          news:bkcecp$9u3 $1@news7.svr.po l.co.uk...[color=blue]
          > "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote
          >[color=green]
          > > I have a program written in c++ and I wish to use a[/color][/color]
          similar function to[color=blue][color=green]
          > > strdup().
          > > The reason I have problems is that the char array[/color][/color]
          requires freeing to avoid[color=blue][color=green]
          > > a memory leak, but I get problems using delete[];[/color]
          >
          > You have to use free:[/color]

          <snip>

          <OT> strdup( ) isn't even a standard C or C++ function.
          Whether you should use free or delete[] depends on what the
          particular compiler vendor's strdup( ) used to allocate the
          memory </OT>


          Comment

          • Buster

            #6
            Re: strdup in c++

            "John Ericson" <jericson@pacbe ll.net> wrote[color=blue]
            > <OT> strdup( ) isn't even a standard C or C++ function.
            > Whether you should use free or delete[] depends on what the
            > particular compiler vendor's strdup( ) used to allocate the
            > memory </OT>[/color]

            I'm learning a lot today. Thanks.


            Comment

            • Ron Natalie

              #7
              Re: strdup in c++


              "Buster" <noone@nowhere. com> wrote in message news:bkcecp$9u3 $1@news7.svr.po l.co.uk...
              [color=blue]
              > // ...
              > char * NewCharArray = strdup ("Hi there");
              > // std::strdup doesn't compile on my machine
              > // (macro?). Good job I never use it.[/color]

              That's because there is no std::strdup. strdup
              is not a standard C or C++ function.


              Comment

              • Andre Kostur

                #8
                Re: strdup in c++

                "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote in
                news:bkcbc5$hr6 $1@news.freedom 2surf.net:
                [color=blue]
                > Hi there,
                >
                > I have a program written in c++ and I wish to use a similar function
                > to strdup().
                > The reason I have problems is that the char array requires freeing to
                > avoid a memory leak, but I get problems using delete[];
                >
                > e.g.
                >
                > char *NewCharArray;
                >
                > NewCharArray = strdup("Some text");
                >
                > delete []NewCharArray;
                >
                >
                > Nybody have any ideas?[/color]

                Two ideas:

                1) As others have mentioned, use std::string in preference (generally) to
                char*'s.

                2) Remember to always delete[] what you new[], and to free() what you
                malloc() (or calloc(), or strdup(), or other functions which are
                documented to requrire free()ing of the memory obtained). Under the
                covers, strdup() effectively calls malloc() (I don't know if this is
                strictly mandated). So, in order to correctly dispose of the memory
                obtained by strdup(), your code should be:

                char * NewCharArray = strdup("Some text");

                free(NewCharArr ay);

                (Depressingly, I had to look up whether free needed the parens, or does
                it get called like delete... It's been so long since I've used
                malloc/free.... :) )

                Comment

                • Gianni Mariani

                  #9
                  Re: strdup in c++

                  John Ericson wrote:[color=blue]
                  > "Buster" <noone@nowhere. com> wrote in message
                  > news:bkcecp$9u3 $1@news7.svr.po l.co.uk...
                  >[color=green]
                  >>"Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote
                  >>
                  >>[color=darkred]
                  >>>I have a program written in c++ and I wish to use a[/color][/color]
                  >
                  > similar function to
                  >[color=green][color=darkred]
                  >>>strdup().
                  >>>The reason I have problems is that the char array[/color][/color]
                  >
                  > requires freeing to avoid
                  >[color=green][color=darkred]
                  >>>a memory leak, but I get problems using delete[];[/color]
                  >>
                  >>You have to use free:[/color]
                  >
                  >
                  > <snip>
                  >
                  > <OT> strdup( ) isn't even a standard C or C++ function.
                  > Whether you should use free or delete[] depends on what the
                  > particular compiler vendor's strdup( ) used to allocate the
                  > memory </OT>[/color]

                  SMACK the supplier over the head if they don't use malloc/free.

                  Use free() to release memory allocated by strdup().

                  If you're using C++ you'd be far better off using std::string.

                  Comment

                  • Mike Wahler

                    #10
                    Re: strdup in c++


                    "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote in message
                    news:bkcbc5$hr6 $1@news.freedom 2surf.net...[color=blue]
                    > Hi there,
                    >
                    > I have a program written in c++ and I wish to use a similar function to
                    > strdup().[/color]

                    Note that 'strdup()' is not part of standard C++ (or C).
                    [color=blue]
                    > The reason I have problems is that the char array requires freeing to[/color]
                    avoid[color=blue]
                    > a memory leak, but I get problems using delete[];[/color]

                    IMO the reason you're having problems is that you're using
                    arrays of characters (with all their traps and pitfalls) and
                    dynamic allocation (with its possible problems) when you should
                    be using the 'std::string' type to represent strings.

                    std::string my_strdup(const std::string& s)
                    {
                    return s;
                    }

                    void foo()
                    {
                    std::string x(my_strdup("ab c"));
                    }

                    But doing this is silly, since the same effect can be
                    achieved with simpler (and probably more efficient)
                    code:

                    std::string s1("abc"); /* 'duplicates' "abc" in the string 's1' */

                    std::string s2(s1); /* 'duplicates' 's1' contents in 's2' */

                    s1 = s2; /* 'duplicates' 's2' contents in 's1' */

                    /* etc */
                    [color=blue]
                    > e.g.
                    >
                    > char *NewCharArray;[/color]

                    This name is misleading. 'NewCharArray' is not an array,
                    it's a pointer.
                    [color=blue]
                    >
                    > NewCharArray = strdup("Some text");[/color]

                    Why not simply:

                    char NewCharArray[] = {"Some text"};
                    [color=blue]
                    >
                    > delete []NewCharArray;[/color]

                    Then you don't need this.
                    [color=blue]
                    >
                    >
                    > Nybody have any ideas?[/color]

                    Yes, stop trying to use C++ to write C code. :-)

                    IF you need a string, the C++ library has a very
                    powerful and easy to use string type which is much
                    safer than arrays of characters and messing with
                    allocation and deallocation.

                    -Mike


                    Comment

                    • Mike Wahler

                      #11
                      Re: strdup in c++


                      "Andre Kostur" <nntpspam@kostu r.net> wrote in message
                      news:Xns93FA561 4EB469nntpspamk osturnet@209.53 .75.21...[color=blue]
                      > "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote in
                      > news:bkcbc5$hr6 $1@news.freedom 2surf.net:
                      >[color=green]
                      > > Hi there,
                      > >
                      > > I have a program written in c++ and I wish to use a similar function
                      > > to strdup().
                      > > The reason I have problems is that the char array requires freeing to
                      > > avoid a memory leak, but I get problems using delete[];
                      > >
                      > > e.g.
                      > >
                      > > char *NewCharArray;
                      > >
                      > > NewCharArray = strdup("Some text");
                      > >
                      > > delete []NewCharArray;
                      > >
                      > >
                      > > Nybody have any ideas?[/color]
                      >
                      > Two ideas:
                      >
                      > 1) As others have mentioned, use std::string in preference (generally) to
                      > char*'s.
                      >
                      > 2) Remember to always delete[] what you new[], and to free() what you
                      > malloc() (or calloc(), or strdup(), or other functions which are
                      > documented to requrire free()ing of the memory obtained). Under the
                      > covers, strdup() effectively calls malloc() (I don't know if this is
                      > strictly mandated).[/color]

                      Of course not, since it's not a standard function. One should
                      follow its documentation for correct usage. This 'correct usage'
                      could be different among implementations .
                      [color=blue]
                      >So, in order to correctly dispose of the memory
                      > obtained by strdup(), your code should be:
                      >
                      > char * NewCharArray = strdup("Some text");
                      >
                      > free(NewCharArr ay);[/color]

                      Only if the particular implementation of 'strdup()' being
                      used documents to do it that way.
                      [color=blue]
                      >
                      > (Depressingly, I had to look up whether free needed the parens, or does
                      > it get called like delete... It's been so long since I've used
                      > malloc/free.... :) )[/color]

                      malloc and free are functions, so argument list must always
                      be enclosed in parens:

                      p = malloc(whatever );
                      free(p);

                      new, new[], delete, and delete[] are operators, and do
                      not need parens.

                      p = new int;
                      delete p;

                      -Mike


                      Comment

                      • Ron Natalie

                        #12
                        Re: strdup in c++


                        "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message news:bkcjbe$p2h @dispatch.conce ntric.net...
                        [color=blue][color=green]
                        > > <OT> strdup( ) isn't even a standard C or C++ function.
                        > > Whether you should use free or delete[] depends on what the
                        > > particular compiler vendor's strdup( ) used to allocate the
                        > > memory </OT>[/color]
                        >
                        > SMACK the supplier over the head if they don't use malloc/free.[/color]

                        They have to supply malloc / free. There's no telling what the
                        function strdup() requires because there's no such function in
                        standard C or C++.



                        Comment

                        • Andre Kostur

                          #13
                          Re: strdup in c++

                          "Mike Wahler" <mkwahler@mkwah ler.net> wrote in news:Dgmab.7671 $UN4.3734
                          @newsread3.news .pas.earthlink. net:
                          [color=blue]
                          >
                          > "Allan Bruce" <allanmb@TAKEAW AYf2s.com> wrote in message
                          > news:bkcbc5$hr6 $1@news.freedom 2surf.net...[color=green]
                          >> Hi there,
                          >>
                          >> I have a program written in c++ and I wish to use a similar function to
                          >> strdup().[/color]
                          >
                          > Note that 'strdup()' is not part of standard C++ (or C).[/color]

                          Huh. Hadn't realized that. Only that every implementation I've used has
                          had a strdup.... oh well. Answer rephrase: check the documentation on your
                          particular implementation of strdup(). All of the implementations I've
                          used, the memory returned by strdup() needs to be free()'d. YMMV

                          Comment

                          • Gianni Mariani

                            #14
                            Re: strdup in c++

                            Ron Natalie wrote:[color=blue]
                            > "Gianni Mariani" <gi2nospam@mari ani.ws> wrote in message news:bkcjbe$p2h @dispatch.conce ntric.net...
                            >
                            >[color=green][color=darkred]
                            >>><OT> strdup( ) isn't even a standard C or C++ function.
                            >>>Whether you should use free or delete[] depends on what the
                            >>>particular compiler vendor's strdup( ) used to allocate the
                            >>>memory </OT>[/color]
                            >>
                            >>SMACK the supplier over the head if they don't use malloc/free.[/color]
                            >
                            >
                            > They have to supply malloc / free. There's no telling what the
                            > function strdup() requires because there's no such function in
                            > standard C or C++.[/color]

                            Sure there is. It's in every man page for strdup.

                            Comment

                            • White Wolf

                              #15
                              Re: strdup in c++

                              Gianni Mariani wrote:[color=blue][color=green]
                              >> They have to supply malloc / free. There's no telling what the
                              >> function strdup() requires because there's no such function in
                              >> standard C or C++.[/color]
                              >
                              > Sure there is. It's in every man page for strdup.[/color]

                              Man pages do not define the C or the C++ language. The international
                              standards do. And they do not contain strdup. So there is no strdup in
                              standard C or C++.

                              --
                              WW aka Attila


                              Comment

                              Working...