How do compilers manage a dynamic type in a static

How do compilers manage a dynamic user-defined (eg. string) type in a static array type. How to perceive and explain their working compile mechanism ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#include <array>
#include <iostream>
using namespace std;

int main(){

	string k[9];
	
	k[1]="aaa";
	k[2]="ooo";
	k[3]="uuu";

	cout<< "k is static array for dynamic length variable ( ?? simply works), how is its mechanism understanding? \n"<< k[1]<<" "<<k[2]<<" "<<k[3]<<"\n";

}
Last edited on
What do you think the problem is?
The string data does not need to fit into the static array.
The string data is stored dynamically on the heap.
Basically only pointers to the data are kept in the static array.
It's a little more complicated due to "short string optimisation", but that's the general idea.
Topic archived. No new replies allowed.