C++ constructor help!!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KENNY LIU
    New Member
    • Feb 2011
    • 5

    C++ constructor help!!

    class description (MyString):

    class defines a sequence of characters (similar to class string). The implementation must be as an array of characters that are dynamically allocated and resized as necessary. You must use a null character to terminate the string - just like C-strings are null terminated.

    deafult constructor:

    Code:
    /// get_length(char *s) gets length of s before null character. buf is private data member to store array for MyString
    
    MyString::MyString(char *s)
    {
    buf = new char(get_length(s)+1); 
    
    for(int i=0;s;++i)
    buf[i] = s[i];
    
    buf[get_length(s)+1] = '\0';
    }
    Copy constructor


    Code:
    MyString::MyString(const MyString &s)
    {
    char *s_arr = s.buf;
    
    buf = new char(s.length()+1); ////length() gets length of MyString object before null
    
    for(int i=0;s_arr;++i)
    {
    buf[i] = s.buf[i];
    }
    
    buf[length()+1] = '\0';
    }

    I'm getting seg faults and errors when I test these constructors. DO you see any errors in my code? I can't figure it out.

    Thanks
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    yes, i see
    on line 10, on the first code block,
    on line 12, on second code block.

    how???
    Code:
    a[4]="joh";
    
    //now you are getting new str with  the same lenght
    b[]=new char(a.length()+1);
    //so, it has length 4. 
    //according to your code
    b[a.length()+1]='\0';
    
    //what is that mean? simple;
    //b[3+1], that is b[0],b[1],b[2],b[3],b[4].. whcih make the length 5, but you have only 4 byte lenght.. 
    //that is all

    Comment

    • KENNY LIU
      New Member
      • Feb 2011
      • 5

      #3
      I'm sorry, I'm not sure I understand your answer. So what am i doing wrong and how do i fix it

      Comment

      • johny10151981
        Top Contributor
        • Jan 2010
        • 1059

        #4
        you should use this.
        Code:
        b[a.length()]='\0';

        Comment

        • KENNY LIU
          New Member
          • Feb 2011
          • 5

          #5
          Like this???

          Code:
          MyString::MyString(char *s)
          {
          buf = new char(get_length(s)+1); 
           
          for(int i=0;s;++i)
          buf[i] = s[i];
           
          buf[get_length(s)] = '\0';
          }

          Comment

          • johny10151981
            Top Contributor
            • Jan 2010
            • 1059

            #6
            yes, and there is more
            your for condition is it limited at all??? it seems unlimited.

            your for loop should be like this
            Code:
            for(int i=0;s[i];++i) or for(int i=0;s[i]!=0;++i)
            or for(int i=0;s;++i,s++)

            Comment

            • morfanaion
              New Member
              • Oct 2011
              • 1

              #7
              ok, maybe I can point you to this line:

              buf = new char(get_length (s)+1);

              Here you are calling the constructor of a char and assigning it the value of get_length(s) + 1. To allocate an array, you need to use [ and ].

              buf = new char[get_length(s)+1];

              Comment

              Working...