Dynamic memory - adding a new item at the end of an array

I am writing a program that reads values from a text file, (a sequence of points), stores it in a dynamic array and then prints it back. I am not allowed to use vectors.

I have been asked to ensure that initially little memory is allocated and that at each loop iteration, array max size (max_size) and element numbers (num_elements) is tracked.
However, if an insertion causes number of elements to exceed array max size, I am meant to reallocate memory as follows:

-Allocate another dynamic array w/ a greater max size
-Copy all elements from previous array to new one
-Deallocate memory area allocated for previous array
-Get pointer to previous array to point to new one
-Add new item at end of array

I'm fine with all of these (I think), except the last one. Whenever I run my code, it gives me the same values as before, but rather than adding new values to the end, it just leaves me with zeros or random numbers. Any help?

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
  #include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string.h>
#include <memory.h>
#include <stdio.h>

using namespace std;

struct point {
	double x;
	double y;
};

void Print_Array(point array[], int array_length){
//2. print out array content. Prameter: array
	
	for (int i=0; i<array_length; i++) {
		cout << array[i].x << ", " << array[i].y << '\n';
	}
	
}





int main(){

	int max_size = 10;
	int num_elements = 0;
	ifstream infile;
	infile.open("datainput.txt");
	point *pp = new (nothrow) point[max_size];
	
	
	
	for (int i = 0; !infile.eof(); i++) {
		infile >> pp[i].x >> pp[i].y;
		if(i>max_size){goto bigger; break;}
		num_elements = i;
	}
	
	
	
bigger:
	//allocate dynamic array with greater one
	max_size =  max_size*2;
	point *pp2 = new (nothrow) point[max_size];
	
	//copy all elements from previous array to new one
	for (int j = 0; j < num_elements; j++) {
		pp2[j].x = pp[j].x;
		pp2[j].y = pp[j].y;
	}
	//deallocate memory area allocated for previous array
	delete [] pp;
	
	//get pointer to previous array to the new one
	pp = &pp2[0];
	
	//add new item at end of array
//Problem section, perhaps?

	for (int k = 0; !infile.eof(); k++) {
		if(k<num_elements){continue;}
		else{infile>>pp2[k].x >>pp2[k].y;
		num_elements++}
			}
	


	Print_Array(pp2, num_elements);
	
	
	infile.close();
	
	return 0;
}
Last edited on
Ugh. use of goto. Lack of use of functions. Improper use of eof(). Multiple read loops. Unnecessary headers.

Line 42: Instead of using a goto, you should be calling a function (e.g. reallocate()) and continuing inline.

Line 44: You realize that when you fall out of the for loop at line 44, you're going to reallocate the array whether you need to or not.

Line 40-41: eof() is only going to be true after you attempt to read and there is no data. When you hit eof at line 41, you're going to execute lines 42-43 even though you didn't read anything.

Line 67: same comment regarding eof()

Line 67-71: Why do you have another read loop here? You should have a single loop to read the file. What happens if you you exceed the size of the array here?

edit:
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
#include <iostream>
#include <fstream>
using namespace std;

struct point {
	double x;
	double y;
};

//2. print out array content. Prameter: array
void Print_Array(point array[], int array_length)
{	for (int i=0; i<array_length; i++) 
	{	cout << array[i].x << ", " << array[i].y << '\n';
	}	
}

//	copy all elements from previous array to new one
void copy_array (point * dest, point * src, int num_elements)
{	for (int j = 0; j < num_elements; j++) 
	{	dest[j].x = src[j].x;
		dest[j].y = src[j].y;
	}
}

//	Reallocate the array
point * reallocate (point * pp, int num_elements, int & max_size)
{	max_size =  max_size*2;
	point *pp2 = new (nothrow) point[max_size];
	copy_array (pp2, pp, num_elements);
	delete [] pp;
	return pp2;
}

point * add_element (point * pp, double x, double y, int & num_elements, int & max_size)
{	pp[num_elements].x = x;
	pp[num_elements].y = y;
	num_elements++;
	if (num_elements >= max_size)
		pp = reallocate (pp, num_elements, max_size); 
	return pp;
}

int main()
{	int max_size = 10;
	int num_elements = 0;
	double x,y;
	ifstream infile;
	infile.open("datainput.txt");
	point *pp;
	
	pp = new (nothrow) point[max_size];
	while (infile >> x >> y) 
		pp = add_element (pp, x, y, num_elements, max_size);
	Print_Array (pp, num_elements);
	delete [] pp;
	system ("pause");
	return 0;
}  

Last edited on
To add to that
41
42
		infile >> pp[i].x >> pp[i].y;
		if(i>max_size){goto bigger; break;}
if the test in line 42 passes, then you had a bad access in line 41.
Last edited on
Thanks so much guys! Just 2 questions @AbstractionAnon

Shouldn't I worry about closing the file with infile.close(); ? And what does system ("Pause"); do?
infile.close() is not needed. infile is closed automatically by ifstream's destructor when the infile object goes out of scope.

system("pause") is a way of suspending your program before it exits. Otherwise the command shell your program is running in can exit before you see the output. Whatever is specified as the argument is passed to the command shell.

Using the system() call is discouraged as there is the opportunity for abuse. See http://www.cplusplus.com/forum/beginner/1988/


Thanks!
Topic archived. No new replies allowed.