Should literal [const char*] be stored in one location?

70 Views Asked by At

Let's say, for example, you have a program that looks kinda like this:

QJsonArray a = data->value("my_key").toArray();
a.push_back(id);
data->insert("my_key", a);

As you can see, the literal "my_key" is inserted twice. From my understanding, this means the program will directly store the char array into the binary twice, even though they are exactly the same, thus bloating things more than they need to be.

Would it be better to do something like this:

const char* n = "my_key";
QJsonArray a = data->value(n).toArray();
a.push_back(id);
data->insert(n, a);

Or does it not really matter? Does the compiler handle things like this at a local scale? How about instead of a local case, if the same literal is used multiple times in multiple different souce files and functions. Is it still worth creating some sort of global variable?

0

There are 0 best solutions below