Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.std.c > #6145 > unrolled thread
| Started by | Philipp Klaus Krause <pkk@spth.de> |
|---|---|
| First post | 2020-08-14 11:34 +0200 |
| Last post | 2020-08-17 15:23 +0200 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.std.c
Making the element type of string literals const Philipp Klaus Krause <pkk@spth.de> - 2020-08-14 11:34 +0200
Re: Making the element type of string literals const David Brown <david.brown@hesbynett.no> - 2020-08-14 13:43 +0200
Re: Making the element type of string literals const Florian Weimer <fw@deneb.enyo.de> - 2020-08-14 23:41 +0200
Re: Making the element type of string literals const David Brown <david.brown@hesbynett.no> - 2020-08-17 15:23 +0200
| From | Philipp Klaus Krause <pkk@spth.de> |
|---|---|
| Date | 2020-08-14 11:34 +0200 |
| Subject | Making the element type of string literals const |
| Message-ID | <rh5lrt$m1k$1@solani.org> |
C does not allow programs to modify a string literal.
However, for historic reasons this is not reflected in their type.
E.g. this compiled, but has undefined behaviour if ever executed:
void f(void)
{
"test"[2] = 'a';
}
It would be more consistent is the element type for string literals
would be const-qualified. And coding standards (e.g. MISRA) require the
use of const-qualified types already in e.g.
const char *c = "test"; // OK
vs.
char *c = "test"; // Allowed by C standard, not allowed by MISRA
I don't think this change would break many recent programs. So even if
the type of char string literals cannot be changed, such a change should
at least be possible for string literals of other types.
Is it worth writing a proposal for C2X?
What do you think of such a change?
Philipp
[toc] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2020-08-14 13:43 +0200 |
| Message-ID | <rh5td1$pb1$1@dont-email.me> |
| In reply to | #6145 |
On 14/08/2020 11:34, Philipp Klaus Krause wrote:
> C does not allow programs to modify a string literal.
>
> However, for historic reasons this is not reflected in their type.
>
> E.g. this compiled, but has undefined behaviour if ever executed:
>
> void f(void)
> {
> "test"[2] = 'a';
> }
>
> It would be more consistent is the element type for string literals
> would be const-qualified. And coding standards (e.g. MISRA) require the
> use of const-qualified types already in e.g.
>
> const char *c = "test"; // OK
> vs.
> char *c = "test"; // Allowed by C standard, not allowed by MISRA
>
> I don't think this change would break many recent programs. So even if
> the type of char string literals cannot be changed, such a change should
> at least be possible for string literals of other types.
>
> Is it worth writing a proposal for C2X?
> What do you think of such a change?
>
> Philipp
>
It would break many programs (perhaps more old ones than new ones, but
backwards compatibility is king). In particular, you can't then use a
string literal as a parameter to a function that has a "char *" argument.
Baring obtuse use of _Generic, I believe that a program that is correct
if string literals have "const char[]" types (like in C++) will have the
same functionality with normal "char[]" types - so the change would
affect the ease of error avoidance and checking rather than the effect
of the code.
So personally I think it would be a good thing - but I strongly doubt it
could happen.
The best alternative is to have it as a compiler switch. gcc's
"-fwrite-strings" option does exactly this, and I use it in my own code.
(I disagree with the naming - as it affects the semantics of the
language, it should not be a "warning" option - but that's another matter.)
[toc] | [prev] | [next] | [standalone]
| From | Florian Weimer <fw@deneb.enyo.de> |
|---|---|
| Date | 2020-08-14 23:41 +0200 |
| Message-ID | <877du0oqgv.fsf@mid.deneb.enyo.de> |
| In reply to | #6146 |
* David Brown: > The best alternative is to have it as a compiler switch. gcc's > "-fwrite-strings" option does exactly this, and I use it in my own code. > (I disagree with the naming - as it affects the semantics of the > language, it should not be a "warning" option - but that's another matter.) The historic -fwritable-strings option actually made string literals writable by placing them into the data segment.
[toc] | [prev] | [next] | [standalone]
| From | David Brown <david.brown@hesbynett.no> |
|---|---|
| Date | 2020-08-17 15:23 +0200 |
| Message-ID | <rhe0bn$68q$3@dont-email.me> |
| In reply to | #6147 |
On 14/08/2020 23:41, Florian Weimer wrote: > * David Brown: > >> The best alternative is to have it as a compiler switch. gcc's >> "-fwrite-strings" option does exactly this, and I use it in my own code. >> (I disagree with the naming - as it affects the semantics of the >> language, it should not be a "warning" option - but that's another matter.) > > The historic -fwritable-strings option actually made string literals > writable by placing them into the data segment. > Sorry, I meant "-Wwrite-strings" ! I don't know how I managed to write exactly the opposite flag.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.std.c
csiph-web