0

I have invalid read of size in the following functions using valgrind. I'm not exactly sure why but if any of you can help me that would be greatly appreciated! From what I can tell it runs okay but there are still some errors that I'm not catching that may even deal with memory allocation and deallocation. Please help!

 //alternate constructor that allows for setting of the inital value of the string
 MyString::MyString(const char *message)
 {
    int counter(0);
    while(message[counter] != '\0')
    {
            counter++;
    }
    Size = counter;
    **String = new char [Size];**
    for(int i=0; i < Size; i++)
            String[i] = message[i];

 }


istream& operator>>(istream& input, MyString& rhs)
{
    char* t;
    int size(256);
    t = new char[size];
    input.getline(t,size);

    **rhs = MyString(t);**
    delete [] t;

    return input;
}



 /*Assignment operator (=) which will copy the source string into the destination string. Note that size of the destination needs to be adjusted to be the same as the source.
 */

  MyString& MyString::operator=(const MyString& rhs)
 {
    if(this != &rhs)
    {
            delete [] String;
            **String = new char[rhs.Size+1];**
            Size = rhs.Size;

            for(int i = 0; i < Size; i++)
            {
                   ** String[i] = rhs.String[i];**
            }
    }

    return *this;
 }

Any suggestions?? (All of the problem lines have **)

1
  • I should've posted that info, I will edit the question with the specific lines highlighted in some way. Sorry! Jul 17, 2012 at 15:29

1 Answer 1

0

One thing I see is that your copy constructor doesn't allocate space for \0 and doesn't copy it. Neither does the assignment operator.. Or, if you don't store terminating zero, then why are you looking for it?

and the two implementations differ, why the inconsistency (Size vs counter)?

"From what I can tell it runs okay" - it's called undefined behaviour, or in this case: luck - or, if you like me, and like to catch bugs: a misfortune.

5
  • thank you very much! I tested the copy constructor to see if it had copied the \0 and it displayed it did. The string copied should be Hello World which would have 10 characters and obtain the 11 index to be for the nul character. I tested that using cout<<String[11]<<endl; and it printed a blank. When I used String[12] it gave me a random character so I was guessing that it did copy over the nul. With that, how can I be certain it copied over? Jul 17, 2012 at 15:46
  • How exactly do they differ by the way? I thought I was making it to where size equals counter? I think I may have gotten ahead of myself while writing the code. Jul 17, 2012 at 15:54
  • it doesn't copy it, do a proper test. Jul 17, 2012 at 16:43
  • I just tested using: if(String[11] != '\0') { cout<<"The nul was not copied"<<endl; } else { cout<<"The nul was copied"<<endl; } and it returned that the nul was copied. Is there any other way to do it? Jul 17, 2012 at 17:06
  • I'm sorry that was in my copy constructor, you are absolutely right the nul isn't copied in my assignment operator. Jul 17, 2012 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.