News for hackers. Stuff that matters.
Re:Reconstant: Share constant definitions between programmin... by tempodox on Sunday March 07, 2021 @08:07AM (#26375561)
String constant generation for C is wrong. It should be

  const char X[] = "This string cannot be reassigned.";
instead of

  const char *X = "This string CAN be reassigned";
In the second case, you can do

  X = "Another value";
X wouldn't be constant, but only the value it points to. Also, it costs sizeof(char *) more memory than the actual string contents.
Re:Reconstant: Share constant definitions between programmin... by nyellin on Sunday March 07, 2021 @10:01AM (#26376303)
Hmm, I'm torn on this. Using `const char* const` can solve the first issue. Regarding the memory, `const char X[]` is obviously better but it is a little less familiar to most developers. I'm not sure if it is worth saving the bytes as the cost of clarity.
Re:Reconstant: Share constant definitions between programmin... by cjaybo on Sunday March 07, 2021 @12:19PM (#26377642)
It's surprising to me that you think char arrays might be unfamiliar to C developers!
Re:Reconstant: Share constant definitions between programmin... by nyellin on Sunday March 07, 2021 @01:51PM (#26378545)
I should have been more clear. It's not that developers are unfamiliar with the string/array equivalence in C, but rather it's less common to encode constant strings that way and I've rarely seen it done.

I can find data showing that adding the second const is uncommon. Here are the number of code results for different github searches:

"const char " - 310 million results

"const char" - 385 million results

"const char * const" - 30 million results

"const char* const" - 37 million results

"const char *const" - 36 million results

Unfortunately I'm not sure how to search for the array style syntax.

One way or another, my point is that I'm more interested in optimizing for readablity and the way people expect things to be defined rather than the extra bytes.

Re:Reconstant: Share constant definitions between programmin... by s_gourichon on Tuesday March 09, 2021 @02:51PM (#26403227)
The better syntax being uncommon is not a reason to not adopt it. Sometimes the wide majority can be unaware of the better choice.

I would rather see the change suggested as an opportunity to educate the developers.

Re:Reconstant: Share constant definitions between programmin... by UncleMeat on Monday March 08, 2021 @08:12AM (#26385314)
I don't know about C, but in C++ this is also not safe in a header file.
Re:Reconstant: Share constant definitions between programmin... by tempodox on Monday March 08, 2021 @09:54AM (#26386373)
True. Putting that in a header file will lead to linker errors if the file is included more than once.