Return C-style string from function?

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

    Return C-style string from function?

    #include <iostream>

    using namespace std;

    const char* test(char *src) {

    string s(src);

    return s.c_str();
    }

    int main() {

    char src[] = "test123";

    cout<<test(src) ;

    return 0;
    };

    how to get the "test123" from the function?

    thanks.

  • Papastefanos Serafeim

    #2
    Re: Return C-style string from function?


    "howa" <howachen@gmail .comwrote in message
    news:1161613411 .186962.87780@b 28g2000cwb.goog legroups.com...
    #include <iostream>
    >
    using namespace std;
    >
    const char* test(char *src) {
    >
    string s(src);
    >
    return s.c_str();
    }
    >
    int main() {
    >
    char src[] = "test123";
    >
    cout<<test(src) ;
    >
    return 0;
    };
    >
    how to get the "test123" from the function?
    >
    thanks.
    >
    When the function finishes the string s goes out of scope,
    and, since its in the stack, it becomes garbage.

    A quick fix is to change the line
    return s.c_str();
    to
    return strdup(s.c_str( ) );

    Serafeim


    Comment

    • Daniel T.

      #3
      Re: Return C-style string from function?

      In article <1161613411.186 962.87780@b28g2 000cwb.googlegr oups.com>,
      "howa" <howachen@gmail .comwrote:
      #include <iostream>
      >
      using namespace std;
      >
      const char* test(char *src) {
      >
      string s(src);
      >
      return s.c_str();
      }
      >
      int main() {
      >
      char src[] = "test123";
      >
      cout<<test(src) ;
      >
      return 0;
      };
      >
      how to get the "test123" from the function?
      >
      thanks.
      const char* test( char* src ) {
      return src;
      }

      --
      To send me email, put "sheltie" in the subject.

      Comment

      • howa

        #4
        Re: Return C-style string from function?


        Papastefanos Serafeim 寫道:
        "howa" <howachen@gmail .comwrote in message
        news:1161613411 .186962.87780@b 28g2000cwb.goog legroups.com...
        #include <iostream>

        using namespace std;

        const char* test(char *src) {

        string s(src);

        return s.c_str();
        }

        int main() {

        char src[] = "test123";

        cout<<test(src) ;

        return 0;
        };

        how to get the "test123" from the function?

        thanks.
        >
        When the function finishes the string s goes out of scope,
        and, since its in the stack, it becomes garbage.
        >
        A quick fix is to change the line
        return s.c_str();
        to
        return strdup(s.c_str( ) );
        >
        Serafeim
        Thanks...

        is this a usual/normal way to pass a string out of a function?

        Comment

        • Daniel T.

          #5
          Re: Return C-style string from function?

          "howa" <howachen@gmail .comwrote:
          Papastefanos Serafeim wrote:
          >"howa" <howachen@gmail .comwrote:
          >>#include <iostream>
          >>>
          >>using namespace std;
          >>>
          >>const char* test(char *src) {
          >> string s(src);
          >> return s.c str();
          >>}
          >>>
          >>int main() {
          >> char src[] = "test123";
          >> cout<<test(src) ;
          >> return 0;
          >>}
          >>>
          >>how to get the "test123" from the function?
          >>>
          >>thanks.
          >>>
          >>
          >When the function finishes the string s goes out of scope,
          >and, since its in the stack, it becomes garbage.
          >>
          >A quick fix is to change the line
          >return s.c str();
          >to
          >return strdup(s.c str() );
          >>
          >Serafeim
          >
          Thanks...
          >
          is this a usual/normal way to pass a string out of a function?
          No. strdup isn't even defined in the language. The usual way to do it is
          by returning a string that was passed in. See: strcpy for an example.

          --
          To send me email, put "sheltie" in the subject.

          Comment

          • loufoque

            #6
            Re: Return C-style string from function?

            howa wrote:
            is this a usual/normal way to pass a string out of a function?
            No, this is just a way to stop using RAII, hence creating non
            exception-safe code and a memory leak in various situations.
            This also has many other obvious problems related to pointers.

            Comment

            • Frederick Gotham

              #7
              Re: Return C-style string from function?

              howa posted:
              #include <iostream>
              >
              using namespace std;
              >
              const char* test(char *src) {
              >
              string s(src);
              >
              return s.c_str();
              }
              >
              int main() {
              >
              char src[] = "test123";
              >
              cout<<test(src) ;
              >
              return 0;
              };
              >
              how to get the "test123" from the function?

              The memory which the string occupies must still be accessible after the
              funciton returns. Therefore, either:

              (1) Have the calling function allocate the memory, and pass a pointer so
              that the called function will alter the memory.

              (2) Have the called function allocate the memory dynamically, and return a
              pointer which the calling function shall have to delete.

              (3) Use global/static data...

              --

              Frederick Gotham

              Comment

              • Martin Steen

                #8
                Re: Return C-style string from function?

                howa wrote:
                #include <iostream>
                >
                using namespace std;
                >
                const char* test(char *src) {
                >
                string s(src);
                >
                return s.c_str();
                }
                >
                int main() {
                >
                char src[] = "test123";
                >
                cout<<test(src) ;
                >
                return 0;
                };
                >
                how to get the "test123" from the function?
                >
                thanks.
                const char* test(char *src)
                {
                return src;
                }

                OR

                const char* test(char *src)
                {
                static string s(src);
                return s.c_str();
                }

                OR

                const char* test(char *src)
                {
                string* s = new string;
                *s = src;
                return s->c_str();
                }

                Best regards,
                -Martin

                Comment

                • Martin Steen

                  #9
                  Re: Return C-style string from function?

                  const char* test(char *src)
                  {
                  string* s = new string;
                  *s = src;
                  return s->c_str();
                  }
                  I forgot to tell you: that's not good,
                  because I create a memory leak here.
                  String s cannot be deleted anywhere.

                  -Martin



                  Comment

                  • Jim Langston

                    #10
                    Re: Return C-style string from function?

                    "howa" <howachen@gmail .comwrote in message
                    news:1161613411 .186962.87780@b 28g2000cwb.goog legroups.com...
                    #include <iostream>
                    >
                    using namespace std;
                    >
                    const char* test(char *src) {
                    >
                    string s(src);
                    >
                    return s.c_str();
                    }
                    >
                    int main() {
                    >
                    char src[] = "test123";
                    >
                    cout<<test(src) ;
                    >
                    return 0;
                    };
                    >
                    how to get the "test123" from the function?
                    >
                    thanks.
                    You're going to have problems no matter what method you decide to return a
                    c-style string. The best solution is to return a std::string. That's why
                    std::strings were invented, to get rid of this problem. What can returning
                    a c-style string get you that returning a std::string can't?


                    Comment

                    • Daniel T.

                      #11
                      Re: Return C-style string from function?

                      In article <453cd2a7$1_2@n ews.arcor-ip.de>,
                      Martin Steen <a0.spam001.10. msteen@spamgour met.comwrote:
                      howa wrote:
                      #include <iostream>

                      using namespace std;

                      const char* test(char *src) {

                      string s(src);

                      return s.c_str();
                      }

                      int main() {

                      char src[] = "test123";

                      cout<<test(src) ;

                      return 0;
                      };

                      how to get the "test123" from the function?

                      thanks.
                      >
                      const char* test(char *src)
                      {
                      return src;
                      }
                      >
                      OR
                      >
                      const char* test(char *src)
                      {
                      static string s(src);
                      return s.c_str();
                      }
                      >
                      OR
                      >
                      const char* test(char *src)
                      {
                      string* s = new string;
                      *s = src;
                      return s->c_str();
                      }
                      Note, that last one leaks memory every time it is called.

                      --
                      To send me email, put "sheltie" in the subject.

                      Comment

                      Working...