resizing array of pointers problem

I am trying to resize an array of pointers to a String class, and an error happens at the last line (getchar is for debugging)

so I read a file, each line constructs a String class into p (the initial array), at first the code only gets the first 2 lines and constructs 2 string objects
then those values are copied into p2, while deleting each p[i]
i tried delete [] p and delete p after the for loop (after the copy process) but that was causing an error
then p is redefined (new String[4]) and the process continues until the end of the file - 4 lines alltogether
the first while prints out the first 2 lines, the second file prints out the next 2, but when i use that last piece of code, I get a run-time error, and without that line of code, when the program comes to an end (after system "pause" i still get an error saying "the stack around "p" was corrupted


this is my code, not the whole main but the part around the resizing:

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
	String*  p[2];
	while (i<2)
		  { 
			File.getline(buf,53);
			cout <<i <<" :: " <<endl;
			p[i] = new String(buf);
			cout << p[i]->getString() <<"\n";
			i++;
		  }

		String*  p2[4];

		for (i=0;i<2;i++) {p2[i] = p[i]; delete p[i];}
		
		*p = new String[10];
		for (i=0;i<2;i++) {p[i] = p2[i];}

	while (!File.eof())
		  { 
			File.getline(buf,53);
			cout <<i <<" :: " <<endl;
			p[i] = new String(buf);
			cout << p[i]->getString() <<"\n";
			i++;
		  }
	getchar();
		for (i=0; i<2; i++) cout << "i printing: "<< i << " " <<p[i]->getString() <<"\n";
Last edited on
if i take the delete p[i] part out, i only get an error at the end of the program (when it closes)
1
2
3
4
for (i=0;i<2;i++) {p2[i] = p[i]; delete p[i];}//your setting p2[i] to point to a deleted location
		
		*p = new String[10];
		for (i=0;i<2;i++) {p[i] = p2[i];}//this is an error, you are trying to assign a deleted pointer to a String object 
even when i take that out, i would get an error when closing, but i somehow got it to work
Topic archived. No new replies allowed.