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


Groups > comp.lang.c > #108213

Re: How to fix: [-Wformat-nonliteral]

From David Brown <david.brown@hesbynett.no>
Newsgroups comp.lang.c
Subject Re: How to fix: [-Wformat-nonliteral]
Date 2017-04-17 18:08 +0200
Organization A noiseless patient Spider
Message-ID <od2p42$vhl$1@dont-email.me> (permalink)
References (10 earlier) <aa3e8f39-85a0-4c75-9adc-bc79d54005f5@googlegroups.com> <ocm91l$nkc$1@dont-email.me> <5ef4318f-a4f1-4be1-8e9e-bcf6071e6439@googlegroups.com> <ocq815$4ud$1@dont-email.me> <ed31f78a-0088-4cd2-a564-e107e2575f71@googlegroups.com>

Show all headers | View raw


On 14/04/17 20:57, supercat@casperkitty.com wrote:
> On Friday, April 14, 2017 at 5:27:52 AM UTC-5, David Brown wrote:
<snip>
> I've not had problems with the const-pointer treatment breaking code; I'm
> not sure how much otherwise-usable code would be broken by it.  On many
> parts, the difference in RAM and ROM sizes is sufficiently large (30:1 or
> more) that a lot of programs would be nowhere close to being able to store
> all their tables in RAM, but would still be nowhere near filling their code
> space.

If you haven't used a compiler where turning a non-const pointer into a 
const pointer breaks things, then of course you won't have had problems 
with it.  Most compilers are perfectly happy with such conversions.

>
>> The problem is non-const data accessed through const qualified pointers.
>>   And such code /is/ common:
>>
>> extern int average(int noOfSamples, const int * pSamples);
>> int x[10];
>> int av = average(10, x);
>
> On the PIC compilers, that will work without difficulty.  The act of reading
> a const pointer causes the compiler to check a bit in the pointer and either
> access RAM or ROM.  The situations the PIC has trouble with are using non-
> const pointers to read const data, or round-tripping const pointers through
> non-const pointers, but of which are much rarer than using const pointers to
> read both const and non-const data.

Those will be rarer situations, but may still mean legal code will fail.

>
> I don't know what the Atmel compiler does,

Please stop referring to this compiler as "the Atmel compiler".  It was 
a compiler written by an independent development tool company for one of 
Atmel's microcontroller families - it was /not/ a compiler from Atmel.

> but if it can't use const pointers
> to read non-const data I would agree that is an unjustifiable abuse of the
> "const" keyword which doesn't accommodate any useful scenarios that would not
> be served just as well using a different keyword.

Great, you agree with the point I made a good many posts at the start of 
this sub-thread.

>  The reason I regard the
> PIC's behavior as somewhat reasonable [albeit obviously non-conforming] is
> that it allows many programs to run efficiently without modification in a
> way which would not be possible had another keyword been chosen.

I agree that such behaviour is "somewhat reasonable".  I'd much prefer a 
command line switch, however.

> If the
> Atmel compiler's implementation doesn't offer that advantage, I would see
> no justification for re-purposing the keyword.

The compiler I was talking about allowed /some/ programs to run more 
efficiently without modification (specifically, those that had a lot of 
constant data but never accessed non-const data through a const 
pointer).  But it would silently break other programs.

>
>> A C compiler should not be made to work only with a particular
>> specialised variety of programs!  A compiler sold as a "C Compiler"
>> should be made to correctly compile as many valid C programs as possible
>> given the limitations of the target.
>
> Since only a particular specialized variety of programs could possibly
> do anything useful with 36 bytes of RAM and 1Kword of code space, it would
> be impossible to write a compiler for such a platform that *wasn't* limited
> to a particular specialized variety of programs.

That is why I added the qualification "as many as possible /given the 
limitations of the target/".

>
> More generally, most embedded compilers are intended largely for use on
> systems which don't have any defined concept of console or file I/O, and
> could thus only process usefully the particular specialized variety of
> programs that are designed to operate with whatever hardware is built into
> the target system.
>
> The advantage of using C on many such systems isn't that one can simply grab
> arbitrary bits of code from here and there and run it, but rather that a
> programmer who wants to turn on a light need only worry about:
>
>    1. Which I/O port is the light hooked up to
>
>    2. Which bits of which addresses need to be written to activate that
>       port
>

There are a good many other advantages in using C (compared to assembly, 
which I think was implied by you).  But I agree that one would not 
expect to take arbitrary existing C code and find it useful on a small 
microcontroller.

> Rather than also having to worry about the sequence of instructions that
> might be needed to perform such an access, any restrictions that might exist
> regarding the placement of such code and any data it requires, and also the
> question of what one must do to ensure that code gets put into whatever kind
> of code section is required on the target.  The placement of the light and
> the sequence of accesses needed to activate it will vary depending upon the
> hardware platform, but there's no reason the syntax for performing such
> accesses should need to.
>

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


Thread

How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-07 12:20 +0430
  Re: How to fix: [-Wformat-nonliteral] Barry Schwarz <schwarzb@dqel.com> - 2017-04-07 01:28 -0700
  Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-07 11:58 +0200
    Re: How to fix: [-Wformat-nonliteral] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-07 11:43 +0100
      Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-07 13:35 +0200
    Re: How to fix: [-Wformat-nonliteral] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-07 13:50 +0100
      Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-07 15:54 +0200
        Re: How to fix: [-Wformat-nonliteral] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-07 19:35 +0100
          Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-08 13:41 +0200
            Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-10 13:52 -0700
              Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-11 00:18 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-10 15:43 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-10 16:10 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-10 16:26 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-11 11:33 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-11 08:37 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-13 00:20 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-12 15:55 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-12 18:17 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-14 12:27 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-14 11:57 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-17 18:08 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-17 09:38 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-12 18:13 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 07:37 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-13 08:43 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 11:24 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-13 11:49 -0700
                Re: How to fix: [-Wformat-nonliteral] Robert Wessel <robertwessel2@yahoo.com> - 2017-04-13 14:43 -0500
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 13:26 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-13 13:32 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 14:27 -0700
                Re: How to fix: [-Wformat-nonliteral] Robert Wessel <robertwessel2@yahoo.com> - 2017-04-13 17:39 -0500
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 16:16 -0700
                Re: How to fix: [-Wformat-nonliteral] Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-16 17:53 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-13 13:19 -0700
                Re: How to fix: [-Wformat-nonliteral] Tim Rentsch <txr@alumni.caltech.edu> - 2017-04-16 17:49 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-17 18:14 +0200
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-17 11:54 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-17 22:41 +0200
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-17 15:02 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-14 12:40 +0200
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-14 12:10 -0700
                Re: How to fix: [-Wformat-nonliteral] Keith Thompson <kst-u@mib.org> - 2017-04-14 13:22 -0700
                Re: How to fix: [-Wformat-nonliteral] supercat@casperkitty.com - 2017-04-14 17:14 -0700
                Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-13 00:20 +0200
      Re: How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-07 20:29 +0430
        Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-07 18:42 +0200
          Re: How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-08 08:04 +0430
            Re: How to fix: [-Wformat-nonliteral] Richard Heathfield <rjh@cpax.org.uk> - 2017-04-08 07:51 +0100
              Re: How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-08 12:11 +0430
                Re: How to fix: [-Wformat-nonliteral] Richard Heathfield <rjh@cpax.org.uk> - 2017-04-08 11:38 +0100
                Re: How to fix: [-Wformat-nonliteral] Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-04-08 18:52 +0100
            Re: How to fix: [-Wformat-nonliteral] David Brown <david.brown@hesbynett.no> - 2017-04-08 13:48 +0200
              Re: How to fix: [-Wformat-nonliteral] MehdiAmini <atorrses@gmail.com> - 2017-04-08 20:34 +0430

csiph-web