Philipp Klaus Krause <p...@spth.de> writes:...
There are 4 functions (getenv, localeconv, setlocale, strerror) in the standard library that return a pointer and state that the return value points to something that "shall not be modified by the program". I
suggest to make these functions return pointers-to-const, e.g.
It would likely break existing code, for the same reason that making
string literals const would break existing code.
Any program that has something like:
char *username = getenv("USER");
would have to be modified. That would include any pre-ANSI code that
calls getenv.
Would limiting the const function prototypes to C2x code (gcc -std=c2x) address this concern about backwards compatibility? Old code that wants the non-const function prototypes can ask for an older C version (e.g. gcc -std=c18).
On Wednesday, May 6, 2020 at 1:18:54 PM UTC-7, Keith Thompson wrote:
Philipp Klaus Krause <p...@spth.de> writes:...
There are 4 functions (getenv, localeconv, setlocale, strerror) in the
standard library that return a pointer and state that the return value
points to something that "shall not be modified by the program". I
suggest to make these functions return pointers-to-const, e.g.
It would likely break existing code, for the same reason that making
string literals const would break existing code.
Any program that has something like:
char *username = getenv("USER");
would have to be modified. That would include any pre-ANSI code that
calls getenv.
Would limiting the const function prototypes to C2x code (gcc -std=c2x) address this concern about backwards compatibility? Old code that wants the non-const function prototypes can ask for an older C version (e.g. gcc -std=c18).
#if __STDC_VERSION__ > 201710L
const char *getenv(const char *name);
#else
char *getenv(const char *name);
#endif
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 913 |
Nodes: | 10 (1 / 9) |
Uptime: | 188:20:44 |
Calls: | 12,143 |
Calls today: | 2 |
Files: | 186,515 |
Messages: | 2,231,605 |