I can't seem to understand why my program runs successfully and then crashes at destructor. Below is my main() source code (which is fairly simple, it sends an array of 5 variables to a class template which creates the appropriate type. I did some research and seem to be missing something that might cause a crash because of an additional call of the destructor? I'm a little fuzzled and it's most likely a simple fix.
main.cpp:
int main()
{
// using integer data type
int arraya[5] = { 1, 2, 3, 4, 5 };
GenericArray<int> a(arraya, 5);
a.print();
// using float data type
float arrayb[5] = { 1.012, 2.324, 3.141, 4.221, 5.327 };
GenericArray<float> b(arrayb, 5);
b.print();
// using string data type
string arrayc[] = { "Ch1", "Ch2", "Ch3", "Ch4", "Ch5" };
GenericArray<string> c(arrayc, 5);
c.print();
return 0;
}
header file contents:
#ifndef GENERIC_ARRAY_H
#define GENERIC_ARRAY_H
#include<string>
#include<iostream>
template<typename type>
class GenericArray
{
public:
GenericArray(type array[], int arraySize); // constructor
~GenericArray(); // destructor
void print(); // the print function
GenericArray(const GenericArray &obj); //copy constructor
private:
type *ptr; //new pointer of respective type
int size;
};
template<typename type>//print() function
void GenericArray<type>::print()
{
for (int index = 0; index < size; index++)
{
cout << ptr[index] << " ";
}
cout << endl;
}
template<typename type>//Constructor
GenericArray<type>::GenericArray(type array[], int arraySize)
{
size = arraySize;
ptr = new type[size];
ptr = array;
}
template<typename type>//Destructor
GenericArray<type>::~GenericArray()
{
cout << "Freeing Memory!";
delete[] ptr;
}
template<typename type>//Copy Constructor
GenericArray<type>::GenericArray(const GenericArray &obj)
{
*ptr = *obj.ptr;
}
#endif
-In the print()
method:
It isn't safe, that there was memory allocated at memory positions ptr ... (ptr + size - 1)
, so you might run into a segmentation fault.
-In the constructor
:
you allocate memory via new
, but then immediately redirect your pointer to point at the same position as array
is pointing at. . This means you got a memory leak.
-In the destructor
:
As was already mentioned, your program crashes here when the destructor
is called, because the delete[] doesn't operate on the memory that was allocated with new
, see the constructor
remarks.
-In the copy constructor
:
There are two problems here. First of all, you can't dereference the lhs- ptr
here because there wasn't memory allocated for him. Moreover, if there was memory allocated for ptr
, the statement *ptr = *obj.ptr;
would just copy the first element of obj.ptr
(if there was memory allocated at this position as well) to the first element of ptr
.`
The constructors are defined incorrectly. They shall copy elements of source objects.
For example
#include <algorithm>
//...
template<typename type>//Constructor
GenericArray<type>::GenericArray( const type array[], int arraySize )
: ptr( new type[arraySize] ), size( arraySize )
{
std::copy( array, array + arraySize, ptr );
}
template<typename type>//Copy Constructor
GenericArray<type>::GenericArray( const GenericArray &obj )
: ptr( new type[obj.size] ), size( obj.size ),
{
std::copy( obj.ptr, obj.ptr + arraySize, ptr );
}
Also you need to define the copy assignment operator.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.