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


Groups > sci.electronics.design > #447544

Re: Arduino / compilers on uC

From David Brown <david.brown@hesbynett.no>
Newsgroups sci.electronics.design
Subject Re: Arduino / compilers on uC
Date 2017-02-13 15:35 +0100
Organization A noiseless patient Spider
Message-ID <o7sg49$pda$1@dont-email.me> (permalink)
References (4 earlier) <d0e5d45c-d759-41e1-a190-7580b47c5d9e@googlegroups.com> <o7c3tl$72a$1@dont-email.me> <e649362c-6a9c-4397-b7ac-2ae1a79e45aa@googlegroups.com> <o7ev1u$cod$1@dont-email.me> <3e6134b9-7c67-461e-919e-2b6c459d4ab4@googlegroups.com>

Show all headers | View raw


On 09/02/17 17:36, dagmargoodboat@yahoo.com wrote:
> On Wednesday, February 8, 2017 at 6:24:32 AM UTC-5, David Brown wrote:
>> On 07/02/17 16:39, dagmargoodboat@yahoo.com wrote:
>>> On Tuesday, February 7, 2017 at 4:29:06 AM UTC-5, David Brown wrote:
<snip>
>> If U.dat.flg is declared as "bool", then the compiler knows that
>> U.dat.flg is already a properly initialised bool.  You would not want
>> the compiler to generate a series of unnecessary instructions doing
>> tests and jumps simply to assign from one bool to another.
>>
>> You lied to your compiler, albeit unknowingly and unintentionally.
>>  It bit you.  That's what happens when you lie to your compiler.
> 
> I used a 'bool' (then a 'boolean') to represent a boolean quantity. Isn't
> that the point of having a boolean type?

That is the point of "bool", yes.  But you took something that you did
not know to be a valid bool, and told the compiler that it /was/ a valid
bool - that's a lie.

An alternative way to think about this is that the data in the eeprom is
external data from an unknown source.  You don't know it is valid until
you have checked it.

If you are writing a web application, and it asks the user for a name,
you don't assume that they have entered a valid name.  You /check/ the
entered data - you don't pass it on to a database query unless you want
to create an SQL injection attack.  The data in the eeprom is the same -
it is unchecked unknown external data, and you have to confirm that it
fits your requirements before using it.  You can't simply /declare/ to
the compiler that it fits your requirements - you have to check it.

> 
> If the compiler's code absolutely depends on a variable having certain
> values and no others, the compiler should take steps to ensure that the
> variable contains a legal value.  

The compiler /will/ take steps to make sure that a bool variable holds a
legal value - it does so every time it stores something that is not
necessary legal.  So if you have a uint8_t item that you are storing
into a bool, the compiler will convert any non-zero value into 1 before
storing it.  But in your code, you have told the compiler that you
already have a checked, valid bool value - so a simple copy is all the
compiler needs to do to be absolutely sure the bool is valid.

> Instead, it prevented me initializing
> the variable to a known condition.

It removed code that was clearly unnecessary based on the information
you gave the compiler.

> 
> That's a common circumstance--variables aren't initialized to known
> conditions when power's applied.

Variables (with program lifetime) are /always/ initialized to a known
condition at the start of main(), as far as C is concerned.  They are
either initialised to an explicit value, or implicitly to 0.  If the
startup code (as run between reset and main()) does not do this, then
that code is lying to the compiler.  (I have seen tools that do this -
it's a serious PITA.)  Local stack data and heap data are not
initialized, and the compiler knows this too.

> 
> What's the value in having a 'boolean' if you have to ultimately use
> bizarre, unsigned, faked, phony types to ultimately represent it with
> a byte anyhow?

You don't have to do anything of the sort.  You simply have to treat
external unknown data as having any value that can physically fit in the
space.  So if you are mapping data from an eeprom, then use uint8_t for
the bytes.  Then you can assign that to a bool type if you want.

Alternatively, you can have a mechanism that checks and corrects the
eeprom data once, then you know it is correct.  For example, at program
startup you could read the eeprom data as a uint8_t, and if it is not a
valid bool, then write a 0 or 1 to it.  For my eeprom data, I will often
have a checksum to be sure that the data is valid - when starting up, I
verify the checksum.  If it is correct, then my eeprom data is safe to
use (bools, enums, and all).  If it is incorrect, then I fill the eeprom
structure with a valid default set of values.

> 
>>>> Similar reasoning gives you the rest of your results.
>>>>
>>>>
>>>> The answer is to declare U.dat.flg as "uint8_t", since it can hold any
>>>> value in that range.
>>>
>>> Already done & described in posts above.
>>>
>>
>> Yes, but hopefully you now understand that the compiler did not make any
>> mistakes - the error was in your code.  And you know that you have not
>> just "found a workaround" - you understand the problem, and understand
>> the correction to your code error.
> 
> No, the compiler *is* at fault.  It optimized out the operation
> 
>  w = (U.dat.flg ? true : false;)
> 
> substituting: U.dat.flg ==> w.
> 
>           1          2         3
>  w = (U.dat.flg) ? (true) : (false);
> 
> To do that is to assume expression #1 (U.dat.flg != 0) is equivalent to, has
> the same range of values as, and is interchangeable with expressions of type
> 'boolean' #2 & #3.
> 
> That's wrong.
> 

You have told the compiler that U.dat.flg contains either 0 or 1 - you
did this when you declared its type to be "bool".  You are asking the
compiler to take this value, and if it is 1 then it should store 1 in w,
and if it is 0 then it should stor 0 in w.  Implicitly, you are also
saying that if U.dat.flg is /not/ 0 or 1, then the compiler can do what
it wants - including launching nasal daemons.  That's how programming
works - it is a contract between the programmer and the compiler.  When
you break your side of the bargain, don't expect the compiler to perform
miracles.


Back to sci.electronics.design | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 15:43 -0800
  Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-07 10:58 +1100
    Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-06 19:09 -0500
    Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 18:30 -0800
      Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-06 21:43 -0500
        Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 19:02 -0800
          Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-06 23:26 -0500
            Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 21:05 -0800
      Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-07 13:55 +1100
        Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 20:00 -0800
          Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-07 19:31 +1100
          Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-07 10:29 +0100
            Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-07 07:39 -0800
              Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-08 12:24 +0100
                Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-09 08:36 -0800
                Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-10 08:15 +1100
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-13 15:37 +0100
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-13 15:35 +0100
          Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-07 08:05 -0800
            Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-08 12:30 +0100
  Re: Arduino / compilers on uC mrdarrett@gmail.com - 2017-02-06 15:59 -0800
  Re: Arduino / compilers on uC Lasse Langwadt Christensen <langwadt@fonz.dk> - 2017-02-06 16:04 -0800
    Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 18:41 -0800
      Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-07 14:08 +1100
        Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 20:14 -0800
          Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-07 08:00 +0000
            Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-09 05:42 -0800
              Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-09 15:07 +0100
                Re: Arduino / compilers on uC Lasse Langwadt Christensen <langwadt@fonz.dk> - 2017-02-09 09:54 -0800
              Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-09 15:55 +0000
                Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-09 08:46 -0800
                Re: Arduino / compilers on uC Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2017-02-09 14:42 -0500
                Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-11 06:54 -0800
                Re: Arduino / compilers on uC pcdhobbs@gmail.com - 2017-02-11 07:31 -0800
                Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-11 07:59 -0800
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-11 17:01 +0000
                Re: Arduino / compilers on uC pcdhobbs@gmail.com - 2017-02-11 12:52 -0800
                Re: Arduino / compilers on uC krw@notreal.com - 2017-02-11 18:46 -0500
                Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-12 10:20 +1100
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-13 16:07 +0100
                Re: Arduino / compilers on uC Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2017-02-09 14:16 -0500
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-09 22:39 +0000
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-13 16:06 +0100
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-13 15:29 +0000
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-13 16:49 +0100
                Re: Arduino / compilers on uC Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2017-02-13 10:55 -0500
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-13 17:09 +0000
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-14 09:49 +0100
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-13 16:07 +0000
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-14 10:01 +0100
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-14 09:49 +0000
                Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-17 04:21 +1100
                Re: Arduino / compilers on uC pcdhobbs@gmail.com - 2017-02-16 14:47 -0800
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-17 00:17 +0000
          Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-07 19:49 +1100
            Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-09 06:22 -0800
          Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-07 10:42 +0100
            Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-09 06:57 -0800
              Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-09 16:12 +0100
              Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-10 08:24 +1100
  Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-06 19:08 -0500
    Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 18:47 -0800
      Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-06 21:51 -0500
        Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 20:39 -0800
          Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 20:55 -0800
            Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-07 20:05 +1100
              Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-08 12:26 -0500
                Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-08 12:32 -0500
                Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-09 08:31 +1100
                Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-09 08:21 +1100
                Re: Arduino / compilers on uC Lasse Langwadt Christensen <langwadt@fonz.dk> - 2017-02-08 13:33 -0800
          Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-07 08:15 +0000
            Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-09 05:51 -0800
              Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-09 16:22 +0000
                Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-09 16:45 -0500
                Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-09 16:51 -0500
                Re: Arduino / compilers on uC Clifford Heath <no.spam@please.net> - 2017-02-10 09:11 +1100
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-09 22:45 +0000
                Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-15 10:11 -0500
          Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-07 10:57 +0100
      Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-07 08:07 +0000
        Re: Arduino / compilers on uC rickman <gnuarm@gmail.com> - 2017-02-07 04:01 -0500
          Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-07 09:40 +0000
            Re: Arduino / compilers on uC rickman <gnuarm@gmail.com> - 2017-02-07 05:52 -0500
              Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-07 11:26 +0000
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-07 12:58 +0100
                Re: Arduino / compilers on uC Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2017-02-09 14:52 -0500
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-09 22:47 +0000
              Re: Arduino / compilers on uC bitrex <bitrex@de.lete.earthlink.net> - 2017-02-09 14:00 -0500
                Re: Arduino / compilers on uC Tim Wescott <seemywebsite@myfooter.really> - 2017-02-09 13:24 -0600
      Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-07 10:52 +0100
    Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-07 10:50 +0100
  Re: Arduino / compilers on uC rickman <gnuarm@gmail.com> - 2017-02-06 19:49 -0500
  Re: Arduino / compilers on uC "David Eather" <eather@tpg.com.au> - 2017-02-07 12:45 +1000
    Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-06 18:51 -0800
      Re: Arduino / compilers on uC "David Eather" <eather@tpg.com.au> - 2017-02-08 06:35 +1000
        Re: Arduino / compilers on uC "David Eather" <eather@tpg.com.au> - 2017-02-08 06:44 +1000
      Re: Arduino / compilers on uC M Philbrook <jamie_ka1lpa@charter.net> - 2017-02-07 17:11 -0500
        Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-08 12:40 +0100
          Re: Arduino / compilers on uC pcdhobbs@gmail.com - 2017-02-08 07:01 -0800
            Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-08 17:25 +0100
              Re: Arduino / compilers on uC jack4747@gmail.com - 2017-02-09 06:51 -0800
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-09 16:41 +0100
                Re: Arduino / compilers on uC M Philbrook <jamie_ka1lpa@charter.net> - 2017-02-09 19:44 -0500
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-13 16:14 +0100
                Re: Arduino / compilers on uC John Devereux <john@devereux.me.uk> - 2017-02-16 12:57 +0000
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-16 14:11 +0000
                Re: Arduino / compilers on uC John Devereux <john@devereux.me.uk> - 2017-02-16 14:40 +0000
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-16 15:58 +0100
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-16 15:56 +0000
                Re: Arduino / compilers on uC John Devereux <john@devereux.me.uk> - 2017-02-16 16:45 +0000
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-16 17:18 +0000
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-17 14:59 +0100
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-17 14:24 +0000
                Re: Arduino / compilers on uC Tauno Voipio <tauno.voipio@notused.fi.invalid> - 2017-02-17 21:07 +0200
                Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-17 20:16 +0000
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-17 23:28 +0100
                Re: Arduino / compilers on uC Phil Hobbs <pcdhSpamMeSenseless@electrooptical.net> - 2017-02-17 10:21 -0500
                Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-17 16:34 +0100
                Re: Arduino / compilers on uC M Philbrook <jamie_ka1lpa@charter.net> - 2017-02-09 19:43 -0500
  Re: Arduino / compilers on uC Tim Wescott <tim@seemywebsite.com> - 2017-02-10 10:41 -0600
    Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-11 07:06 -0800
      Re: Arduino / compilers on uC Tom Gardner <spamjunk@blueyonder.co.uk> - 2017-02-11 17:09 +0000
    Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-13 16:28 +0100
  Re: Arduino / compilers on uC Tim Wescott <tim@seemywebsite.com> - 2017-02-14 09:59 -0600
    Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-14 13:46 -0800
      Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-15 10:04 +0100
        Re: Arduino / compilers on uC dagmargoodboat@yahoo.com - 2017-02-15 05:57 -0800
          Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-15 16:02 +0100
            Re: Arduino / compilers on uC David Brown <david.brown@hesbynett.no> - 2017-02-15 17:15 +0100
          Re: Arduino / compilers on uC Tim Wescott <tim@seemywebsite.com> - 2017-02-16 14:19 -0600

csiph-web