Debug Assertion Failed

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.
Last edited on
First, your program doesn't compile
|In member function ‘CBinary CBinary::operator=(CBinary)’:
|110 col 11| error: no viable constructor returning object of type 'CBinary'
At global scope:
|246 col 12| error: ‘::main’ must return ‘int’
In function ‘int main()’:
|248 col 33| error: ISO C++ forbids converting a string constant to ‘char*’
|248 col 49| error: ISO C++ forbids converting a string constant to ‘char*’



After you fix the errors, run through valgrind
$ valgrind ./a.out
==3370== Conditional jump or move depends on uninitialised value(s)
==3370==    at 0x400D80: CBinary::CBinary(CBinary const&) (foo.cpp:92)
==3370==    by 0x4008A8: main (foo.cpp:252)

It referes to
92
93
94
95
96
97
98
	CBinary(const CBinary &temp)
	{
		number=new int[len]; //here len is uninitialized
		for(int i=0;i<temp.len;i++)
			number[i]=temp.number[i];
		len=temp.len; //it's too late now
	}


moving on
$ valgrind ./a.out
==3615== Invalid read of size 4
==3615==    at 0x400E5B: CBinary::operator^=(CBinary) (foo.cpp:182)
==3615==    by 0x4008BB: main (foo.cpp:252)
==3615==  Address 0x5a9ee3c is 0 bytes after a block of size 28 alloc'd
==3615==    at 0x4C2BCAF: operator new[](unsigned long)
==3615==    by 0x400C48: CBinary::CBinary(char const*) (foo.cpp:65)
==3615==    by 0x40086C: main (foo.cpp:248)
1
2
3
4
5
6
7
8
9
10
11
12
13
	c.display(); //7 digits
	d.display(); //10 digits
	c^=d;
	//(that calls to)
	CBinary& operator^=(CBinary z)
	{
		for(int i=0;i<z.len;i++) //z.len=10, this->len=7
		{
			if (number[i]==z.number[i]) //out of bounds access
			//...
		}
		return *this;
	}



moving on
$ valgrind --leak-check=full ./a.out
==4318== HEAP SUMMARY:
==4318==     in use at exit: 44 bytes in 1 blocks
==4318==   total heap usage: 9 allocs, 8 frees, 73,972 bytes allocated
==4318== 
==4318== 44 bytes in 1 blocks are definitely lost in loss record 1 of 1
==4318==    at 0x4C2BCAF: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==4318==    by 0x400ADC: CBinary::CBinary(int) (foo.cpp:42)
==4318==    by 0x40085B: main (foo.cpp:249)
temp=new int[len]; `temp' was never delete[]d
So what should I do:

First
1
2
3
4
5
6
7
8
CBinary(const CBinary &temp)
{
	len=temp.len; 
        number=new int[len]; 
	for(int i=0;i<temp.len;i++)
		number[i]=temp.number[i];
	
}


Second
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
c.display(); //7 digits
d.display(); //10 digits
c^=d;

CBinary& operator^=(CBinary z)
{
        int length;
        if (len>z.len) length=len;
        else length=z.len;
	for(int i=0;i<length;i++) 
	{
		if (number[i]==z.number[i]) 
		//...
	}
	return *this;
}


And the last
1
2
3
4
5
CBinary(int a)
{
          //...
          delete[] temp;
}


Thank you so much... That was a great help...
Topic archived. No new replies allowed.