Deutsch   English   Français   Italiano  
<v95ieq$qh1q$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: how cast works?
Date: Fri, 9 Aug 2024 19:08:42 +0200
Organization: A noiseless patient Spider
Lines: 118
Message-ID: <v95ieq$qh1q$1@dont-email.me>
References: <v8vlo9$2oc1v$1@dont-email.me> <slrnvb7kis.28a.dan@djph.net>
 <v929ah$3u7l7$1@dont-email.me> <v92gt1$e1l$1@dont-email.me>
 <20240808193203.00006287@yahoo.com> <v92va5$4msg$1@dont-email.me>
 <v9310a$4v1a$2@dont-email.me> <v93565$6ffo$1@dont-email.me>
 <v93h12$9vom$1@dont-email.me> <v93pfg$c7b8$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 09 Aug 2024 19:08:42 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="20dc90042b48f6417a3734e9d0abbff5";
	logging-data="869434"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19kqPEq44X6UT8ltToqYRSqmykVWTvjFwg="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:BWywE0BpvA2J+0os7G/EQSpO8GI=
Content-Language: en-GB, nb-NO
In-Reply-To: <v93pfg$c7b8$1@dont-email.me>
Bytes: 5113

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?  How hard is it for 
you to actually write a C function that has defined behaviour that does 
what you want it to do?  Get step 1 of these tests right, and we won't 
have to keep repeating this stuff.

> 
> OK, let's initialise it and see what difference it actually makes. My 
> code is now this:
> 
>    #include <stdbool.h>
> 
>    void BC(void) {
>        _Bool b;
>        int i;
>        i=b;
>    }
> 
>    void DB(void) {
>        _Bool b=false;
>        int i;
>        i=b;
>    }
> 
> The output from gcc -O1 is this:
> 
> BC:
>          ret
> DB:
>          ret
> 
> There is no difference. 

There is a difference in the code.  One (BC) has no defined behaviour. 
The other (DB) has defined behaviour with no observable behaviour.  It's 
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?  Or look at the functions that I wrote?

The stuff you write is either meaningless, or pointless, or perhaps 
both.  I have no doubt that you know this fine.

So the big question is, why are you writing it?  Is it just to provoke 
me?  Is it because you want to confuse the OP and other readers?  Do you 
like pretending to be a fool?

I'm giving up trying to help you - at least until you show some hint of 
trying to learn.  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.