【发布时间】:2012-07-17 15:02:39
【问题描述】:
我在使用 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;
}
有什么建议吗?? (所有问题行都有**)
【问题讨论】:
-
我应该发布该信息,我将编辑问题,并以某种方式突出显示特定行。对不起!
标签: string oop function error-handling size