重载运算符 = 中的分段错误
我刚刚在重载类 FeatureRandomCounts 的赋值运算符时遇到了 seg 错误,该类的指针成员是 _rects,指向一个包含 FeatureCount 和大小 rhs._dim 的数组,而其他日期成员是非指针:
FeatureRandomCounts & FeatureRandomCounts::operator=(const FeatureRandomCounts &rhs)
{
if (_rects) delete [] _rects;
*this = rhs; // segment fault
_rects = new FeatureCount [rhs._dim];
for (int i = 0; i < rhs._dim; i++)
{
_rects[i]=rhs._rects[i];
}
return *this;
}
有人有一些线索吗? ?谢谢和问候!
I just got a seg fault in overloading the assignment operator for a class FeatureRandomCounts, which has _rects as its pointer member pointing to an array of FeatureCount and size rhs._dim, and whose other date members are non-pointers:
FeatureRandomCounts & FeatureRandomCounts::operator=(const FeatureRandomCounts &rhs)
{
if (_rects) delete [] _rects;
*this = rhs; // segment fault
_rects = new FeatureCount [rhs._dim];
for (int i = 0; i < rhs._dim; i++)
{
_rects[i]=rhs._rects[i];
}
return *this;
}
Does someone have some clue? Thanks and regards!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
调用operator=(),这是您正在编写的函数。提示无限递归、堆栈溢出、崩溃。
另外,如果您使用 std::vector 而不是 C 样式数组,您可能根本不需要实现operator=()。
calls operator=(), which is the function you are writing. Cue infinite recursion, stack overflow, crash.
Also, if you used a std::vector rather than a C-style array, you probably would not need to implement operator=() at all.
如前所述,您有无限递归;然而,除此之外,这里有一个万无一失的方法来实现 op=:
编写正确的复制构造函数和交换,并且异常安全和所有边缘情况都会为您处理!
copy 参数按值传递,然后进行更改。当copy被销毁时,当前实例必须销毁的任何资源都会被处理。这遵循当前建议并处理干净地自我分配。
请注意,它适用于手动管理的成员 (p) 或 RAII/SBRM 样式的成员 (s)。
As mentioned, you have infinite recursion; however, to add to that, here's a foolproof way to implement op=:
Write a correct copy ctor and swap, and exception safety and all edge cases are handled for you!
The copy parameter is passed by value and then changed. Any resources which the current instance must destroy are handled when copy is destroyed. This follows current recommendations and handles self-assignment cleanly.
Notice it works with either manually managed members (p) or RAII/SBRM-style members (s).
这绝对不是这样做的方法。您递归地调用
=
,而不是调用内置的赋值运算符。将变量一一赋值。不要偷懒。This is definitively not the way to do it. You call
=
recursively, not calling the built in assignment operator. Assign variables one by one. Don't be lazy.以下行:
将递归调用
operator=()
函数,导致堆栈溢出。您可能应该用各种成员字段的直接分配来替换它。
正如尼尔所说,使用类似
std::vector< ;>
将从您的代码中消除大部分责任。如果出于某种原因您不能或不想使用std::vector
,您可能还需要考虑对赋值运算符使用“交换习惯用法”。这将使函数异常安全(如果为FeatureCount
数组分配内存失败并引发异常,则分配给的原始对象将保持不变)。如下所示:现在您的作业可能如下所示:
请注意,您需要一个适当的复制构造函数才能使其工作,但考虑到 三大规则 你已经需要一个合适的复制者了。
The following line:
will recursively call your
operator=()
function resulting in a stack overflow.You should probably replace it with straight-forward assignments of the various member fields.
As Neil said, using something like
std::vector<>
will remove much of the responsibility away from your code. If for whatever reason you can't or don't want to usestd::vector<>
, you might also want to consider using the 'swap idiom' for your assignment operator. This will make the function exception safe (if the allocation of the memory forFeatureCount
array fails and throws an exception, the original object that's being assigned to will be left unchanged). Something like the following:Now your assignment can look like:
Note that you need a proper copy constructor for this to work, but given the Big 3 rule you already need a proper copy ctor.