reading memory in a pointer

Hi,
I am having a problem with a small exercise program. The program works the first time through the loop but not the second. It faults on this line:
cin >> *(pint + i1);, with this error: pint <Unable to read memory>.

thanks, any help would be appreciated.

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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int MAX(5);
	int count(4);
	int value(MAX);
	int i1(0);
	int i2(0);
	char answer('n');
	int* pint(nullptr);
	pint = new int[MAX];

	do
	{
		cout << "Enter five integers to fill array: \n";
		for (i1 = i2; i1 < MAX; i1++)
		{
			cout << "Enter integer: ";
			cin >> *(pint + i1);
		}
			cout << "Do you want to continue with five more? ";
			cin >> answer;
			if (answer == 'y')
			{
				MAX = MAX + 5;
				i2 = 0;
				// cout << "Initialize phold..";
				int* phold(nullptr);
				phold = new int[MAX];
				// cout << "Copy pint to phold.";
				for (i2 = 0; i2 <= count; i2++)
					*(phold + i2) = *(pint + i2);
				// cout << "Delete pint, create an initialize pint";
				delete [] pint;
				int* pint(nullptr);
				pint = new int[MAX];
				// cout << "Reload pint from phold";
				for (i2 = 0; i2 <= count; i2++)
					*(pint + i2) = *(phold + i2);
				// cout << "delete phold";
				delete [] phold;
			}
	}	while (answer == 'y');


	int i(0);
	for (i = 0; i < MAX; i++)
	{
		if (i % 5 == 0)
			cout << endl;
		cout << setw(7) << *(pint + i);
	}
	cout << endl;
	delete [] pint;

}
On line#14 you assign memory to pint on the heap

at line#37 you delete it.

at line#38 you declare a local variable of the same name

at line#39 you assign memory to the local variable named pint

at this point the outer variable 'pint' is still not assigned to anything

at line #45 the local variable named pint goes out of scope and this is a memory leak.

So, on the 2nd iteration of the do{} loop you enter the for() loop and pint is not valid!!!!

I think you are best advised to not re-use variable names to start with!
see line # 28, you have only allocated for 5 int's but now MAX is 10, so is the error

Further to @Kulkarnisr comment above; the variable MAX should be declared as a const int, it is common practice to use UPPER CASE variable names for variables that are const. In this instance MAX should be const anyway to avoid its value changing and introducing "issues".

The whole program as it stands is crying out for a refactor. Try breaking the problem down into smaller manageable functions that only do what their name implies and nothing more.
the variable MAX should be declared as a const int, it is common practice to use UPPER CASE variable names for variables that are const.


It is common practice to capitalize macros. Constants, not so much.
Hi,
I have rewritten the program and it now works. Thanks to everyone for their help.

Bob

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
// Chapter 4 Exercise 1
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	int max(5);
	int count(4);
	int value(0);
	int* phold(nullptr);
	int i1(0);
	int i2(0);
	char answer('n');
	int* pint = new int[max];
	

	do
	{
		cout << "Enter five integers to fill array: \n";
		for (i1 = (max - 5); i1 < max; i1++)
		{
			cout << "Enter integer: ";
			cin >> pint[i1];
		}
			cout << "Do you want to continue with five more? ";
			cin >> answer;
			if (answer == 'y')
			{
				// initiate array by 5
				max += 5;
				// cout << "Initialize phold..";
				phold = new int[max];
				// cout << "Copy pint to phold.";
				for (i2 = 0; i2 <= count; ++i2)
					phold[i2] = pint[i2];
				count += 5;
				// cout << "Clear pint, copy phold to pint, initialize phold";
				delete [] pint;
				pint = phold;
				phold = nullptr;
			}	
	}	while (answer == 'y');


	int i(0);
	int total(0);
	double avg(0.0);
	cout << "Total and average by groups of five." << endl;
	for (i = 0; i < max; i++)
	{
		if (i % 5 == 0)
		{
			avg = total / 5;
			cout << " " << setw(10) << "Total: " << total << setw(10) << "Average: " << avg;
			cout << endl;
			total = 0;
		}
		cout << setw(7) << *(pint + i);
		total += *(pint + i);
	}
	avg = total / 5;
	cout << setw(10) << "Total: " << total << setw(10) << "Average: " << avg;
	cout << endl;
	cout << endl;
	delete [] pint;

}
Topic archived. No new replies allowed.