Heap Problems

I am creating a program that allocates memory dynamically for a book store management program.

The first error that I receive is: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

If I click "ignore" I then get: _CtrIsValidHeapPointer(pUserData)

The program deals with two classes. One is a Book class which contains Cstrings that hold the author and title, and it also holds an enumerated Genre type along with a price variable.

The second class is a Store class. In this class's constructor I create the dynamically allocated array.


1
2
3
4
5
6
Store::Store()
{
	maxSize = 5;
	currentSize = 0;
	bookList = new Book[maxSize];
}


*A side note on the Store class: currentSize is constantly resetting itself when I try to increment it (I was trying to use it as an index variable). No where in the entire code is there a statement that reassigns it... just the ++ operator.

Both classes interact through a main program where all the input is done (it's a rule for the assignment).


The program will read one book object, and appears to store the data fine.
Then once I try to store another object (mind you, the second book object actually does get created) the program crashes and gives me these errors.


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
int main()
{
	int index= 0;
	Store s;
	showMenu();
	char menuAction;

	do
	{
		menuAction = menuSelection();
		switch(menuAction)
		{
			case 'A': addBook(s, index);
				index++;
				break;
			case 'F':
				break;
			case 'S':
				break;
			case 'D': s.showInventory(index);
				break;
			case 'G':
				break;
			case 'M':
				break;
			case 'X':
				break;
		}
	} 
	while (menuAction != 'X');


		
	return 0;
}


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
void addBook(Store s, int index)
{
	Book b;
	char t[31];
	char a[21];
	char type;
	double p;
	cin.get();

	cout << "Enter the title-> ";
	cin.getline(t, 31);
	cout << endl;

	cout << "Enter the author-> ";
	cin.getline(a, 21);
    cout << endl;

	cout << "Enter a genre -> ";
	cin >> type;
	cout << endl;

	cout << "Enter a price-> ";
	cin >> p;
	cout << endl;

	Genre g;
	switch(type)
	{
	   case 'F': g = FICTION;
		   break;
	   case 'M': g = MYSTERY;
		   break;
	   case 'S': g = SCIFI;
		   break;
	   case 'C': g = COMPUTER;
	   default:
		   break;
	}

	b.Set(t, a, g, p);
	s.Add(b, index);
}


1
2
3
4
5
6
7
void Book::Set(const char* t, const char* a, Genre g, double p)
{
	strcpy(title, t);
	strcpy(author, a);
	price = p;
	type = g;
}


1
2
3
4
void Store::Add(Book b, int index)
{
	bookList[currentSize++] = b; //I've tried bookList[index] too
}


The index (which the debugger shows does in fact hold value) variable is something that is just being passed around the entire body of code because currentSize won't hold a value.

This is a homework assignment... So, I am not trying to post all my code on here for it to be vultured away by a fellow classmate.

Topic archived. No new replies allowed.