jeudi 25 novembre 2021

Is it possible for different

If I initialize several const char* variables in the following ways:

const char* myString1 = "string content 1";
const char* myString2 = "string content 2";
...

Since const char* is simply a pointer a specific char object, it does not contain any size or range information of the character array it is pointing to.

So, is it possible for two string literals to overlap each other? (The newly allocated overlap the old one)

By overlap, I mean the following behaviour;

// Continue from the code block above
std::cout << myString1 << std::endl;
std::cout << myString2 << std::endl;

It outputs

string costring content 2
string content 2

So the start of myString2 is somewhere in the middle of myString1.

How does C++/compiler avoid such problem?

If I change const char* to const char[], is it still the same?

Aucun commentaire:

Enregistrer un commentaire