Thread: String Class

  1. #76
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    >> String::String(char letter, int repeat) : name(0), stringlength(repeat)
    That constructor isn't quite right, but from the looks of it you added it recently and with a different style than your other ones.

    The other constructors have solved the initialization problem I mentioned before, except now one of your operator= does the same bad thing of not setting all values to valid states.

    Look at how your operator+= sets stringlength at the end of the function. It's not quite right.

    Your operator=(char) has a memory leak. Remember that you need to clean up existing memory inside operator=. Also check your setName function for the same problem.

    I assume you haven't used your operator[] functions yet. You'll see the problem when you do.


    Do you have test code? You should write a small test app that tests one function at a time. Start with a string, run one of the functions, and check the values inside the string to make sure they are accurate. do this for every function separately, and then they should work better when you combine them.
    You are right. I made the String(char letter, int repeat) constructor but I changed some of it according to the suggestions of another user and it works better now.

  2. #77
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    >> String::String(char letter, int repeat) : name(0), stringlength(repeat)
    That constructor isn't quite right, but from the looks of it you added it recently and with a different style than your other ones.

    The other constructors have solved the initialization problem I mentioned before, except now one of your operator= does the same bad thing of not setting all values to valid states.

    Look at how your operator+= sets stringlength at the end of the function. It's not quite right.

    Your operator=(char) has a memory leak. Remember that you need to clean up existing memory inside operator=. Also check your setName function for the same problem.

    I assume you haven't used your operator[] functions yet. You'll see the problem when you do.


    Do you have test code? You should write a small test app that tests one function at a time. Start with a string, run one of the functions, and check the values inside the string to make sure they are accurate. do this for every function separately, and then they should work better when you combine them.
    whats wrong with my []operators?

  3. #78
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    I'm sure you'll find out as soon as you use them. It's rather obvious.

    You should make the first one return a const char& or a char since the function is const and you shouldn't be providing non-const access to the member data. But that's not the obvious thing that is wrong with them.

  4. #79
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by Daved View Post
    I'm sure you'll find out as soon as you use them. It's rather obvious.

    You should make the first one return a const char& or a char since the function is const and you shouldn't be providing non-const access to the member data. But that's not the obvious thing that is wrong with them.
    I changed them and I think they are okay now:

    Code:
    char& String::operator [](int i) const 
    {
    	if(i >= 0 && i < stringlength)
    		return sink;
    	else
    		return buf[i];
    }
    
    char& String::operator [](int i)
    {
    	if(i >= 0 && i < stringlength)
    		return sink;
    	else
    		return buf[i];
    }

  5. #80
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if i is -1 you will return buf[-1] - out of bounds access
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #81
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    if i is -1 you will return buf[-1] - out of bounds access
    so, should it be this:

    Code:
    char& String::operator [](int i) const 
    {
    	if(i <= 0 && i < stringlength)
    		return sink;
    	else
    		return buf[i];
    }
    
    char& String::operator [](int i)
    {
    	if(i <= 0 && i < stringlength)
    		return sink;
    	else
    		return buf[i];
    }

  7. #82
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in this case using i == stringlength will give you the out of bound access
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #83
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    in this case using i == stringlength will give you the out of bound access
    I don't understand.

  9. #84
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by BKurosawa View Post
    I don't understand.
    What exactly you do not understand?

    if i is stringlength than
    i<=0 is false, the if is false and you go to the else getting buf[stringlength]
    this is out of bounds access
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #85
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    What exactly you do not understand?

    if i is stringlength than
    i<=0 is false, the if is false and you go to the else getting buf[stringlength]
    this is out of bounds access
    oh, so you are saying replace what I have in the if() with i == stringlength.

  11. #86
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    How do I make the print function use printf() to output the name of the string, the string literal, and the size of the string literal with the three all displayed on one line per string object?

  12. #87
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What is string name?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #88
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    What is string name?
    Like if I make a string:

    String s1;

    and then I do this:

    s1.setName("s1");

    Then I should be able to show the name of the object which in this case is s1.

  14. #89
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    like
    Code:
    printf("&#37;s: %s(%d)\n", name, buf, stringlength);
    ?

    But why do you need a printf if you trying to write in C++?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #90
    Registered User
    Join Date
    Jul 2007
    Posts
    109
    Quote Originally Posted by vart View Post
    like
    Code:
    printf("&#37;s: %s(%d)\n", name, buf, stringlength);
    ?

    But why do you need a printf if you trying to write in C++?
    isn't printf used in C++?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. class object manipulation
    By guda in forum C++ Programming
    Replies: 2
    Last Post: 10-09-2004, 10:43 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM