<坏指针>超载+操作员坏指针>
当我在此构造函数中创建一个新的字符串数组时,调试器给我“错误的 ptr”,但只有当我的重载运算符方法创建一个新的 MyString 对象时......感到困惑。
这是我的构造函数
MyString::MyString()
{
stringSize = 0;
stringCap = 16;
stringArray = new char[stringCap + 1];
stringArray[0] = '\0';
}
这是我的重载运算符方法
MyString operator+(const char* leftOp, const MyString& rightOp)
{
MyString result; // new object used to store result
result.stringSize = strlen(leftOp) + rightOp.stringSize;
// if the string does not fit in the array
if( result.stringSize > result.stringCap )
{
delete[] result.stringArray;
result.stringCap = ( result.stringSize + 15 ) & ~15;
result.stringArray = new char[result.stringCap + 1];
}
strcpy(result.stringArray, leftOp);
strcat(result.stringArray, rightOp.stringArray);
return result;
}
这是我的复制构造函数,调试器也永远不会获取它
MyString::MyString(const MyString& s)
{
stringSize = s.stringSize;
stringCap = s.stringCap;
//stringArray[stringCap + 1];
stringArray = new char[stringCap + 1];
stringArray = s.stringArray;
}
The debugger is giving me 'bad ptr' when I create a new string array in this constructor, but only when my overloading operator method creates a new MyString object... confused.
Here is my constructor
MyString::MyString()
{
stringSize = 0;
stringCap = 16;
stringArray = new char[stringCap + 1];
stringArray[0] = '\0';
}
Here is my overloading operator method
MyString operator+(const char* leftOp, const MyString& rightOp)
{
MyString result; // new object used to store result
result.stringSize = strlen(leftOp) + rightOp.stringSize;
// if the string does not fit in the array
if( result.stringSize > result.stringCap )
{
delete[] result.stringArray;
result.stringCap = ( result.stringSize + 15 ) & ~15;
result.stringArray = new char[result.stringCap + 1];
}
strcpy(result.stringArray, leftOp);
strcat(result.stringArray, rightOp.stringArray);
return result;
}
Here is my copy constructor, which the debugger never gets too
MyString::MyString(const MyString& s)
{
stringSize = s.stringSize;
stringCap = s.stringCap;
//stringArray[stringCap + 1];
stringArray = new char[stringCap + 1];
stringArray = s.stringArray;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,当这个方法返回时,“结果”将被复制并破坏原始结果。如果析构函数删除了数组,并且没有智能复制构造函数来确保新副本包含有效的新数组,那么您就会遇到问题。
但是你说编译器说了一些关于坏指针的信息——在哪里?什么线?
Well, when this method returns, "result" is going to be copied and the original destructed. If the destructor deletes the array, and there isn't a smart copy constructor which ensures that the new copy includes a valid new array, then you're going to have problems.
But you said the compiler says something about a bad pointer -- where? What line?
由于从您的代码片段来看,似乎没有任何问题,我的第六感告诉我您没有编写复制构造函数,并且正在使用编译器生成的默认构造函数,或者可能
stringArray
不是空终止字符串!编辑:
在您的复制构造函数中,这是错误的:
使用
strcpy
代替:确保所有字符串都以 null 结尾!
Since from your code snippet, nothing seems wrong, my sixth sense tells me that you've NOT written copy-constructor, and are working with the default one generated by the compiler, or possibly
stringArray
is not a null-terminated string!EDIT:
In your copy-constructor, this is wrong:
Use
strcpy
instead:Make sure all your strings are null-terminated!