[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 340
  • Last Modified:

Segmentation Fault in dynamic array

it compiled + linked however it gave Segmentation Fault when i tried to run .


is there something wrong with my function implementation part ?


+++++++++++=

#ifndef BIG_ARRAY_H
#define BIG_ARRAY_H

class BigArray
{
 public:
  BigArray(long n);
  // will create an array of doubles of size 10000 + n
  ~BigArray();
  BigArray (const BigArray& other);
  BigArray& operator = (const BigArray& rhs);
 private:
  double* a;
  long size;
};

#endif



+++++++++++++++++


#include "big_array.h"


BigArray::BigArray(long n)
{
         size = n;
         
}

BigArray::~BigArray()

{
                     
     delete [] a;
     
}

BigArray::BigArray (const BigArray& other)
{
                   
    size = other.size;
      for (int i =0; i < size ; i++)
      a[i]= (other.a[i]);
   
}

BigArray& BigArray::operator = (const BigArray& rhs)

{
         
          if (this != &rhs)
          delete []a;
           
           size = rhs.size;
           a = new double [size];
           
           for (int k =0; k < size; k++ )
           a[k] = rhs.a[k];
           
           
       return *this;

}



++++++++++++++++
#include "big_array.h"

void assign_many (int n);

int main ()
{
  assign_many(10000);
  return 0;
}








+++++++++++++++++++
function implementation
+++++++++++++

0
kevinng
Asked:
kevinng
  • 3
  • 2
  • 2
2 Solutions
 
mrwad99Commented:
>>
+++++++++++++++++++
function implementation
+++++++++++++

It would be nice to see the code for that, too...
0
 
mrwad99Commented:
Problems with code posted:

>> BigArray::BigArray(long n)
{
      size = n;
      
}
You need to allocate memory here:

BigArray::BigArray(long n)
{
       a = new double(n);
      size = n;
      
}

otherwise the destructor is calling delete on a, which is uninitialised.

>> BigArray::BigArray (const BigArray& other)
{
      
    size = other.size;
      for (int i =0; i < size ; i++)
            a[i]= (other.a[i]);
      
}

You need to allocate memory here as well:

BigArray::BigArray (const BigArray& other)
{
    size = other.size;
    a = new double [size];
   
      for (int i =0; i < size ; i++)
            a[i]= (other.a[i]);
      
}
0
 
kevinngAuthor Commented:
the function implementation is already there . As in sequence header => implementation => driver
sorry for the confusion

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jkrCommented:
>>void assign_many (int n);

The function's implementation is indeed missing.
0
 
mrwad99Commented:
Also

>> BigArray& BigArray::operator = (const BigArray& rhs)

needs to be

BigArray& BigArray::operator = (const BigArray& rhs)
{
      
      if (this != &rhs)
        {
            delete []a;
      
              size = rhs.size;
              a = new double [size];
      
              for (int k =0; k < size; k++ )
                  a[k] = rhs.a[k];
      
      }
      return *this;
      
}
0
 
kevinngAuthor Commented:
argh
i forgot to include  that assign_many  function


+++++++++++
void assign_many (int n)
{
  BigArray b(10);
  for (int i = 0; i < n; i++)
  {
    BigArray bc (20);
    bc = b;
  }
}
0
 
jkrCommented:
Runs without any segmentation fault here - BTW, you forgot to allocate the memory in the constructor:

BigArray::BigArray(long n)
{
        size = n;
       a = new double[size]; // <----------
}

BigArray::~BigArray()

{
                     
     delete [] a;
   
}

BigArray::BigArray (const BigArray& other)
{
                 
    size = other.size;
    a = new double[size]; // <----------
     for (int i =0; i < size ; i++)
     a[i]= (other.a[i]);
   
}
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now