Need help with tranferring array to other array

I don't quite get what to do with the TO DO: parts of the code, how would I copy an array to new array and copy the address of new array to pointer as well?

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
  #include <iostream>
#include "Kingdom.h"

using namespace std;
using namespace sict;

void read(Kingdom&);

int main() {
	int count = 0; // the number of kingdoms in the array
	
	Kingdom* pKingdom = nullptr; //pointer
	

	cout << "==========\n"
		<< "Input data\n"
		<< "==========\n"
		<< "Enter the number of Kingdoms: ";
	cin >> count;
	cin.ignore();

	if (count < 1) return 1;

	
	pKingdom = new Kingdom[count];
	for (int i = 0; i < count; ++i) {
		cout << "Kingdom #" << i + 1 << ": " << endl;
		cout << "Enter the name of the Kingdom: ";
		cin >> pKingdom[i].m_name;
		cout << "Enter the number of people living in " << pKingdom[i].m_name << ": ";
		cin >> pKingdom[i].m_population;
	}
	cout << "==========" << endl << endl;

	// testing that "display(...)" works
	cout << "------------------------------" << endl
		<< "The 1st kingdom entered is" << endl
		<< "------------------------------" << endl;
	display(pKingdom[0]);
	cout << "------------------------------" << endl << endl;

	// expand the array of Kingdoms by 1 element
	// TODO: allocate dynamic memory for count + 1 Kingdoms
	
	// TODO: copy elements from original array into this newly allocated array
	for (int i = 0; i < count; i++) {
		resized[i] = pKingdom[count];
	}
	// TODO: deallocate the dynamic memory for the original array
	
	// TODO: copy the address of the newly allocated array into pKingdom pointer
	// add the new Kingdom
	cout << "==========\n"
		<< "Input data\n"
		<< "==========\n"
		<< "Kingdom #" << count + 1 << ": " << endl;
	// TODO: accept input for the new element in the array
	count++;
	cout << "==========\n" << endl;

	// testing that the overload of "display(...)" works
	display(pKingdom, count);
	cout << endl;

	// TODO: deallocate the dynamic memory here

	return 0;
}

// read accepts data for a Kingdom from standard input
//
void read(Kingdom& kingdom) {
	cout << "Enter the name of the Kingdom: ";
	cin.get(kingdom.m_name, 32, '\n');
	cin.ignore(2000, '\n');
	cout << "Enter the number of people living in " << kingdom.m_name << ": ";
	cin >> kingdom.m_population;
	cin.ignore(2000, '\n');
}
Last edited on
this seems like your other question. Are you all set now?
no i don't seem to get it. If i were to allocate dynamic memory for count +1 kingdom, would I write kingdom[count+1];?
yes and no.
yes, that is the correct syntax.
No, you cannot just re-allocate a pointer as in your second post.

so if you have a pointer that you allocated and it is full of data, you cannot reallocate that. You MUST do that with these steps:
1) create a new pointer with the new size.
2) copy the data into the new pointer's location(s)
3) delete the original pointer
4) copy the address (pointer variable) of the new one to the old one.

some of the reasons for this...
every new must be paired to a delete. if you do this:
x = new something; //pretend x is now assigned memory location 1
x = new something; //pretend x is now assigned memory location 2
you lost memory location 1 and have no way to delete it because you have not stored it anywhere. That is why you need the second variable and all the juggling.

also, memory for pointers is allocated back to back and there is no way to know that if the computer gives you 10 locations that the 11th will be free to use later; something else in your program or another program could take control of that location. The only way to be SURE is to allocate 11 locations elsewhere and move the data from the 10 to the 11.

so explicitly to resize pkingdom here you need:

1
2
3
4
5
6
Kingdom *tmp = new Kingdom[count+1]; //this is your count+1 correct syntax being used correctly
for(j = 0; j < count; j++)
  tmp[j] = pKingdom[j]; //if you do not have an assignment operator for this, you have to copy each class field one by one here.

delete [] pKingdom;
pKingdom = tmp;



I can't stress enough that vectors do this for you using tools of the language so you don't have to keep reinventing, debugging, and dealing with this. Vectors do a lot of other stuff, but this feature coupled with their assignment operator are the top 2 in my eyes.


Last edited on
Topic archived. No new replies allowed.