0

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?

Griffort
  • 1,174
  • 1
  • 10
  • 26
  • [c++ - Two string literals have the same pointer value? - Stack Overflow](https://stackoverflow.com/questions/13515561/two-string-literals-have-the-same-pointer-value) – user202729 Aug 12 '18 at 14:48
  • That depends on how the `QJsonArray` stores keys, if it stores by pointers or by some object. If it only stores and uses the pointer as a key, then in most compilers-implementations both would be equal since most compilers usually optimizes string literals by storing only *one*. – Some programmer dude Aug 12 '18 at 14:48
  • @user202729 I’m using gcc, specifically MinGW-w64 on Windows. I’ll add that to the post. – Griffort Aug 12 '18 at 14:49
  • 1
    As an experiment, try e.g. `int main() { char const* a = "my_key"; std::cout << static_cast(a) << ' ' << static_cast("my_key") << '\n'; }`. See if the program prints the same address for both pointers. – Some programmer dude Aug 12 '18 at 14:50
  • 1
    @Someprogrammerdude They’re stored using the value, I believe, since it technically takes a QString that has an explicit constructor for const char* – Griffort Aug 12 '18 at 14:51
  • @Someprogrammerdude Sounds good, I’ll try that. – Griffort Aug 12 '18 at 14:51
  • Then it really depends on the implementation of `QString`, how much it itself handles string duplication. However, it just feels like you're attempting premature optimization, which is usually bad. Try to write good, readable, understandable, maintainable and *working* code first of all. Unless you're on an extremely memory-constrained system (where Qt might not be a good choice to begin with) then as long as it's "good enough" (which often *is* good enough) don't bother with such micro-management unless it required to or you're measuring it to be needed. – Some programmer dude Aug 12 '18 at 14:57
  • @Someprogrammerdude Wow, perfect. The local, global, and literal values all returned the same address, so I guess the compiler has it down. Definitely make that an answer if you want. I understand that this may be a bit too much in terms of management, but was pretty curious since I saw a bunch of the same literals everywhere. And, well, always nice to have more knowledge regarding this. – Griffort Aug 12 '18 at 15:01
  • Identical code/constant folding is an optimization most compilers are pretty good at. Especially if you build with link time optimization. – Jesper Juhl Aug 12 '18 at 15:09
  • This doesn’t address the question, but “literal [const char*]” isn’t correct. The type of a string literal (such as `”my_key”`) is **array** of `char`. In most contexts its type **decays** to `const char*`, but the two types are not the same. – Pete Becker Aug 13 '18 at 12:08

0 Answers0