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


Groups > comp.arch.embedded > #28012

Re: arm-gcc: pointer to constant string

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.arch.embedded
Subject Re: arm-gcc: pointer to constant string
Date 2018-09-14 20:56 +0200
Organization A noiseless patient Spider
Message-ID <pnh09a$ou3$1@dont-email.me> (permalink)
References <pnfp7q$a7b$1@dont-email.me> <g01ee7F9ruuU1@mid.individual.net> <png2d4$opd$1@dont-email.me> <pnh3s7.12k.1@stefan.msgid.phost.de>

Show all headers | View raw


On 14/09/18 19:57, Stefan Reuther wrote:
> Am 14.09.2018 um 12:26 schrieb David Brown:
>> On 14/09/18 11:47, Nils M Holm wrote:
>>> (A2.6) "A string has type 'array of characters' and storage class 'static'."
>>
>> No, it is not - not for decades.  In C, "string" is defined in 7.1.1 of
>> the current standard as "A string is a contiguous sequence of characters
>> terminated by and including the first null character."
> 
> That is the library definition of "string". The language definition of a
> string literal is in ยง6.4.5 which says:
> 
> # In translation phase 7, a byte or code of value zero is appended to
> # each multibyte character sequence that results from a string literal
> # or literals. The multibyte character sequence is then used to
> # initialize an array of static storage duration and length just
> # sufficient to contain the sequence. [...]
> 
> (paragraph 5 in ISO 9899:1999, paragraph 6 in n1548)

Yes, I know the difference between a string and a string literal - I 
said that in a bit you snipped.

And the library definition of a "string" /is/ the C definition of 
"string".  The term is italicised in 7.1.1, denoting that it is defined 
in that paragraph.

> 
>> In particular, there is /no/ mention of storage class, and strings are
>> independent of the storage class.
> 
> Sure, because the library does not care.
> 
>> A C compiler /can/ implement the call function "foo" in this manner :
>>
>> void foo(void) {
>> 	char s[STRING_LIT_LENGTH_HELLO];	// On stack
>> 	copy_string_literal_from_compressed_storage(&s,
>> 		STRING_LIT_IDENTIFIER_HELLO, STRING_LIT_LENGTH_HELLO);
>> 	dummy(s);
>> }
> 
> No, it can not. The literal in OP's code needs to have static storage
> duration.
> 

I know that's what the OP's code needs in order to be correct - the 
question is if the C standards guarantee that his code does what he wants.

> Maybe you're confusing it with
> 
>     void foo(void) {
>         char s[] = "hello";
>         dummy(s);
>     }
> 

No, I am not.

> This will be implemented exactly as you've sketched, but here the
> programmer explicitly said they wanted an array with automatic storage
> duration.

I know what the programmer wants, and I know what the programmer wrote. 
I know that with the compiler the programmer is using, the code he wrote 
does what he wants.  The question is if that behaviour is guaranteed by 
the C standards, or is merely the most practical implementation in most 
cases.  I cannot say for sure, but I believe the C standards do not 
guarantee the desired behaviour - I believe an implementation would be 
allowed to give the kind of implementation I described.

> 
> 
> TL;DR: OP's code is safe, language-wise.

Can you back that up with references to the standard?

> 
> Of course, this is comp.arch.embedded, and people do strange things with
> their silicon and their compilers, so I wouldn't be too surprised if
> there were a compiler that needed special measures. But ARM has a nice
> flat address space, so no problem here.
> 

I would be surprised to see an implementation - especially on an ARM - 
which did not work the way the OP wants.  A quick check with gcc on the 
AVR (where the string data would have to be copied from flash to ram) 
shows it works there too.  Perhaps someone would like to check it on 
other AVR C compilers, or more awkward processors.

Back to comp.arch.embedded | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

arm-gcc: pointer to constant string pozz <pozzugno@gmail.com> - 2018-09-14 09:50 +0200
  Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-14 11:18 +0200
    Re: arm-gcc: pointer to constant string pozz <pozzugno@gmail.com> - 2018-09-14 13:05 +0200
      Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-14 13:25 +0200
  Re: arm-gcc: pointer to constant string Nils M Holm <nmh@ananda.local> - 2018-09-14 09:47 +0000
    Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-14 12:26 +0200
      Re: arm-gcc: pointer to constant string Stef <stef33d@yahooI-N-V-A-L-I-D.com.invalid> - 2018-09-14 14:52 +0200
        Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-14 16:04 +0200
          Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-14 15:48 +0100
            Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-14 17:02 +0200
              Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-14 18:45 +0100
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-14 20:37 +0200
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-14 20:51 +0100
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-15 16:24 +0200
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-15 17:28 +0100
                Re: arm-gcc: pointer to constant string Clifford Heath <no.spam@please.net> - 2018-09-16 13:58 +1000
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-16 08:42 +0100
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-16 08:47 +0100
                Re: arm-gcc: pointer to constant string Clifford Heath <no.spam@please.net> - 2018-09-16 20:18 +1000
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-16 12:25 +0100
                Re: arm-gcc: pointer to constant string Clifford Heath <no.spam@please.net> - 2018-09-16 22:22 +1000
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-16 15:00 +0200
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-16 14:33 +0100
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-16 22:06 +0200
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-16 21:42 +0100
                Re: arm-gcc: pointer to constant string Les Cargill <lcargill99@comcast.com> - 2018-09-17 21:24 -0500
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-18 08:49 +0200
                Re: arm-gcc: pointer to constant string Les Cargill <lcargill99@comcast.com> - 2018-09-21 22:15 -0500
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-22 11:53 +0200
                Testing (Was: arm-gcc: pointer to constant string) Jacob Sparre Andersen <jacob@jacob-sparre.dk> - 2018-09-22 12:16 +0200
                Re: Testing (Was: arm-gcc: pointer to constant string) David Brown <david.brown@hesbynett.no> - 2018-09-22 13:31 +0200
                Re: Testing (Was: arm-gcc: pointer to constant string) Clifford Heath <no.spam@please.net> - 2018-09-23 09:26 +1000
                Re: arm-gcc: pointer to constant string Niklas Holsti <niklas.holsti@tidorum.invalid> - 2018-09-22 20:06 +0300
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-23 14:39 +0200
                Re: arm-gcc: pointer to constant string Les Cargill <lcargill99@comcast.com> - 2018-09-22 13:50 -0500
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-23 15:21 +0200
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-23 16:06 +0100
                Re: arm-gcc: pointer to constant string julio@diegidio.name - 2018-09-23 08:41 -0700
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-23 16:49 +0100
                Re: arm-gcc: pointer to constant string julio@diegidio.name - 2018-09-23 08:56 -0700
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-23 18:20 +0100
                Re: arm-gcc: pointer to constant string julio@diegidio.name - 2018-09-23 12:05 -0700
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-23 20:50 +0100
                Re: arm-gcc: pointer to constant string Les Cargill <lcargill99@comcast.com> - 2018-09-23 12:04 -0500
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-23 18:27 +0100
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-23 19:37 +0200
                Re: arm-gcc: pointer to constant string Jacob Sparre Andersen <jacob@jacob-sparre.dk> - 2018-09-24 12:53 +0200
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-24 13:49 +0200
                Re: arm-gcc: pointer to constant string Les Cargill <lcargill99@comcast.com> - 2018-09-28 20:17 -0500
                Re: arm-gcc: pointer to constant string Tom Gardner <spamjunk@blueyonder.co.uk> - 2018-09-18 08:32 +0100
                Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-18 10:02 +0200
                Re: arm-gcc: pointer to constant string Les Cargill <lcargill99@comcast.com> - 2018-09-21 22:17 -0500
          Re: arm-gcc: pointer to constant string Stef <stef33d@yahooI-N-V-A-L-I-D.com.invalid> - 2018-09-14 18:30 +0200
            Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-14 20:46 +0200
      Re: arm-gcc: pointer to constant string Stefan Reuther <stefan.news@arcor.de> - 2018-09-14 19:57 +0200
        Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-14 20:56 +0200
          Re: arm-gcc: pointer to constant string Stefan Reuther <stefan.news@arcor.de> - 2018-09-15 18:32 +0200
            Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-16 15:06 +0200
      Re: arm-gcc: pointer to constant string Richard Damon <Richard@Damon-Family.org> - 2018-09-14 19:59 -0400
        Re: arm-gcc: pointer to constant string David Brown <david.brown@hesbynett.no> - 2018-09-15 16:25 +0200
  Re: arm-gcc: pointer to constant string Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2018-09-14 13:49 +0300

csiph-web