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


Groups > comp.lang.c > #149168

Re: Pointer goes to null

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: Pointer goes to null
Date 2020-01-16 02:20 +0000
Organization A noiseless patient Spider
Message-ID <87sgkgdtvf.fsf@bsb.me.uk> (permalink)
References (3 earlier) <qv2erl$jp2$1@dont-email.me> <qv2fm6$n8m$1@dont-email.me> <1bab6894-09d3-41e5-8a79-f068db00b0d8@googlegroups.com> <87lfq8ftxs.fsf@bsb.me.uk> <qvo3s9$q4n$1@dont-email.me>

Show all headers | View raw


Lew Pitcher <lew.pitcher@digitalfreehold.ca> writes:

> Ben Bacarisse wrote:
>
>> m.nasim85@gmail.com writes:
>> 
>>> Lew, acctually, I cannot accept either
>>>>   blk_cnt = ((data_bitlen)/256) + ((data_bitlen%256)/(data_bitlen%256))
>>> or
>>>>   blk_cnt = ((data_bitlen/256) + ((data_bitlen%256) != 0))
>>>
>>> 1st form may divide by zero. (you mentioned this)
>>> 2nd form may have unpredictable behavior as ((data_bitlen%256) != 0)
>>> returns TRUE ,which is only guaranteed not to be zero. it may be 1
>>> (which you intended) or -1.
>> 
>> No.  All of C's equality and comparison operators are defined to give
>> either 0 or 1.  No other value is possible.  Some of C's /functions/
>> don't behave this way (strcmp, isascii etc.), but that another matter.
>> 
>> But Lew's second suggestion does not do what your original code did.
>> Your code had the odd exception that the block count should be 1 when
>> the bit length was zero.  If you just want the number of whole or part
>> 256-bit blocks, the usual way is to write
>> 
>>   blk_cnt = (data_bitlen + 255) / 256;
>> 
>> but that does not give blk_cnt == 1 when data_bitlen == 0.  Do you
>> really want that?
>
> My suggested
>   ((data_bitlen)/256) + ((data_bitlen%256) != 0);
> expression gives the same results as the OP's original
>   ceil(data_bitlen/256.)
> expression.

Ah, sorry, I thought you were replacing the whole thing.  Easy to miss
since the OP's whole expression is rather odd.  I don't think I've ever
wanted a "block" count of 1 when the "part" count is 0!

> I apologize if I did not make it clear that the suggested expression
> was a substitution for the original ceil() expression, making the OP's
>   u64 blk_cnt = (data_bitlen == 0) ? 1 : ceil(data_bitlen / 256.0);
> into
>   u64 blk_cnt = (data_bitlen == 0) ? 1 : ((data_bitlen)/256) + ((data_bitlen%256) != 0);
> and thus keeping the computation within the realm of integer math.

-- 
Ben.

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


Thread

Pointer goes to null m.nasim85@gmail.com - 2020-01-02 01:34 -0800
  Re: Pointer goes to null Jorgen Grahn <grahn+nntp@snipabacken.se> - 2020-01-02 12:47 +0000
  Re: Pointer goes to null Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-01-02 08:55 -0500
    Re: Pointer goes to null Bart <bc@freeuk.com> - 2020-01-02 18:49 +0000
      UB awaits... (Was: Pointer goes to null) gazelle@shell.xmission.com (Kenny McCormack) - 2020-01-02 19:53 +0000
        Re: UB awaits... (Was: Pointer goes to null) David Brown <david.brown@hesbynett.no> - 2020-01-02 23:34 +0100
          Re: UB awaits... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-03 01:43 +0000
            Re: UB awaits... David Brown <david.brown@hesbynett.no> - 2020-01-03 09:50 +0100
              Re: UB awaits... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-03 14:30 +0000
                Re: UB awaits... David Brown <david.brown@hesbynett.no> - 2020-01-03 17:27 +0100
  Re: Pointer goes to null Bart <bc@freeuk.com> - 2020-01-02 14:07 +0000
    Re: Pointer goes to null m.nasim85@gmail.com - 2020-01-02 07:29 -0800
      Re: Pointer goes to null Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2020-01-02 10:12 -0800
  Re: Pointer goes to null jak <nospam@please.ty> - 2020-01-02 16:46 +0100
  Re: Pointer goes to null Barry Schwarz <schwarzb@delq.com> - 2020-01-02 08:50 -0800
    Re: Pointer goes to null m.nasim85@gmail.com - 2020-01-03 16:20 -0800
      Re: Pointer goes to null Barry Schwarz <schwarzb@delq.com> - 2020-01-03 22:16 -0800
        Re: Pointer goes to null m.nasim85@gmail.com - 2020-01-07 03:21 -0800
      Re: Pointer goes to null Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-01-07 12:24 -0500
        Re: Pointer goes to null Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-01-07 12:38 -0500
          Re: Pointer goes to null m.nasim85@gmail.com - 2020-01-15 09:36 -0800
            Re: Pointer goes to null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-15 18:36 +0000
              Re: Pointer goes to null Lew Pitcher <lew.pitcher@digitalfreehold.ca> - 2020-01-15 17:31 -0500
                Re: Pointer goes to null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-16 02:20 +0000
                Re: Pointer goes to null m.nasim85@gmail.com - 2020-01-17 14:51 -0800
                Re: Pointer goes to null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-17 23:32 +0000
              Re: Pointer goes to null m.nasim85@gmail.com - 2020-01-17 14:47 -0800
                Re: Pointer goes to null Ben Bacarisse <ben.usenet@bsb.me.uk> - 2020-01-17 23:25 +0000
            Re: Pointer goes to null James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-15 10:49 -0800
              Re: Pointer goes to null m.nasim85@gmail.com - 2020-01-17 14:49 -0800
        Re: Pointer goes to null Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2020-01-07 09:42 -0800
          Re: Pointer goes to null m.nasim85@gmail.com - 2020-01-15 09:40 -0800
  Re: Pointer goes to null James Kuyper <jameskuyper@alumni.caltech.edu> - 2020-01-02 22:38 -0500

csiph-web