lundi 14 août 2023

Why gcc does not create a new variable?

I have such code:

static const char kFmt[] = "some string: - %s";
char buf[sizeof(kFmt) + 10];
snprintf(buf, sizeof(buf), kFmt, "string");

It is look like arm gcc 6.3.0 does not create addition char array kFmt in RAM and just pass the pointer to string literal (that store in flash memory in my case) to snprintf. But if I remove const and have static char kFmt[] I can see that additional array is created in RAM and then passed to snprintf. It is not a problem but I wounder if this behavior is described somewhere in C++ standard or it is compiler optimization and it might differ in different compilers?

Aucun commentaire:

Enregistrer un commentaire