include string?

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

    include string?

    Hello.

    I thought that strlen was only available after adding
    #include <string>
    to my c++ program, but apperently not, or the compiler is
    clever enough to find it by it self, or what?
    The program compiles and runs fine on my Linx box, with
    g++ version 2.95.4


    Second question. Wouldn't the method, Set, result in that the char* text
    will point to a newly allocated char array that is one char too short?
    strlen ignores the final nullchar, and then the final null char will be
    omitted in the copy procedure?

    Regards.
    ----------------------

    #include <iostream>
    class Problem {
    private:
    char* text;
    public:
    Problem() {text=new char[1]; text='\0';}
    void prt() {cout<<text;}
    void Set(char* str) {delete text; text=new char[strlen(str)];
    strcpy(text,str );}
    };
    int main() {
    Problem a;
    Problem b;
    a.Set("Gandalf" );
    a.Set("Bigger Longer Uncut text");
    a.prt();
    }

  • Ioannis Vranos

    #2
    Re: include string?

    "Gandalf" <gandalf@gunix. dk> wrote in message
    news:nO56b.2282 0$mU6.45172@new sb.telia.net...[color=blue]
    > Hello.
    >
    > I thought that strlen was only available after adding
    > #include <string>[/color]

    <cstring>

    [color=blue]
    > to my c++ program, but apperently not, or the compiler is
    > clever enough to find it by it self, or what?
    > The program compiles and runs fine on my Linx box, with
    > g++ version 2.95.4[/color]


    You were lucky.


    [color=blue]
    > Second question. Wouldn't the method, Set, result in that the char* text
    > will point to a newly allocated char array that is one char too short?
    > strlen ignores the final nullchar, and then the final null char will be
    > omitted in the copy procedure?
    >
    > Regards.
    > ----------------------
    >
    > #include <iostream>
    > class Problem {
    > private:
    > char* text;
    > public:
    > Problem() {text=new char[1]; text='\0';}[/color]


    text=new char;



    [color=blue]
    > void prt() {cout<<text;}
    > void Set(char* str) {delete text; text=new char[strlen(str)];[/color]


    text=new char[strlen(str)+1];






    --
    Ioannis

    * Programming pages: http://www.noicys.freeurl.com
    * Alternative URL 1: http://run.to/noicys
    * Alternative URL 2: http://www.noicys.cjb.net

    Comment

    • Christoph Rabel

      #3
      Re: include string?

      Ioannis Vranos wrote:[color=blue]
      > "Gandalf" <gandalf@gunix. dk> wrote in message
      > news:nO56b.2282 0$mU6.45172@new sb.telia.net...[/color]
      [color=blue][color=green]
      >>private:
      >> char* text;
      >>public:
      >> Problem() {text=new char[1]; text='\0';}[/color]
      >
      > text=new char;[/color]

      No. Dont mix new and new[]. Leave it as it is.
      [color=blue][color=green]
      >> void prt() {cout<<text;}
      >> void Set(char* str) {delete text; text=new char[strlen(str)];[/color]
      >
      > text=new char[strlen(str)+1];[/color]

      Yes. And even better:

      delete [] text; text=new char[strlen(str) + 1];


      Christoph

      Comment

      • Kevin Goodsell

        #4
        Re: include string?

        Gandalf wrote:[color=blue]
        > Hello.
        >
        > I thought that strlen was only available after adding
        > #include <string>[/color]

        strlen() has nothing to do with <string>. strlen() is declared in
        <string.h> or <cstring>. <string> is for the std::basic_stri ng template
        (from which std::string is created).
        [color=blue]
        > to my c++ program, but apperently not, or the compiler is
        > clever enough to find it by it self, or what?[/color]

        If you want to use a function like strlen, you should #include the
        appropriate header. Sometimes the correct header will be indirectly
        #included through a different header, but you can't count on this.
        [color=blue]
        > The program compiles and runs fine on my Linx box, with
        > g++ version 2.95.4
        >[/color]

        Old version. Why are you using it?
        [color=blue]
        >
        > Second question. Wouldn't the method, Set, result in that the char* text
        > will point to a newly allocated char array that is one char too short?
        > strlen ignores the final nullchar, and then the final null char will be
        > omitted in the copy procedure?[/color]

        I'm not sure I understand that paragraph.
        [color=blue]
        >
        > Regards.
        > ----------------------
        >
        > #include <iostream>[/color]

        You need <string.h> or <cstring> here.
        [color=blue]
        > class Problem {
        > private:
        > char* text;
        > public:
        > Problem() {text=new char[1]; text='\0';}[/color]

        This is not doing what you want it to do. First, you allocate a
        character, then you immediately overwrite the pointer with 0, thus
        leaking the allocated memory.
        [color=blue]
        > void prt() {cout<<text;}[/color]

        It's called 'std::cout'.
        [color=blue]
        > void Set(char* str) {delete text; text=new char[strlen(str)];
        > strcpy(text,str );}[/color]

        This is broken. You need another character to store the null terminator.
        Your destination string is not large enough for the strcpy. The result
        is undefined behavior - your program may crash, data may be corrupted,
        it could behave erratically... anything could happen.

        Also, you are deleting incorrectly. The memory 'text' points to was
        allocated via new[], therefore it must be deallocated using delete[].
        [color=blue]
        > };
        > int main() {
        > Problem a;
        > Problem b;[/color]

        Two memory leaks.
        [color=blue]
        > a.Set("Gandalf" );
        > a.Set("Bigger Longer Uncut text");[/color]

        Two cases of undefined behavior.
        [color=blue]
        > a.prt();
        > }
        >[/color]

        Other problems with your code:

        * No destructor means you leak memory when an instance of your class
        goes out of scope.

        * The default copy constructor and copy assignment operator will cause a
        new copy to have its 'text' pointer pointing to the exact same memory as
        the original. When that happens, then one of the copies deletes the
        memory 'text' pointed to, the other copy will point to memory that is no
        longer valid, and will have no way of knowing that it's no longer valid.
        Essentially anything you do with it at that point causes undefined behavior.

        You should have avoided all these problems by using std::string instead
        of C-style strings represented by char arrays.

        I recommend you read the FAQ a few more times. You've obviously missed
        several important points.



        -Kevin
        --
        My email address is valid, but changes periodically.
        To contact me please use the address from a recent posting.

        Comment

        • Ron Natalie

          #5
          Re: include string?


          "Christoph Rabel" <odie@hal9000.v c-graz.ac.at> wrote in message news:3f58f315$0 $29344[color=blue][color=green][color=darkred]
          > >>public:
          > >> Problem() {text=new char[1]; text='\0';}[/color]
          > >
          > > text=new char;[/color]
          >
          > No. Dont mix new and new[]. Leave it as it is.[/color]
          But the text = '\0'; is wrong. You're leaking memory
          *text = '\0'; most likely what you want.



          Comment

          • Gandalf

            #6
            Re: include string?

            >> >>public:[color=blue][color=green][color=darkred]
            >> >> Problem() {text=new char[1]; text='\0';}
            >> >
            >> > text=new char;[/color]
            >>
            >> No. Dont mix new and new[]. Leave it as it is.[/color]
            > But the text = '\0'; is wrong. You're leaking memory
            > *text = '\0'; most likely what you want.[/color]
            Oh, did I miss that too....dang.

            Comment

            Working...