重载算子中的分段错误= [英] segmentation fault in overloading operator =

查看:119
本文介绍了重载算子中的分段错误=的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚得到一个seg故障重载的类FeatureRandomCounts的赋值运算符,它有指向FeatureCount和大小rhs._dim的数组的指针成员,其其他日期成员是非指针: p>

  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;
}

有人有一些线索吗?

解决方案

由于男性 < a href =#1996562 =nofollow> tion ed ,you have infinite recursion;然而,为了补充,这里是一个愚蠢的方式来实现op =:

  struct T {
T const& other);
T& operator =(T copy){
swap(* this,copy);
return * this;
}
friend void swap(T& a,T& b);
};

写一个正确的拷贝ctor和交换,异常安全和所有边缘情况为你处理! / p>

参数传递值,然后更改。任何当前实例必须销毁的资源都会在销毁 时处理。这遵循当前推荐,并处理自行分配






  #include< algorithm> 
#include< iostream>

struct ConcreteExample {
int * p;
std :: string s;

ConcreteExample(int n,char const * s):p(new int(n)),s(s){}
ConcreteExample(ConcreteExample const& other)
: p(new int(* other.p)),s(other.s){}
〜ConcreteExample(){delete p; }

ConcreteExample& operator =(ConcreteExample copy){
swap(* this,copy);
return * this;
}

friend void swap(ConcreteExample& a,ConcreteExample& b){
using std :: swap;
// using boost :: swap; // if available
swap(a.p,b.p); //使用ADL(当p有不同的类型时),整个原因
swap(a.s,b.s); // this'method'does not really a member(所以它可以使用
//以同样的方式)
}
};

int main(){
ConcreteExample a(3,a),b(5,b);
std :: cout<< a.s<< * a.p < ''<< b.s < * b.p << '\\\
';
a = b;
std :: cout<< a.s<< * a.p < ''<< b.s < * b.p << '\\\
';
return 0;
}

注意它适用于手动管理的成员( p )或RAII / SBRM样式成员( )。


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!

解决方案

As mentioned, you have infinite recursion; however, to add to that, here's a foolproof way to implement op=:

struct T {
  T(T const& other);
  T& operator=(T copy) {
    swap(*this, copy);
    return *this;
  }
  friend void swap(T& a, T& b);
};

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.


#include <algorithm>
#include <iostream>

struct ConcreteExample {
  int* p;
  std::string s;

  ConcreteExample(int n, char const* s) : p(new int(n)), s(s) {}
  ConcreteExample(ConcreteExample const& other)
  : p(new int(*other.p)), s(other.s) {}
  ~ConcreteExample() { delete p; }

  ConcreteExample& operator=(ConcreteExample copy) {
    swap(*this, copy);
    return *this;
  }

  friend void swap(ConcreteExample& a, ConcreteExample& b) {
    using std::swap;
    //using boost::swap; // if available
    swap(a.p, b.p); // uses ADL (when p has a different type), the whole reason
    swap(a.s, b.s); // this 'method' is not really a member (so it can be used
                    // the same way)
  }
};

int main() {
  ConcreteExample a (3, "a"), b (5, "b");
  std::cout << a.s << *a.p << ' ' << b.s << *b.p << '\n';
  a = b;
  std::cout << a.s << *a.p << ' ' << b.s << *b.p << '\n';
  return 0;
}

Notice it works with either manually managed members (p) or RAII/SBRM-style members (s).

这篇关于重载算子中的分段错误=的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆