Copying between two-dimensional dynamic memory

I need to write a program that will read in an unlimited number of names that are an unlimited number of characters long using dynamic memory. Things go smoothly until I get to trying to copy memory from one two-dimensional dynamic array to another.

EDIT: Solved, thanks!

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
  void readnames () // the first half is for your reference to see what I'm doing
	{
			const int firstguess (5);
			char input;
			int	numchars;
			int	arraysize;
			int arraysize2;
			char *	temp;
			char ** temp2;
			bool Continue(false);
	
	numnames = 0;
	arraysize2 = firstguess;
	names = new char * [arraysize2];
	do {
		Continue = false;
		numchars = 0;
		arraysize = firstguess;
		name = new char[arraysize + 1];
		cout << "Enter a name you want to sort: ";
		while ((input = cin.get()) != '\n')
		{
			name[numchars++] = input;
			if (numchars >= arraysize)
			{
				arraysize += firstguess;
				temp = new char[arraysize + 1];
				memcpy(temp, name, numchars);
				delete[] name;
				name = temp;
			}
		}
		name[numchars] = '\0';
		if (name[0] != '\0') // down here is where I need help
		{
			names[numnames++] = name;
			if (numnames >= (arraysize2 - 1))
			{
				arraysize2 += firstguess;
				temp2 = new char * [arraysize2];
				cout << names[numnames - 1] << endl; // to compare to what comes after copying the memory
				memcpy(temp2, names, sizeof(temp2)); // where the program seems to not do what I want
				cout << temp2 [numnames - 1] << endl; // to test that it worked, it crashes here
				delete[] names;
				names = temp2;
			}
			Continue = true;
		}
		else
			if (numnames == 0)
				names[0] = '\0';
	} while (Continue);
	}
Last edited on
sizeof(temp2) is only the size of the pointer. If you want to copy the content of names The memcpy would look like this:

memcpy(temp2, names, sizeof(temp2) * numnames);

If numnames is actually the amount to copy.
Topic archived. No new replies allowed.