Segmentation error when initiating an object

Hi everyone. I'm having trouble with my program. I have a class poly, and this class has a default constructor. However when I write in the main function "poly p;", I get the error message "Segmentation error(core dumped)". Why does this happen? Thanks!

1
2
3
4
5
6
7
8
9
10
11
class poly{
	public: bool *coeff;
	int deg;
	bool sign;
	poly(){sign=false;};
	poly(int n, bool* a);
	~poly(){delete [] coeff;}
	int print();
	poly &operator =(poly &p);

};
Impossible to say without seeing the code for your default constructor.
You never initialize 'coeff'. Therefore it's likely going to be a pointer to random memory.

Then you attempt to delete[] it... but you are attempting to delete[] random memory.

You need to initialize coeff in your constructor. And since you are doing manual memory management, you will also need to overload the copy constructor and assignment operator.


OR

Instead of manually managing memory... use a container like a vector. Then there's no problem and you don't have to worry about leaks or cleanup.
Thanks for you replies. I rewrote my default constructor as

poly(){coeff=NULL;};

No error now!
Topic archived. No new replies allowed.