分割故障;三法则

Segmentation fault; rule of three

提问人:Reza Nishat 提问时间:3/18/2015 最后编辑:Reza Nishat 更新时间:3/19/2015 访问量:118

问:

我有一个这样的头文件

#ifndef __coulomb_h_
#define __coulomb_h_
#include "nml_dcvector.h"
#include <fstream>
#include <iostream>
#include <complex>

#include <vector>
using namespace std;

class Coulomb{

public:

 typedef complex<double> Complex_t;


 Coulomb(int na, int g1, int g2, int o)
  : numAtoms(na), g_min(g1), g_max(g2), o_max(o)
  {
   o_total=o_max*o_max;

   _H_coul = new complex<double>[1];
   _H_exch = new complex<double>[1];
  }



  ~Coulomb() 
  {delete [] _H_coul; delete [] _H_exch;}

private:

  complex<double>* _H_coul;
  complex<double>* _H_exch;

然后在我的.cpp文件中写下这一行

  int main(int argc, char** argv) {

   int rank=0, size=1;
 #if (defined MPI3d && !defined FAKE_MPI)
 MPI_Init(&argc, &argv);
 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
 MPI_Comm_size(MPI_COMM_WORLD, &size);
  #endif
char data[123];
int NumElectrons = 0;
int NumHoles = 0;
int NumAtoms = 0;
int NumOrbitals = 0;
int* eList;
int* hList;
..............
..............
int bat= NumAtoms/size;
int residue = NumAtoms%size;
int myAtoms_begin, myAtoms_end;
 if(rank<residue){
 myAtoms_begin = rank*(bat+1);
 myAtoms_end   = myAtoms_begin + bat + 1;
 }
 else {
 myAtoms_begin = residue*(bat+1) + (rank - residue)*bat;
 myAtoms_end   = myAtoms_begin + bat;
 }

  if(ComputeCoulomb) {

  Coulomb data_coulomb = 
  Coulomb(NumAtoms, myAtoms_begin, myAtoms_end, NumOrbitals);

这是给分割错误。我想这是由于三法则.然后我添加了这部分;Copy 构造函数和 Assignment 构造函数

Coulomb (const Coulomb& v):numAtoms(v.numAtoms), g_min(v.g_min),          
 g_max(v.g_max), o_max(v.o_max) 
{
 o_total= v.o_total;

_H_coul = new complex<double>[1];
_H_exch = new complex<double>[1];
 }

 Coulomb& operator= (const Coulomb& v)
  {
  int o_total= v.o_total;

  complex<double>* _H_coul_temp = new complex<double>[1];
  std::copy(v._H_coul,v._H_coul+1, _H_coul_temp  ) ;

  complex<double>* _H_exch_temp = new complex<double>[1];
  std::copy(v._H_exch,v._H_exch+1, _H_exch_temp  );

  delete [] _H_coul; delete [] _H_exch;

  _H_coul= _H_coul_temp;
  _H_exch= _H_exch_temp;

  return *this; 
}

仍然不工作。请任何人帮助我解决这个问题。谢谢

C++ 分段-故障 三法则

评论

4赞 Beta 3/18/2015
源文件中的那一行不是有效的C++,我怀疑它真的在那里。如果您给我们一个最小的完整示例,我们也许可以为您提供帮助。
4赞 David Rodríguez - dribeas 3/18/2015
为什么要动态分配 1 的数组?如果你不这样做,你的生活会简单得多。complex<double>
1赞 user3528438 3/18/2015
很难说没有看到你的破坏者。
0赞 Reza Nishat 3/18/2015
我已经编辑过了,现在你可以看到实际存在的内容......如果您还需要更多信息,请随时询问。我也使用了析构函数。我认为我的复制构造函数和复制分配不正确,因为我是自己写的,我没有那么多想法

答:

-1赞 Md Kauser Ahmmed 3/18/2015 #1

尝试像使用 一样使用析构函数。new

public:

    ~Coulomb()
    {
       delete _H_coul;
       delete _H_exch;
    }
0赞 Antu 3/19/2015 #2
#include <iostream>
#include <string>
//#include "nml_dcvector.h"
#include <fstream>
#include <complex>

#include <vector>
using namespace std;

class Coulomb {

public:

    int numAtoms, g_min, g_max, o_max, o_total;
    typedef complex<double> Complex_t;

    Coulomb(int na, int g1, int g2, int o) :
            numAtoms(na), g_min(g1), g_max(g2), o_max(o) {
        o_total = o_max * o_max;

        cout << "Inside normal constructor" << endl;
        _H_coul = new complex<double> [1];
        _H_exch = new complex<double> [1];
    }

    Coulomb(const Coulomb& v) :
            numAtoms(v.numAtoms), g_min(v.g_min), g_max(v.g_max), o_max(v.o_max) {
        o_total = v.o_total;

        cout << "Inside copy constructor" << endl;
        _H_coul = new complex<double> [1];
        _H_exch = new complex<double> [1];
    }

    Coulomb& operator=(const Coulomb& v) {
        o_total = v.o_total;

        complex<double>* _H_coul_temp = new complex<double> [1];
        std::copy(v._H_coul, v._H_coul + 1, _H_coul_temp);

        complex<double>* _H_exch_temp = new complex<double> [1];
        std::copy(v._H_exch, v._H_exch + 1, _H_exch_temp);

        delete[] _H_coul;
        delete[] _H_exch;
        cout << "Inside assignment overloader" << endl;
        _H_coul = _H_coul_temp;
        _H_exch = _H_exch_temp;

        return *this;
    }

    ~Coulomb() {
        delete[] _H_coul;
        delete[] _H_exch;
    }

private:

    complex<double>* _H_coul;
    complex<double>* _H_exch;
};

int main() {

    Coulomb data_coulomb = Coulomb(1000, 5, 500, 20);

    Coulomb* data_coulomb_2 = new Coulomb(1000, 5, 500, 0);

    delete data_coulomb_2;
    std::cout << "The Program is terminating!!" << endl;
}

这对我来说很好用。