Re: how cast works?

Liste des GroupesRevenir à l c 
Sujet : Re: how cast works?
De : bc (at) *nospam* freeuk.com (Bart)
Groupes : comp.lang.c
Date : 10. Aug 2024, 12:03:02
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v97dsn$i03p$1@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11
User-Agent : Mozilla Thunderbird
On 09/08/2024 18:08, David Brown wrote:
On 09/08/2024 02:56, Bart wrote:
On 08/08/2024 23:32, David Brown wrote:
On 08/08/2024 21:09, Bart wrote:
>
Sorry but my function is perfectly valid. It's taking a Bool value and converting it to an int.
>
No, it is not.
>
Attempting to use the value of a non-static local variable that has not been initialised or assigned is undefined behaviour.  Your function is garbage.  No one can draw any conclusions about how meaningless code is compiled.
>
FFS. You really think that makes a blind bit of difference?
 Yes, I do.
 
A variable is not initialised, so the bool->int code shown must be just random rubbish generated by the compiler? You think I wouldn't spot if there was something amiss?
 You wrote C code that had something amiss!
 How often are you going to do this?  Write some piece of meaningless crap with undefined behaviour or - at best - no observable behaviour at all, compile with silly choices of flags, and then make nonsensical claims about what compilers do or how C is defined?
What did I get wrong about that particular conversion?
I write such fragments of code for my own compilers a hundred times a day.
It only seems to cause a hoo-hah with your favourite compiler.
As for UB, what exactly is undefined about:

   void DB(void) {
       _Bool b=false;
       int i;
       i=b;
   }

There is a difference in the code.  One (BC) has no defined behaviour. The other (DB) has defined behaviour with no observable behaviour.
Ah. No observable behaviour.
This is indeed a problem with an optimising compiler, because it is in that mode that a compiler will detect that your code has no observable effects (other than in half a dozen ways I could mention, such as looking at the generated code!), and decides it's not going to bother producing it.
This is why I recommend -O0 with such compilers. Or better, use a simpler compiler.
(Mine for example have absolute no problem with meaningless fragments, so long as they satisfy language rules. They will even produce comment lines like these:
  ;------------------------------
   ...
  ;------------------------------
to more easily isolate a function body from entry/exit code.
For something available to everyone, then Tiny C is available on godbold.org.)

not a surprise that a compiler generates no code for either of them.
 
So, even initialised, it tells me nothing about what might be involved in bool->int conversion. It is useless.
>
 Agreed.  Nobody suggested your code above as a good idea.
 
Now I'll try it with -O0 (line breaks added):
>
BC:
         push    rbp
         mov     rbp, rsp
>
         movzx   eax, BYTE PTR [rbp-1]
         mov     DWORD PTR [rbp-8], eax
>
         nop
         pop     rbp
         ret
DB:
         push    rbp
         mov     rbp, rsp
         mov     BYTE PTR [rbp-1], 0
>
         movzx   eax, BYTE PTR [rbp-1]
         mov     DWORD PTR [rbp-8], eax
>
         nop
         pop     rbp
         ret
>
Exactly the same code, except DB has an extra line to initialise that value.
>
Are you surprised it is the same? I am 99% sure that you already knew this, but were pretending that the code was meaningless, for reasons that escape me.
>
 Instead of trolling with what you know, without doubt, are pointless straw men, why not apply a little thought and write functions that make sense?
When you are testing code fragments, they DON'T make sense. Hence -O0 to avoid having to soft-soap or cajole the compiler into producing relevant code.
It's to make life easier not harder. The only thing with -O0 is to learn to disregard the irrelevant bits. But at least YOU get to say what is irrelevant.

I'm giving up trying to help you - at least until you show some hint of trying to learn.
What the fuck are you on about?
I'm suggesting using -O0 because it is easier: you write code fragments without have to write a complete, meaningful program that has observable behaviour.
Which is quite hard to do; benchmaking code from an optimising compiler can be challenging, since it is easy for it to circumvent the very task you're trying to measure. Apparently, program runtime does not count as 'observable behaviour' so it is not something that attempt is made to preserve.

  I will still make posts pointing out when you write nonsense that might confuse or mislead others, but I'll stop trying to explain things unless you specifically ask.
So will I, to other people.
The set of tests posted by MS, compiler with optimising options with Clang, shed no light on bool to int conversion.

Date Sujet#  Auteur
7 Aug 24 * how cast works?122Thiago Adams
7 Aug 24 +* Re: how cast works?7Thiago Adams
7 Aug 24 i+- Re: how cast works?1Keith Thompson
12 Aug 24 i`* Re: how cast works?5Tim Rentsch
12 Aug 24 i `* Re: how cast works?4Vir Campestris
12 Aug 24 i  `* Challenge/exercise problem - signum() function3Tim Rentsch
12 Aug 24 i   `* Re: Challenge/exercise problem - signum() function2Lew Pitcher
12 Aug 24 i    `- Re: Challenge/exercise problem - signum() function1Tim Rentsch
7 Aug 24 +* Re: how cast works?107Dan Purgert
7 Aug 24 i+- Re: how cast works?1Keith Thompson
8 Aug 24 i+- Re: how cast works?1Lawrence D'Oliveiro
8 Aug 24 i+* Re: how cast works?101Thiago Adams
8 Aug 24 ii+* Re: how cast works?25Bart
8 Aug 24 iii`* Re: how cast works?24Michael S
8 Aug 24 iii +- Re: how cast works?1Thiago Adams
8 Aug 24 iii `* Re: how cast works?22Bart
8 Aug 24 iii  +* Re: how cast works?5Thiago Adams
8 Aug 24 iii  i+- Re: how cast works?1Thiago Adams
8 Aug 24 iii  i+* Re: how cast works?2Bart
8 Aug 24 iii  ii`- Re: how cast works?1Thiago Adams
8 Aug 24 iii  i`- Re: how cast works?1Keith Thompson
8 Aug 24 iii  `* Re: how cast works?16David Brown
8 Aug 24 iii   `* Re: how cast works?15Bart
9 Aug 24 iii    +* Re: how cast works?13David Brown
9 Aug 24 iii    i+* Re: how cast works?9Keith Thompson
9 Aug 24 iii    ii+* Re: how cast works?3Lawrence D'Oliveiro
9 Aug 24 iii    iii+- Re: how cast works?1Keith Thompson
9 Aug 24 iii    iii`- Re: how cast works?1James Kuyper
9 Aug 24 iii    ii`* Re: how cast works?5David Brown
9 Aug 24 iii    ii `* Re: how cast works?4Keith Thompson
12 Aug 24 iii    ii  `* Re: how cast works?3Tim Rentsch
12 Aug 24 iii    ii   `* Re: how cast works?2Keith Thompson
3 Sep 24 iii    ii    `- Re: how cast works?1Tim Rentsch
9 Aug 24 iii    i`* Re: how cast works?3Bart
9 Aug 24 iii    i `* Re: how cast works?2David Brown
10 Aug 24 iii    i  `- Re: how cast works?1Bart
9 Aug 24 iii    `- Re: how cast works?1Lawrence D'Oliveiro
8 Aug 24 ii`* Re: how cast works?75Keith Thompson
8 Aug 24 ii `* Re: how cast works?74Thiago Adams
8 Aug 24 ii  +* Re: how cast works?72Bart
9 Aug 24 ii  i+* Re: how cast works?47Keith Thompson
9 Aug 24 ii  ii+* Re: how cast works?38Bart
9 Aug 24 ii  iii+* Re: how cast works?2David Brown
12 Aug 24 ii  iiii`- Re: how cast works?1Bart
9 Aug 24 ii  iii+* Re: how cast works?29James Kuyper
9 Aug 24 ii  iiii+* Re: how cast works?14Bart
9 Aug 24 ii  iiiii+* Re: how cast works?3Keith Thompson
10 Aug 24 ii  iiiiii`* Re: how cast works?2Bart
10 Aug 24 ii  iiiiii `- Re: how cast works?1Keith Thompson
10 Aug 24 ii  iiiii`* Re: how cast works?10James Kuyper
13 Aug 24 ii  iiiii +- Re: how cast works?1David Brown
13 Aug 24 ii  iiiii +- Re: how cast works?1Bart
13 Aug 24 ii  iiiii `* Re: how cast works?7James Kuyper
13 Aug 24 ii  iiiii  `* Re: how cast works?6Bart
13 Aug 24 ii  iiiii   `* Re: how cast works?5Keith Thompson
13 Aug 24 ii  iiiii    `* Re: how cast works?4Bart
14 Aug 24 ii  iiiii     `* Re: how cast works?3Tim Rentsch
14 Aug 24 ii  iiiii      `* Re: how cast works?2Bart
18 Aug 24 ii  iiiii       `- Re: how cast works?1Tim Rentsch
9 Aug 24 ii  iiii+* Re: how cast works?2Keith Thompson
10 Aug 24 ii  iiiii`- Re: how cast works?1James Kuyper
9 Aug 24 ii  iiii`* Re: how cast works?12Kaz Kylheku
9 Aug 24 ii  iiii +* Re: how cast works?9Keith Thompson
10 Aug 24 ii  iiii i`* Re: how cast works?8Kaz Kylheku
10 Aug 24 ii  iiii i +* Re: how cast works?6Keith Thompson
10 Aug 24 ii  iiii i i+* Re: how cast works?3Kaz Kylheku
10 Aug 24 ii  iiii i ii+- Re: how cast works?1Keith Thompson
10 Aug 24 ii  iiii i ii`- Re: how cast works?1James Kuyper
10 Aug 24 ii  iiii i i`* Re: how cast works?2Bart
13 Aug 24 ii  iiii i i `- Re: how cast works?1David Brown
12 Aug 24 ii  iiii i `- Re: how cast works?1Tim Rentsch
10 Aug 24 ii  iiii +- Re: how cast works?1James Kuyper
12 Aug 24 ii  iiii `- Re: how cast works?1Tim Rentsch
9 Aug 24 ii  iii+* Re: how cast works?4Keith Thompson
9 Aug 24 ii  iiii`* Re: how cast works?3Bart
9 Aug 24 ii  iiii `* Re: how cast works?2Keith Thompson
9 Aug 24 ii  iiii  `- Re: how cast works?1Bart
12 Aug 24 ii  iii`* Re: how cast works?2Tim Rentsch
12 Aug 24 ii  iii `- Re: how cast works?1Bart
12 Aug 24 ii  ii`* Re: how cast works?8Tim Rentsch
12 Aug 24 ii  ii `* Re: how cast works?7Bart
12 Aug 24 ii  ii  +- Re: how cast works?1Keith Thompson
12 Aug 24 ii  ii  `* Re: how cast works?5Tim Rentsch
12 Aug 24 ii  ii   `* Re: how cast works?4Keith Thompson
12 Aug 24 ii  ii    `* Re: how cast works?3Ben Bacarisse
12 Aug 24 ii  ii     `* Re: how cast works?2Tim Rentsch
12 Aug 24 ii  ii      `- Re: how cast works?1Keith Thompson
9 Aug 24 ii  i`* Re: how cast works?24Thiago Adams
9 Aug 24 ii  i +* Re: how cast works?2Bart
9 Aug 24 ii  i i`- Re: how cast works?1Keith Thompson
9 Aug 24 ii  i +* Re: how cast works?19David Brown
9 Aug 24 ii  i i`* Re: how cast works?18Thiago Adams
9 Aug 24 ii  i i +* Re: how cast works?3Thiago Adams
9 Aug 24 ii  i i i+- Re: how cast works?1David Brown
9 Aug 24 ii  i i i`- Re: how cast works?1Keith Thompson
9 Aug 24 ii  i i +* Re: how cast works?11David Brown
10 Aug 24 ii  i i i`* Re: how cast works?10Bart
10 Aug 24 ii  i i i `* Re: how cast works?9Thiago Adams
10 Aug 24 ii  i i i  `* Re: how cast works?8Bart
11 Aug 24 ii  i i i   `* Re: how cast works?7Thiago Adams
11 Aug 24 ii  i i i    `* Re: how cast works?6Keith Thompson
9 Aug 24 ii  i i `* Re: how cast works?3Keith Thompson
9 Aug 24 ii  i `* Re: how cast works?2Keith Thompson
9 Aug 24 ii  `- Re: how cast works?1David Brown
8 Aug 24 i`* Re: how cast works?3Stefan Ram
7 Aug 24 +* Re: how cast works?6Keith Thompson
8 Aug 24 `- Re: how cast works?1Lawrence D'Oliveiro

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal