What happens to pointer if variable is reallocated?

what happens to pointer pt when string s is reallocated to accommodate bigger size? does it updates itself or does it points to previous s which is not used anymore?
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
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s= "aa";
	
	char *pt;
	pt = &s[0];
	
	cout << &pt << endl;
	cout << pt << endl;
	cout << &s[0] << endl;
	cout << s.capacity() << endl;
	cout << s.length() << endl;
	
	
	cout << "\n\n\n";
	s = "fcsfcbjk ewgoeig egoeginewg rbbr";
	
	cout << &pt << endl;
	cout << pt << endl;
	cout << &s[0] << endl;
	cout << s.capacity() << endl;
	cout << s.length() << endl;
	
	cout << "\n\n\n";
	pt = &s[0];
	cout << &pt << endl;
	
	return 0;
}
does it points to previous s which is not used anymore
This. Do not expect anything magical happens. The data pt points to might become invalid which leads to undefined behavior if accessed (a crash is likely)
Topic archived. No new replies allowed.