Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Tim Rentsch Newsgroups: comp.lang.c Subject: Re: does char *str="abcd"; alloc addressable memory? Date: Thu, 09 Jul 2020 10:13:20 -0700 Organization: A noiseless patient Spider Lines: 41 Message-ID: <86fta0fw7j.fsf@linuxsc.com> References: <87k0zgbiwt.fsf@nosuchdomain.example.com> <6HCNG.37756$bQ4.31924@fx04.iad> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="c87a676e1fef5793b5d15a831e4eb134"; logging-data="30408"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+WIawVxVmaCJLmqN/DYz0vJSl0nVJZpB0=" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:9SSRt7a03vqOFOHAPY7VAkHrPvo= sha1:cNcD88rOWkbwu/343+bnDBYvltQ= Xref: csiph.com comp.lang.c:153163 Richard Damon writes: > On 7/7/20 3:35 AM, David Brown wrote: > >> On 07/07/2020 05:05, John Forkosh wrote: >> >>> Keith Thompson wrote: >>> >>>> >>>> Some compilers have non-conforming options to treat string literals as >>>> const. (gcc has "-Wwrite-strings".) >>> >>> Wasn't aware of that warning, and sure enough... >>> bash-5.0$ cc -Wwrite-strings tester.c -o tester >>> tester.c: In function 'main': >>> tester.c:7:15: warning: initialization discards 'const' qualifier >>> from pointer target type [-Wdiscarded-qualifiers] >>> 7 | char *str = ( argc>1? argv[1] : "01234&56789" ); >>> | ^ >>> And (as suggested downthread) changing line 7 to >>> char *str = ( argc>1? argv[1] : (char *)"01234&56789" ); >>> eliminates the warning, but the program still segfaults. >>> >>> So, the really nice -switch would be one that reverts to the >>> classical behavior where "string literals" aren't const. >>> Any way to do that? >> >> What do you mean by "classical behaviour" ? Writing to string literals >> has been undefined behaviour since C was standardised, and AFAIK it was >> not explicitly allowed in pre-standard C. >> >> You can't expect a switch to allow something that has been considered an >> extremely bad idea since the beginnings of C ! > > My memory was that in the 80s, there were implementations that defined > that it would work, and implementation definitions has always been the > highest authority, when working on that implementation. Much more recently than that, gcc had an option -fwriteable-strings. That option has since been removed, but it was still available in the last ten years or so.