Im new in c++. This is for the first time I have this type of error. It works well until when the debug reached the end of void main, the error appear. The output was displayed but I also get this error and it says,
Debug Assertion Failed!
Expression: _CrtIsValidHeapPointer(pUserData)
For information on how your program can cause an assertion failure, see the Visual C++ documentation on asserts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
|
#include<stdio.h>
#include<string.h>
#include<ctype.h> // library for isalpha
class CBinary{
int *number,len; //len=length
public:
CBinary()
{
number=new int[1];
number[0]=0;
len=0;
}
CBinary(int a)
{
int b=a,digit=0;
while(b>0)
{
b/=2;
if(b>0)
digit++;
}
number=new int [digit+1];
for(int i=0;i<digit+1;i++)
number[i]=0;
int i=0;
for(;i<digit;)
{
number[i]=a%2;
a/=2;
i++;
}
len=digit+1;
if (digit>1)
number[digit]=1;
int *temp;
temp=new int[len];
for(int i=0;i<len;i++)
{
temp[i]=0;
}
int z=i;
for(int j=0;j<i+1;j++)
{
temp[j]=number[z];
z--;
}
delete[] number;
number=new int[len];
for(int j=0;j<i+1;j++)
{
number[j]=temp[j];
}
}
CBinary(char *b)
{
/*printf("%s\n",b);*/
len=strlen(b);
number=new int [len];
for(int i=0;i<len;i++)
{
number[i]=0;
}
for(int i=0;i<len;i++)
{
if (isalpha(b[i]))
{
number[i]=1;
/*printf("%d\n",number[i]);*/
}
else if (b[i]=='0' || b[i]=='1')
{
number[i]=b[i]-48;
/*printf("%d\n",number[i]);*/
}
else
{
number[i]=1;
/*printf("%d\n",number[i]);*/
}
}
}
CBinary(CBinary &temp)
{
number=new int[len];
for(int i=0;i<temp.len;i++)
number[i]=temp.number[i];
len=temp.len;
}
~CBinary()
{
delete[] number;
}
CBinary operator=(CBinary z)
{
delete[] number;
number=new int[z.len];
for(int i=0;i<z.len;i++)
number[i]=z.number[i];
len=z.len;
return *number;
}
CBinary operator&(CBinary z)
{
CBinary temp;
temp.number=new int[z.len];
for(int i=0;i<z.len;i++)
{
if (number[i]==1 && z.number[i]==1)
temp.number[i]=1;
else
temp.number[i]=0;
printf("%d ",temp.number[i]);
}
return temp;
}
CBinary operator|(CBinary z)
{
CBinary temp;
temp.number=new int[z.len];
for(int i=0;i<z.len;i++)
{
if (number[i]==0 && z.number[i]==0)
temp.number[i]=0;
else
temp.number[i]=1;
printf("%d ",temp.number[i]);
}
return temp;
}
CBinary operator^(CBinary z)
{
CBinary temp;
temp.number=new int[z.len];
for(int i=0;i<z.len;i++)
{
if (number[i]==z.number[i])
temp.number[i]=0;
else
temp.number[i]=1;
printf("%d ",temp.number[i]);
}
return temp;
}
CBinary& operator&=(CBinary z)
{
for(int i=0;i<z.len;i++)
{
if (number[i]==1 && z.number[i]==1)
number[i]=1;
else
number[i]=0;
printf("%d ",number[i]);
}
return *this;
}
CBinary& operator|=(CBinary z)
{
for(int i=0;i<z.len;i++)
{
if (number[i]==0 && z.number[i]==0)
number[i]=0;
else
number[i]=1;
printf("%d ",number[i]);
}
return *this;
}
CBinary& operator^=(CBinary z)
{
for(int i=0;i<z.len;i++)
{
if (number[i]==z.number[i])
number[i]=0;
else
number[i]=1;
printf("%d ",number[i]);
}
return *this;
}
CBinary operator>>(int n)
{
CBinary temp;
temp.number=new int[len];
int idx=0;
for(int i=0;i<len;i++)
{
if (i<n)
temp.number[i]=0;
else
{
temp.number[i]=number[idx];
idx++;
}
}
return temp;
}
CBinary operator<<(int n)
{
CBinary temp;
temp.number=new int[len];
int idx=len-1;
for(int i=0;i<len;i++)
{
if (i<=n)
{
temp.number[i]=number[idx];
idx--;
}
else
temp.number[i]=0;
}
return temp;
}
int GetLength()
{
return len;
}
bool operator[](int i)
{
int show;
show=number[i];
if (number[i]==0)
return false;
else if (number[i]==1)
return true;
}
void display()
{
for(int i=0;i<len;i++)
printf("%d ",number[i]);
printf("\n");
}
};
void main ()
{
CBinary a, b(1100),c("0;10 00"),d("1101110010");
c.display(); //7 digits
d.display(); //10 digits
c^=d;
}
|
So my program is about compare one digit to another digit.
Operator ^ is like implication function in math.
If 0 compare with 1 the result is 1
If 1 compare with 0 the result is 1
If 0 compare with 0 the result is 0
If 1 compare with 1 the result is 0
If none compare with 0 or 1 the result is 1
For the example like above:
if I run the program it shows
0 1 1 0 1 0 0 // c("0;10 00") = 7 digits // everything except 0 or 1 will be 1
1 1 0 1 1 1 0 0 1 0 // d("1101110010") = 10 digits
1 0 1 1 0 1 0 1 1 1 // the result
If I use comment out for destructor, it works well.
Comment out:
1 2 3 4
|
/*~CBinary()
{
delete[] number;
}*/
|
But if the destructor must exist in the program, how it should be.
As soon as the output was displayed, I also get a popup error message about Debug Assortion Failure. I dont have any idea what should I do. Please help me. Thank you.