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


Groups > comp.lang.c > #6676

Re: int * vs char *

From Ian Collins <ian-news@hotmail.com>
Newsgroups comp.lang.c
Subject Re: int * vs char *
Date 2011-06-23 10:23 +1200
Message-ID <96f8b3FmtvU10@mid.individual.net> (permalink)
References (1 earlier) <lnliwuiz7c.fsf@nuthaus.mib.org> <its93d$62e$1@dont-email.me> <itsfv5$e9l$1@dont-email.me> <96evn3FmtvU8@mid.individual.net> <ittpbt$k92$1@dont-email.me>

Show all headers | View raw


On 06/23/11 11:12 AM, Shao Miller wrote:
> On 6/22/2011 2:56 PM, Ian Collins wrote:
>> On 06/22/11 10:25 PM, James Kuyper wrote:
>>> On 06/22/2011 04:28 AM, Noob wrote:
>>> ...
>>>> Since modifying a character string literal already has UB in
>>>> the current standard, then why doesn't the next standard
>>>> specify that string literal have type const char[] instead
>>>> of just char[] ?
>>>
>>> It would break a large amount of existing code. The most common problem
>>> would be code which assigns the pointer value of a string literal to a
>>> char* object. You could argue that this is bad practice - if a pointer
>>> might be pointing at a string literal, it should be declared 'const
>>> char*', rather than 'char*'. However, there's no actual problem with
>>> such code so long as no attempt is made to write through that pointer
>>> value, and there's an awful lot of existing code which relies upon that
>>> fact.
>>
>> The existing code problem was a acknowledged when C++ changed the type
>> of string literals. Compilers may choose not to issues a diagnostic for
>> this case. Now we have had over a decade to fix the smelly code, I
>> believe a diagnostic is now required by the new C++ standard.
>>
>> C could and should have done the same, but as usual those worried about
>> breaking already broken code appear to have won the day.
>>
>
> Again, why should a C implementation be rendered non-conforming [to some
> future Standard] thusly?

Because in most cases such code is an accident waiting to happen.  It 
was amusing to see how much cruft was removed from the OpenSolaris code 
base when the default action of the native C compiler changed to use 
read only literals!  Although it's a specific example, it does 
illustrate a more general point - assuming writeable literals is 
non-portable.  All those code changes were also required to get the code 
to compile with gcc.

> In a "bare metal" environment, one might very well wish to overwrite
> their string literals' storage, no?  The "bare metal" implementation
> might need to define such action as being appropriate.

Most "bare metal" environments I have used place literals in a read only 
segment, RAM is usually a more precious (and expensive) resource than 
ROM/FLASH on embedded systems.

> By using "proper" static arrays, we lose out on the "shared storage"
> benefit.  Writing for bare metal, hopefully one knows what one is doing.

Embedded tools usually have a rich set of pragmas and link options to 
specify where various types of object live.  It's nigh on impossible to 
write a pure standard C embedded application.

> Is this example silly?

No, but it is easily worked round, tool sets with a C++ compiler already 
have to.

-- 
Ian Collins

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

int * vs char * yugandhar <nospam@nospam.com> - 2011-06-21 21:49 +0000
  Re: int * vs char * John Gordon <gordon@panix.com> - 2011-06-21 21:58 +0000
    Re: int * vs char * "Morris Keesan" <mkeesan@post.harvard.edu> - 2011-06-21 20:25 -0400
  Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-22 09:59 +1200
  Re: int * vs char * Ike Naar <ike@sverige.freeshell.org> - 2011-06-21 22:18 +0000
  Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-06-21 18:34 -0500
  Re: int * vs char * Keith Thompson <kst-u@mib.org> - 2011-06-21 15:42 -0700
    Re: int * vs char * Noob <root@127.0.0.1> - 2011-06-22 10:28 +0200
      Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-06-22 05:23 -0500
      Re: int * vs char * James Kuyper <jameskuyper@verizon.net> - 2011-06-22 06:25 -0400
        Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-23 07:56 +1200
          Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-06-22 18:12 -0500
            Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-23 10:23 +1200
            Re: int * vs char * Keith Thompson <kst-u@mib.org> - 2011-06-22 17:23 -0700
              Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-23 12:54 +1200
                Re: int * vs char * Keith Thompson <kst-u@mib.org> - 2011-06-22 18:18 -0700
                Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-23 13:23 +1200
              Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-06-24 15:39 -0400
                Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-25 09:28 +1200
                Re: int * vs char * Keith Thompson <kst-u@mib.org> - 2011-06-24 14:58 -0700
                Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-25 10:06 +1200
                Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-06-26 15:44 -0500
                Re: int * vs char * Keith Thompson <kst-u@mib.org> - 2011-06-24 14:29 -0700
                Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-25 09:36 +1200
                Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-06-26 16:24 -0500
                Re: int * vs char * Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-25 08:39 -0700
            Re: int * vs char * gordonb.5qick@burditt.org (Gordon Burditt) - 2011-06-22 23:49 -0500
      Re: int * vs char * Stephen Sprunk <stephen@sprunk.org> - 2011-06-22 11:31 -0500
        Re: int * vs char * gordonb.idn9q@burditt.org (Gordon Burditt) - 2011-06-23 00:45 -0500
          Re: int * vs char * pacman@kosh.dhis.org (Alan Curry) - 2011-06-23 20:08 +0000
          Re: int * vs char * Stephen Sprunk <stephen@sprunk.org> - 2011-06-23 22:40 -0500
            Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-24 15:46 +1200
              Re: int * vs char * Stephen Sprunk <stephen@sprunk.org> - 2011-06-24 09:24 -0500
                Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-07-04 18:00 -0400
                Re: int * vs char * Dr Nick <3-nospam@temporary-address.org.uk> - 2011-07-05 07:45 +0100
                Re: int * vs char * Harald van Dijk <truedfx@gmail.com> - 2011-07-05 10:39 -0700
                Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-07-05 14:09 -0400
                Re: int * vs char * Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-07 21:56 +0300
                Re: int * vs char * Phil Carmody <thefatphil_demunged@yahoo.co.uk> - 2011-07-08 02:46 +0300
                Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-07-07 22:26 -0400
                Re: int * vs char * Stephen Sprunk <stephen@sprunk.org> - 2011-07-07 22:41 -0500
                Re: int * vs char * "Joel C. Salomon" <joelcsalomon@gmail.com> - 2011-07-05 15:32 -0400
                Re: int * vs char * Shao Miller <sha0.miller@gmail.com> - 2011-07-06 09:58 -0400
      Re: int * vs char * Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-23 01:02 -0700
        Re: int * vs char * Ian Collins <ian-news@hotmail.com> - 2011-06-23 20:11 +1200
          Re: int * vs char * Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-25 09:23 -0700
            Re: int * vs char * Keith Thompson <kst-u@mib.org> - 2011-06-25 12:37 -0700
              Re: int * vs char * Tim Rentsch <txr@alumni.caltech.edu> - 2011-06-27 11:40 -0700
  Re: int * vs char * Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-06-21 21:46 -0400
    Re: int * vs char * "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-06-22 10:06 -0700
      Re: int * vs char * Keith Thompson <kst-u@mib.org> - 2011-06-22 13:06 -0700
        Re: int * vs char * "Kleuskes & Moos" <kleuske@xs4all.nl> - 2011-06-22 13:29 -0700
  Re: int * vs char * Stephen Sprunk <stephen@sprunk.org> - 2011-06-21 21:29 -0500

csiph-web