Saturday, 28 September 2013

C++ destructor deletes shared memory

C++ destructor deletes shared memory

I have program in C++ that does the following: get a non-sorted array of
integers and devide it into sub-arrays in accending order. for example. my
array is 1,2,3,-2,4 So the output will be: 1,2,3 //sub array 1 -2,4 //sub
array 2
the way I requiered to do it is to allocate array of pointers and each
cell in that array will point to sub array (also dynamic allocated)
containing one series. also, I am not allowed to change the "main"
function
now, I have "print" method that print the sub-arrays using operator
overloading.
I checked and found the print method working fine but in this piece of code:
cout << "\ns3: \n";
print(s3);
s3 = s1;
cout << "\ns3 again: \n";
print(s3);
print(s3) prints fine, but after the assignment operator when print(s3)
called again I get a junk output. after some dubugging I think it is
something releated to the destructor.
I very apperciate it if anyone can point and identify my problem and come
with a solution. of course I will give the source code
here:
// HW4.cpp : Defines the entry point for the console application.
//
#include <iostream>
using namespace std;
//global declerations and indexes//
typedef long int * LONG_INT_PTR;
//class
class SeriesArray
{
//members declerations
private:
LONG_INT_PTR *stable;
int *count_arr; //help array that indicate how many serieses
made from the array and how many element in each series
int count_size; // help counter
int size;
public:
SeriesArray::SeriesArray(LONG_INT_PTR arr, int n );
SeriesArray::SeriesArray(const SeriesArray& src );
SeriesArray::~SeriesArray();
long int SeriesArray::get_entry(int i,int j) const;
SeriesArray& SeriesArray::operator=(const SeriesArray& src);
friend ostream& operator<<(ostream& stream, const SeriesArray&
src);//global - not member
};
//members implementation
//default constructor
SeriesArray::SeriesArray(LONG_INT_PTR arr, int n )
{
size=n;
int j=0;
int s=0;
count_size=0;
count_arr=new int[size];
for (int i=0;i<size;i++)
{
if (arr[i]<arr[i+1])
count_size++;
else
{
count_size++;
count_arr[j]=count_size;
j++;
count_size=0;
}
}
size=j;
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
stable[i]=new long int[count_arr[i]];
for (int k=0;k<count_arr[i];k++)
{
stable[i][k]=arr[s];
s++;
}
}
}
//copy constructor
SeriesArray::SeriesArray(const SeriesArray& src )
{
size=src.size;
count_arr=new int[size];
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
count_arr[i]=src.count_arr[i];
stable[i]=new long int[count_arr[i]];
}
memcpy(this->stable,src.stable,src.size*sizeof(long int));
}
//destructor
SeriesArray::~SeriesArray()
{
for (int i=0;i<size;++i)
delete[] stable[i];
delete[] this->count_arr;
count_size=0;
}
//member
long int SeriesArray::get_entry(int i,int j) const
{
if (i<this->count_size&&this->stable[i][j]!=NULL)
return (this->stable[i][j]);
else return NULL;
}
//overload = operator
SeriesArray& SeriesArray::operator=(const SeriesArray& src)
{
if (this==&src)
return *this;
if (stable!=NULL)
delete[] stable;
if (count_arr!=NULL)
delete[] count_arr;
//for (int i=0;i<size;i++)
// delete[] stable[i];
size=src.size;
count_arr=new int[src.size];
memcpy(count_arr,src.count_arr,src.size*sizeof(int));
stable=new LONG_INT_PTR[size];
for (int i=0;i<size;i++)
{
// count_arr[i]=rhs.count_arr[i];
stable[i]=new long int[src.count_arr[i]];
}
memcpy(stable,src.stable,src.size*sizeof(long int));
return *this;
}
//***************************************************************************************************************************************************//
//global//
//overload operator <<
ostream& operator<<(ostream& stream,const SeriesArray& src)
{
for (int i=0;i<src.size;i++)
{
for (int j=0;j<src.count_arr[i];j++)
{
stream << " " << src.stable[i][j] << " " ;
}
stream << "\n\n\n" ;
}
return stream;
}
//print
void print(SeriesArray src)
{
cout << src;
cout << "\n";
} // print
//main
int main()
{
long int arr1[20] = {23, 91, -71, -63, 22, 55, 51, 73, 17, -19,
-65, 44, 95, 66, 82, 85, 97, 30, 54, -34};
long int arr2[10] = {0, 1, -7, -6, 2, 5, 6, 7, 1, -1};
int count[20], i, j, n =20, sno;
long int *parr[20];
SeriesArray s1(arr1, 20);
SeriesArray s2(arr2, 10);
SeriesArray s3(arr2, 10);
cout << "\narr1:\n";
for(i=0; i < 20; i++)
cout << " " << arr1[i] << " ";
cout << "\n";
cout << "\n\ntable:\n";
print(s1);
cout << "\narr2\n";
for(i=0; i < 10; i++)
cout << " " << arr2[i] << " ";
cout << "\n";
cout << "\n\ntable:\n";
print(s2);
cout << "\ns3: \n";
print(s3);
s3 = s1;
cout << "\ns3 again: \n";
print(s3);
cout << "\ns1 again: \n";
print(s1);
cin>>i;
return 0;
} // main

No comments:

Post a Comment