currently I'm rewriting old euler solutions into classes so i can store them in a library. I wrote a program that digitizes a number so it can check to see if it a pallindrome, The base class digitize dynamically allocates memory for the size of a number and stores each digit, My second class pallindrome inherits the members of digitize, and refrences the constructor using the :, however when I construct the pallindrome class i get a back trace error. I know I can probably just not use inheritance and it will work but i want to know why this is occur, but i want to inherit it, how can i fix this?
the error message looks like this
*** glibc detected *** /home/developer/workspace/Eruler/4- Palindrome/Debug/4- Palindrome: double free or corruption (fasttop): 0x000000000241b010 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x78a8f)[0x7f0e84df9a8f]
/lib/x86_64-linux-gnu/libc.so.6(cfree+0x73)[0x7f0e84dfd8e3]
/home/developer/workspace/Eruler/4- Palindrome/Debug/4- Palindrome[0x400b25]
/home/developer/workspace/Eruler/4- Palindrome/Debug/4- Palindrome[0x400b81]
/home/developer/workspace/Eruler/4- Palindrome/Debug/4- Palindrome[0x400aa0]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xff)[0x7f0e84d9feff]
/home/developer/workspace/Eruler/4- Palindrome/Debug/4- Palindrome[0x4007e9]
======= Memory map: ========
#include <iostream>
#include <math.h>
using namespace std;
typedef unsigned long long _uint64;
class Digitize
{
protected:
int * number;
int Digits;
public:
Digitize(_uint64);
~Digitize() {delete [] number;}
void Display();
};
Digitize::Digitize(_uint64 x)
{
int y = 0, tmp = x;
while(tmp!=0) tmp/=10, y++;
Digits = y, number = new int[y];
for(int c=y-1; c>=0; c--, x/=10)
number[c]=x%10;
}
void Digitize::Display()
{
int D=0;
while (D<Digits)
{
cout << number[D] << " ";
D++;
}
cout << endl;
}
class Pallindrome : public Digitize
{
public:
Pallindrome(_uint64 Con) : Digitize(Con) {};
~Pallindrome (){delete [] number;}
bool ispallindrome ();
};
bool Pallindrome::ispallindrome()
{
for (int start = 0, end=Digits-1; start<end; start++, end--)
{
if (number[start]!=number[end])
return false;
}
return true;
}
int main ()
{
Pallindrome num(809); // <- This Does
//Digitize numb(809); // <- This Does Not Give an Error
//num.ispallindrome() ? cout << "True" << endl : cout<< "False" << endl;
//num.Display();
return 0;
}