0

I have a header file like this

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

Then in my .cpp file I write this line

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

which is giving segmentation fault. I guess it is due to rule of three . Then I have added this part;copy constructor and assignment constructor

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

Still not working. Please anybody help me to resolve this problem. thanks

  • 4
    That "line" in your source file is not valid C++, and I doubt it's really there. If you give us a [minimal complete example](http://stackoverflow.com/help/mcve) we may be able to help you. – Beta Mar 18 '15 at 03:32
  • 4
    Why are you dynamically allocating arrays of 1 `complex`? Your life would be much simpler if you didn't. – David Rodríguez - dribeas Mar 18 '15 at 03:32
  • 1
    hard to say without seeing your destructor. – user3528438 Mar 18 '15 at 03:32
  • I have edited and now you can see what is actually there.... if u still need more information, eel free to ask. And I used destructor too. I think my copy constructor and copy assignment is not correct as I have written myself and i dont have that much idea – Reza Nishat Mar 18 '15 at 05:34

2 Answers2

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

This works fine for me.

Antu
  • 1
  • 1
-1

Try to use a destructor as you used new.

public:

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