0

I'm getting the error message segmentation fault for the following function. I was wondering if anyone could help me fix this problem

Polynomial Polynomial :: multiply(const Polynomial& p) const{
  //The functions returns the mathematical product of the host polynomial and p

  // new object created
  Polynomial multiply;
  // node that points to the beginning of the list
  Node *temporary = m_head;

  Node *copy = p.m_head;

  long num_coeff;

  unsigned int num_deg;

  while(temporary -> m_next != NULL){
    copy = p.m_head;
    while(copy -> m_next != NULL){
      Polynomial variable;
      num_coeff = temporary -> m_coefficient * copy -> m_coefficient;
      num_deg = temporary -> m_degree + copy -> m_degree;
      variable.insertMonomial(num_coeff, num_deg);
      multiply.add(variable);
      copy = copy -> m_next;
    }
    temporary = temporary -> m_next;
  }
  return multiply; 
}
John
  • 11
  • 1

1 Answers1

0

I am willing to bet that you are running into segmentation fault because you don't have the right tests in the conditionals of the while statements.

You are using temporary -> m_next != NULL. What happens when temporary is NULL?

Instead of

while(temporary -> m_next != NULL){
   copy = p.m_head;
   while(copy -> m_next != NULL){

you should use

while(temporary != NULL){
   copy = p.m_head;
   while(copy != NULL){
R Sahu
  • 204,454
  • 14
  • 159
  • 270