Q: Solving Segmentation Fault(core dumping) error This program doubles the every second integer for the account number given and if the number is greater than 10 it is subtracted by 9. Then output whether the number entered is correct or not. Assuming that the account number is off 5 numbers. I wrote this program but does not get the answer for few number but got a correct answer for other number. Thanks for hint.
#include <iostream>

class AccountNumber {

private:
  int size = 5;

  int *p;

public:
  AccountNumber() { int *p = new (std::nothrow) int[size]; }

  ~AccountNumber() { delete[] p; }

  void getaccount() {
    int acc;
    std::cout << "Enter the account number: ";
    std::cin >> acc;
    for (int i = 0; i < size; i++) {
      p[i] = acc % 10;
    }
    setaccount(p);
  }

  void setaccount(int a[]) {
    for (int i = 0; i < size; i++) {
      p[i] = a[i];
    }
  }

  void doubles() {
    AccountNumber at;
    at.p = new int[size];
    at.p = p;
    for (int i = 0; i < size; i++) {
      if (i % 2 == 1) {
        at.p[i] = at.p[i] * 2;
        if (at.p[i] > 10) {
          at.p[i] = at.p[i] - 9;
        }
      }
    }
    p = at.p;
  }

  bool sum() {
    bool ot;
    int sum = 0;
    for (int i = 0; i < size; i++) {
      sum = sum + p[i];
    }
    int mod = sum % 10;
    if (mod == 0) {
      ot = true;
    } else {
      ot = false;
    }
    return ot;
  }

  void display(std::ostream &outs) {
    bool ot = sum();
    doubles();
    outs << "Account number entered is ";
    if (ot) {
      outs << " correct.\n";
    } else {
      outs << " is not correct. \n";
    }
  }
};

int main(int argc, char const *argv[]) {
  AccountNumber accn;
  accn.getaccount();
  accn.display(std::cout);
  return 0;
}

Output
Enter the account number: 35556
Segmentation fault (core dumped)

I don't know where I'm going wrong.

A: The issue here is that you never allocate p. Look at your constructor:
AccountNumber() 
{
    int *p = new(std::nothrow) int[size];
}

Here you are defining a new pointer variable p, which will be used instead of the member pointer variable p you defined in the private fields. What happens here is that you are allocating an int array for a new variable p, but that variable p gets thrown out at the end of the constructor (and also causes a memory leak because of the dynamic allocation that will never be reclaimed).
What you should do here instead is simply assigning the new allocated array to the member pointer variable p without redefining it, ie.
AccountNumber() {
    p = new (std::nothrow) int[size];
}

And to prevent such mistakes from happening again, you should consider using a specific naming convention for class members, such as m_ prefix (for example)
class AccountNumber {
private:
    int m_size = 5;
    int *m_p;
public:
    AccountNumber() {
        m_p = new (std::nothrow) int[size];
    }
};

