Problem with allocation

I got this program about farm:

CCow.h
1
2
3
4
5
6
7
8
9
10
11
class CBo : public CAnimal
{

public:
	virtual void Input();
	virtual void Output();
	
	virtual ~CCow();

};


CCow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void CCow::Input() {
	
	cin.ignore(1);
	cout << "Please input name of Cow: ";
	cin.getline(name, 50);
}

void CCow::Xuat() {
	
	cout << "Name Cow: " << name << endl;	
}



CCow::~CCow() {
	
	return;
}



CSheep.h
1
2
3
4
5
6
7
8
9
10
11
12
class CSheep : public CAnimal
{

public:
	virtual void Input();
	virtual void Output();
	
	

	virtual ~CSheep();

};


CSheep.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void CSheep::Input() {
	
	cout << "Please input name of sheet: ";
	cin.ignore(1);
	cin.getline(name, 50);
	
	
}

void CSheep::Output() {
	
	cout << "Name sheep: " << name << endl;
	
}

CSheep::~CSheep() {
	
	return;
}


CAnimal.h
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
class CAnimal
{
protected:
	char name[50];
public:
	virtual void Input() = 0;
	virtual void Output() = 0;
	
	

	CAnimal() {

		strcpy(name, " ");

	} 


	CCAnimal(char name1[50]) {
		
		strcpy(name, "name1");

	}
       virtual ~CAnimal();

};


CAnimal.cpp
1
2
3
4
5
6

CAnimal::~CAnimal() {

	return;
	
}


CFarm.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class CFarm
{
protected:
	CAnimal **arrayanimal;
	int num;
public:
	virtual void Input();
	virtual void Output();
	
        CFarm(); 
	CFarm(const CFarm &);
	
	virtual ~CFarm();
};


CFarm.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
53
54
55
56
57
58
59
60
61
void CFarm::Input() {
	
	int n;
	cout << "Please input number of animal: ";
	cin >> n;
	cout << endl;
	
	arrayanimal = new CAnimal*[num];
	
	for (int i = num; i < num + n; i++) {
		
		int option;
		do
		{
			cout << "Input option:      1.COW - 2.SHEEP: ";
			cin >> option;
			
		}while (option < 1 || option > 2);
		
		switch (option)
		{
		case 1:
			arrayanimal[i] = new CCow();
			break;
		case 2:
			arrayanimal[i] = new CSheet();
			break;
		}
		
		arrayanimal[i]->Input();
	}
	num = num + n;
}
void CFarm::Output () {
	
	
	
	for (int i = 0; i < num; i++) {
		
		arrayanimal[i]->Output();
	}
	
}
CFarm::CFarm() {
	
        arrayanimal = NULL;
	num = 0;
	
}


CFarm::~CFarm() {
	
	if (num > 0) {

		num = 0;
		delete []arrayanimal;
		arrayanimal = NULL;
	}
	
}


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

int main () {
	
	CFarm t;
	int number;
	cout << endl << endl;
	
	do
	{
		cout << "-----------	1.Input.	 ----------- "<< endl; 
		cout << "-----------	2.Output.	 ----------- "<< endl; 
		cout << "-----------	3.Exit.		 ----------- "<< endl << endl; 
		
		cout << "Input your menu: ";
		cin >> number;

		switch (number)
		{
		case 1:
			{
				cout << "-------------- INPUT ----------------" << endl << endl;
				t.Input();
				cout << endl << endl;
				break;
			}
		case 2:
			{
				cout << "-------------- OUTPUT ----------------" << endl << endl;
				t.Output();
				cout << endl << endl;
				break;
			}
			
			
		}
	}
	while (number != 3);	
}
         return 0;



READ THIS Input just like this, you will see the error
1
2
3
4
5
6
7
8
9
10
11
12
13
Step 1: 
+ choose 1: Input your menu: 1
+ Please input number of animal: 1
+ Input option:      1.COW - 2.SHEET: 1
+ Then Input the Information of the Cow. No output this time => check a look Step 2.
+ Input your menu: 1

Step 2:
+ choose 1: Input your menu: 1
+ Please input number of animal: 1
+ Input option:      1.COW - 2.SHEET: 2
+ Then Input the Information of the Sheet. 
+ Input your menu: 2 // This time output => the error appear. 


Thanks for your help.
Last edited on
You seem to have a few typos; did you actually retype all of the code rather than copy/paste? I don't know what type of animal a "sheet" is. Sheep maybe?

Anyway, check your loop boundary and increment. Array should start at zero with indexes up to num-1. You put the first one in at index "num" and the second at index "num+n", but then try to output by accessing index 0 and index 1.

Also, I do not see where you initialize num to anything other than zero. It needs to be at least number of animals in size or you are attempting to read or write memory outside of the array bounds.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
	int n;
	arrayanimal = new CAnimal*[num];
	
	for (int i = num; i < num + n; i++) {
	}
	num = num + n;

// does not match with output loop
	
	for (int i = 0; i < num; i++) {
		
		arrayanimal[i]->Output();
	}

When I use the "realloc". It's complete perfect no error

I see the error but I don't know how to fix that.
Last edited on
Just because the program didn't crash, doesn't mean it's correct.

You need to allocate space for all the elements in the array. In your case, the array is num + n, so you must allocate that much space. It's a serious error to overwrite the bounds of an array.

So the declaration should be:
arrayanimal = new CAnimal*[num + n];
It's still error. Make sure you input just like the same thing above. You will see the error. Thanks for helping
Actually the array size is n
1
2
3
4
cout << "Please input number of animal: ";
	cin >> n;
//...
for (int i = num; i < num + n; i++)
(will loop n times)
You will get a memory leak the next time you choose input. Try to use vector instead.
I think about a vector too, but I don't know how to put it in my code. Give me some code for example for this. Thanks a lot.
Topic archived. No new replies allowed.