Debug Assertion Failed … _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

Debug Assertion Failed … _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)


May I know why im getting this error
in google, they said it something problem about dynamic array deletion

I have my own vector class used for a creating a single dimensional array.
Below is the code for my own vector (Vector)
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
#ifndef H_VECOTR
#define H_VECTOR
#include"file.h"

template <class elemType>

class Vector
{
	
public:
	friend ostream& operator<<(ostream&, const Vector<elemType>&);
	//const Vector<elemType>& operator=(const Vector<elemType>&);//
	int list_size() const;//
	int max_list_size() const;//
	void print() const;//
	
	void insert(const elemType& insert_item);//
	elemType& retrieve(int location,elemType& ret_item) const;//
	
	Vector(int size=100);//
	virtual ~Vector();//
//protected:
	
	elemType *list;
	int length;
	int max_size;
};
template<class elemType>
int Vector<elemType>::list_size() const
{
	return length;
}

template<class elemType>
int Vector<elemType>::max_list_size() const
{
	return max_size;
}

template<class elemType>
void Vector<elemType>::print() const
{
	for(int i=0;i<length;i++)
		cout<<list[i]<<" ";
	
}



template<class elemType>
elemType& Vector<elemType>::retrieve(int location,elemType& ret_item)const
{
	ret_item = NULL;
	if(location<0||location>=length)
		cout<<"The item is out of range"<<endl;
	else
		ret_item = list[location];
	return ret_item;
}

template<class elemType>
Vector<elemType>::Vector(int size)
{
	if(size<=0)
	{
		cout<<"The array size is negative\n";
		cout<<"Creating an array of size 100.\n";
	}
	else
		max_size=size;
	length=0;
	list=new elemType[max_size];
}
template<class elemType>
Vector<elemType>::~Vector()
{
	delete [] list;
}

template<class elemType>
void Vector<elemType>::insert(const elemType& insert_item)
{
	if(length>=max_size)
		cout<<"List is full. Cannot perform insertion"<<endl;
	else
	{
		list[length]=insert_item;
		//cout<<list[length];
		length++;
	}
}


template<class elemType>
ostream& operator<<(ostream& os, const Vector<elemType>& Tx)
{
	os<<Tx;
	return os;
}

#endif  
Help please
Now I where the error part is but I dont know exactly which point.
The error is in passing parameters

I passed vector , length and class object as parameter in a function

specific_month(length,Tx,user_month,E); //This is to calculate the number of shares and total cost for the given month
length=file size
Tx= vector class object ( Vector<ex>Tx(length) )
E= object of class ex
user_month=month entered by the user;

I copied the whole code from the function "specific_month" and put it in the main, then I ran the program. Now the error has gone.
What wrong im doing in function parameters?

My function prototype is void specific_month(const int length,Vector<ex> Tx,int user_month,ex E);

Am i passing the vector and class object correctly?
Last edited on
Not enough code here to diagnose your problem.

If you're getting a debug assertion occurring in the run time memory management routines, you're either passing a bad pointer to delete or you've trashed the heap.
given my test(main) code. Will be that ok?

Look for the lines 12,62,from 128 to last(these things causing error


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
#include<iostream>
#include<fstream>
#include"file.h"
#include<string>
#include<sstream>
#include"Vector.h"

using namespace std;
int get_file_size(ifstream& in);
void output(int& choice);
int month_input();
void specific_month(const int length,Vector<ex> Tx,int user_month,ex E);
int main()
{
	
	ifstream in;
	//finidng file size
	const int length=get_file_size(in);
	
	Vector<ex>Tx(length);
	
	int day,month,year,shares;
	double price;
	string code,activity,line;
	int choice, user_month;
	cout<<"Helloo0"<<endl;
	
	in.open("share-data.txt");
	ex E;
	for(int i=0;i<length;i++)
	{	
		getline(in,line,'/');
		stringstream(line)>>day;
		getline(in,line,'/');
		stringstream(line)>>month;
		getline(in,line,',');
		stringstream(line)>>year;
		getline(in,line,',');
		stringstream(line)>>code;
		getline(in,line,',');
		stringstream(line)>>activity;
		getline(in,line,',');
		stringstream(line)>>shares;
		getline(in,line);
		stringstream(line)>>price;
		E.set_all(day,month,year,code,activity,shares,price);
		cout<<i<<endl;
		Tx.insert(E);		
	}
	
	output(choice);
	
	if(choice==2)
		user_month=month_input();
	else 
	{
		cout<<"Thanks for using the program.Bye-Bye\n";
		system("pause");
		return -1;
	}

	specific_month(length,Tx,user_month,E);
	
	in.close();
	system("pause");
	return 0;
}

int get_file_size(ifstream& in)
{
	in.open("share-data.txt");	
	int temp;
	int size=0;
	while(!in.eof())
	{
		in.ignore(100,'\n');
		in>>temp;
		size++;
	}
	in.close();
	return size;
	cout<<"Size of the file is: "<<size<<endl;
}

void output(int& choice)
{
	cout<<"1. Total capital gain of the year\n";
	cout<<"2. Total shares bought for the given month\n";
	cout<<"3. Exit the program\n";
	cout<<"Please enter your choice: ";
	cin>>choice;
	while(choice<1 || choice>3)
	{
		cout<<"\nERROR:Invalid choice entered "<<endl;
		cout<<"Please enter your choice(1/2/3): ";
		cin>>choice;
	}
	cout<<endl;
}
int month_input()
{
	int user_month;
	cout<<"Please choose the month you want to know shares bought and amount spend\n";
	cout<<"Month     Number\n";
	cout<<"January     1 \n";
	cout<<"Febrauary   2 \n";
	cout<<"March       3 \n";
	cout<<"April       4 \n";
	cout<<"May         5 \n";
	cout<<"June        6 \n";
	cout<<"July        7 \n";
	cout<<"August      8 \n";
	cout<<"September   9 \n";
	cout<<"October    10 \n";
	cout<<"November   11 \n";
	cout<<"Decemebr   12 \n";
	cout<<"\nPlease enter the month number: ";
	cin>>user_month;
	while(user_month<1||user_month>12)
	{
		cout<<"ERROR: Invalid month number!!\n";
		cout<<"Please enter the month number again: ";
		cin>>user_month;
	}
	cout<<endl;
	return user_month;
}
void specific_month(const int length,Vector<ex> Tx,int user_month,ex E)
{
	int month_shares=0;
	double month_spent=0;
	bool found=false;

	for(int i=0;i<length;i++)
	{
		if(user_month==Tx.retrieve(i,E).date1.get_month() && Tx.retrieve(i,E).get_activity()=="buy")
		{
			
			month_shares+=Tx.retrieve(i,E).get_share();
			month_spent+=Tx.retrieve(i,E).get_price();
			found=true;
		}
		
	}
	if(found)
	{
	cout<<"Total share bought for the month "<<user_month<<" is "<<month_shares<<" shares"<<endl;
	cout<<"Amount spend for the month "<<user_month<<" $ "<<month_spent<<endl;
	}
	else
		cout<<"No such activites"<<endl;
}

Last edited on
Got it! In my function declaration,
void specific_month(const int length,Vector<ex> Tx,int user_month,ex E);

I didnt pass vector class as reference. In vector class, i have a destructor of array created dynamically. As I pass Vector as value, the function couldnt delete it after program finish. When I changed the Vector as pass by reference, the error has gone

void specific_month(const int length,Vector<ex>& Tx,int user_month,ex E);
Topic archived. No new replies allowed.