Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c > #6151

Re: Return pointers-to-const where data shall not be modified

From James Kuyper <jameskuyper@alumni.caltech.edu>
Newsgroups comp.std.c
Subject Re: Return pointers-to-const where data shall not be modified
Date 2020-08-28 08:42 -0400
Organization A noiseless patient Spider
Message-ID <riau35$k90$1@dont-email.me> (permalink)
References <r8urv8$82c$1@solani.org> <87tv0szuj7.fsf@nosuchdomain.example.com> <04b46dc3-f655-4292-823c-60b04b881425n@googlegroups.com>

Show all headers | View raw


On 8/19/20 1:44 PM, Chris Peterson wrote:
> 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

No, backwards compatibility means that code which was correctly written
for an older version of C should, if compiled for a newer version of C
without modification, compile without mandatory diagnostics and with the
same behavior. Maintaining backwards compatibility is only one of
several goals the committee has attempted to achieve when developing new
versions of the C standard. Every version of the standard has had at
least a few cases where backwards compatibility has been sacrificed to
achieve one of the other goals. However, it is one of the most important
such goals.

Back to comp.std.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Return pointers-to-const where data shall not be modified Philipp Klaus Krause <pkk@spth.de> - 2020-05-06 19:25 +0200
  Re: Return pointers-to-const where data shall not be modified Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-05-06 13:18 -0700
    Re: Return pointers-to-const where data shall not be modified Chris Peterson <cpeterson@mozilla.com> - 2020-08-19 10:44 -0700
      Re: Return pointers-to-const where data shall not be modified Philipp Klaus Krause <pkk@spth.de> - 2020-08-28 11:40 +0200
      Re: Return pointers-to-const where data shall not be modified James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-08-28 08:42 -0400

csiph-web