I have a safearray class and I'm trying to get it to create an array of Cat types. When trying to initialize it, though, it gives me this error:
munmap chunk invalid pointer
I haven't found a lot of info on this error message and it doesn't tell me where it occurred so I'll post the entire file (it's not long)
#include <iostream>
#include <cassert>
class Cat{
private:
std::string breed;
double age;
bool friendly;
public:
Cat(std::string b, double a, bool f);
Cat();
void setAge(int a);
void setBreed(std::string b);
void setFriendliness(bool f);
int getAge();
std::string getBreed();
bool getFriendliness();
};
Cat::Cat() {age = 1; breed = "Siamese"; friendly = true; }
Cat::Cat(std::string b, double a, bool f) : breed(b), age(a), friendly(f) {}
int Cat::getAge(){ return age; }
std::string Cat::getBreed(){ return breed; }
bool Cat::getFriendliness(){ return friendly; }
void Cat::setAge(int a){ age = a; }
void Cat::setBreed(std::string b){ breed = b; }
void Cat::setFriendliness(bool f) { friendly = f; }
template<typename T>
class SA{
private:
T* pT;
int lIdx;
int hIdx;
const int dSize = 10;
int aSize;
public:
SA();
SA(int n);
SA(int l, int h);
SA(const SA& arr);
SA<T> &operator =(const SA& rhs);
~SA();
T& operator [](int offset);
T& operator [](int offset) const;
int getSize() const;
int getLower() const;
int getHigher() const;
template <typename U>
friend std::ostream& operator <<(std::ostream &os, const SA<U>& arr);
};
//Default constructor (size of 10)
template<typename T>
SA<T>::SA(){
lIdx = 0;
hIdx = 9;
//Add one to high index so it's included as an index
aSize = hIdx + 1;
pT = new T[aSize];
if (std::is_same<T, Cat>::value){
for (int i = 0; i < aSize; i++){
T cat;
pT[i] = cat;
}
}
}
//Index from 0 to n
template<typename T>
SA<T>::SA(int n){
lIdx = 0;
hIdx = n;
aSize = hIdx + 1;
pT = new T[aSize];
if (std::is_same<T, Cat>::value){
for (int i = 0; i < aSize; i++){
T cat;
pT[i] = cat;
}
}
}
//Arbitrary indexes
template<typename T>
SA<T>::SA(int l, int h){
lIdx = l;
hIdx = h;
//Slightly modified for arbitrary indexes
aSize = (hIdx - lIdx) + 1;
pT = new T[aSize];
if (std::is_same<T, Cat>::value){
for (int i = 0; i < aSize; i++){
T cat;
pT[i] = cat;
}
}
}
//Copy constructor (SA<T> arr2 = arr1)
template<typename T>
SA<T>::SA(const SA& arr){
int s1 = arr.getSize();
//Create a new array and assign it to the array pointer (pT)
pT = new T[s1];
int lower = arr.getLower();
int higher = arr.getHigher();
lIdx = lower; hIdx = higher;
//Copy all the values in the array
for (int i = 0; i < s1; i++){
pT[i] = arr[i + lower];
}
}
template<typename T>
SA<T>::~SA(){
delete pT;
}
//Assignment operator (arr2 = arr1)
template<typename T>
SA<T> &SA<T>::operator =(const SA& arr){
int s1 = getSize();
int s2 = arr.getSize();
int lower = getLower();
int higher = getHigher();
if (s2 != s1){
throw "Array sizes not equal";
}
if (this == &arr){
throw "Cannot self assign!";
}
//Same as in copy constructor
for (int i = 0; i < s1; i++){
pT[i] = arr[i + lower];
}
return *this;
}
template<typename T>
int SA<T>::getSize() const{
return aSize;
}
template<typename T>
T& SA<T>::operator [](int offset){
//Check that the index is between arbitrary index boundaries (or regular ones)
if (offset < lIdx || offset > hIdx){
std::cout << "invalid index: write" << std::endl;
}
return pT[offset - lIdx];
}
//Overloaded version for reading-only
template<typename T>
T& SA<T>::operator [](int offset) const{
if (offset < lIdx || offset > hIdx){
std::cout << "invalid index: read" << std::endl;
}
return pT[offset - lIdx];
}
//Overloading std::out for printing
template<typename T>
std::ostream &operator <<(std::ostream &os, const SA<T>& arr) {
for (int i = arr.getBase(); i < arr.getSize(); i++){
os << arr.pT[i] << std::endl;
}
return os;
}
template<typename T>
int SA<T>::getLower() const{
return lIdx;
}
template<typename T>
int SA<T>::getHigher() const{
return hIdx;
}
int main(){
SA<float> thing1;
SA<float> thing2(0, 9);
SA<float> thing3(15, 30);
thing3[19] = 30;
thing2[8] = 15;
thing1[7] = 15;
std::cout << thing3[19] << std::endl;
std::cout << thing2[8] << std::endl;
SA<float> thing4(thing3);
std::cout << thing1[7] << std::endl;
std::cout << thing4[19] << std::endl;
SA<float> thing5(15, 30);
thing5 = thing3;
std::cout << thing5[19] << std::endl;
//SA<Cat> catArray(0, 5); <--- ERROR OCCURS WHEN I UNCOMMENT THIS LINE
//catArray[0].setAge(15);
return 0;
}