我在使用valgrind的以下函數中讀取的大小無效。我不完全確定爲什麼,但如果你們中的任何人都可以幫助我,那將不勝感激!從我可以告訴它運行良好,但仍然有一些錯誤,我不捕捉,甚至可能處理內存分配和釋放。請幫忙!在valgrind中無效讀取和寫入大小
//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;
}
有什麼建議? (所有的問題行都有**)
在哪條線上? – 2012-07-17 15:23:17
我應該發佈該信息,我將用某種方式突出顯示特定行的問題進行編輯。抱歉! – user1363061 2012-07-17 15:29:32