Bug hunt!

closed account (ETAkoG1T)
I made a program that can use a function to manipulate data from a list class. The program is very basic and I think the error makers may be obvious to some of you. I just wanted to find it without having to scrap my program. It doesn't generate a compile error but it announces a memory error while it is running. I use visual studio 2012, the program is an exercise from a c++ book.
list.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef LIST_H_
#define LIST_H_
typedef double Item;
const int MAX = 10;
class List
{
private:
	Item arr[MAX];
	int elements;
public:
	List(Item ar[] = 0, int max = MAX);
	void AddToList(Item a);
	bool isempty();
	bool isfull();
	void visit(void (*pf) (Item &));
	void show();

};

#endif; 


main.cpp
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
// Ch10_8.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "stdafx.h"
#include "list.h"
void divide(Item &x);
int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	Item ages[4] = {0, 4, 8, 2};
	List age(ages, 4);
	cout << "The string is empty "<< age.isempty() << endl;
	cout << "The string is full " << age.isfull() << endl;
	cout << "The list, ";
	age.show(); 
	cout << endl;
	age.AddToList(23.5);
	age.visit(divide);
	List age2;
	cout << "The string is empty "<< age2.isempty() << endl;
	cout << "The string is full " << age2.isfull() << endl;
	cout << "The list, ";
	age2.show(); 
	cout << endl;
	age2 = age;
	cout << "The string is empty "<< age2.isempty() << endl;
	cout << "The string is full " << age2.isfull() << endl;
	cout << "The list, ";
	age2.show(); 
	cout << endl;
	while(!age2.isfull())
	{
		age2.AddToList(2.1);
	}
	cout << "The string is empty "<< age2.isempty() << endl;
	cout << "The string is full " << age2.isfull() << endl;
	cout << "The list, ";
	age2.show(); 
	cout << endl;
	cin.get();
	cin.get();
	return 0;
}

void divide(Item &x)
{
	x = x/2;
}


//function definitions for class
list.cpp
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
// Ch10_8.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include "stdafx.h"
#include "list.h"
void divide(Item &x);
int main()
{
	using std::cout;
	using std::cin;
	using std::endl;
	Item ages[4] = {0, 4, 8, 2};
	List age(ages, 4);
	cout << "The string is empty "<< age.isempty() << endl;
	cout << "The string is full " << age.isfull() << endl;
	cout << "The list, ";
	age.show(); 
	cout << endl;
	age.AddToList(23.5);
	age.visit(divide);
	List age2;
	cout << "The string is empty "<< age2.isempty() << endl;
	cout << "The string is full " << age2.isfull() << endl;
	cout << "The list, ";
	age2.show(); 
	cout << endl;
	age2 = age;
	cout << "The string is empty "<< age2.isempty() << endl;
	cout << "The string is full " << age2.isfull() << endl;
	cout << "The list, ";
	age2.show(); 
	cout << endl;
	while(!age2.isfull())
	{
		age2.AddToList(2.1);
	}
	cout << "The string is empty "<< age2.isempty() << endl;
	cout << "The string is full " << age2.isfull() << endl;
	cout << "The list, ";
	age2.show(); 
	cout << endl;
	cin.get();
	cin.get();
	return 0;
}

void divide(Item &x)
{
	x = x/2;
}


I know the design of the program is flawed, I don't want to totally change my approach, just find the bug and kill it.
You have posted main.cpp twice.
closed account (ETAkoG1T)
lol, my mistake. That however is not the reason for the error :P
list.cpp
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
#include "list.h"
#include <iostream>

List::List(Item ar[], int max)
{
	int i = 0;
	while(i < max)
	{
		arr[i] = ar[i];
		i++;
	}
	elements = i;
}

void List::AddToList(Item a)
{
	if(elements < MAX)
		arr[elements] = a;
	++elements;
}

bool List::isempty()
{
	if(elements == 0)
		return true;
	else
		return false;
}

bool List::isfull()
{
	if(!elements < MAX)
		return true;
	else
		return false;
}

void List::visit(void (*pf) (Item &))
{
	for(int i = 0;i<elements;i++)
	{
		pf(arr[i]);
	}
}

void List::show()
{
	for(int i = 0;i<elements;i++)
	{
		std::cout << arr[i] << ' ';
	}
}
Last edited on
You need to rethink your List constructor(s);

Your List constructor looks like this:

List(Item ar[] = 0, int max = MAX); //not good default parameters for the constructor

The implementation looks like this:
1
2
3
4
5
6
7
8
9
10
List::List(Item ar[], int max)
{
	int i = 0;
	while(i < max)
	{
		arr[i] = ar[i];
		i++;
	}
	elements = i;
}



In main you create a List Like this:
List age2; //Uses default parameters for constructor - which will use a NULL pointer!!!!!


Now you can see why you have the problem.
Last edited on
closed account (ETAkoG1T)
Wow, that wasn't so hard after all :) I overloaded the constructor so that it would work with empty lists!
Topic archived. No new replies allowed.