Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #162946
| Path | csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Tim Rentsch <tr.17687@z991.linuxsc.com> |
| Newsgroups | comp.lang.c |
| Subject | Re: How to add ssize_t a by size_t b? |
| Date | Sat, 02 Oct 2021 14:52:44 -0700 |
| Organization | A noiseless patient Spider |
| Lines | 34 |
| Message-ID | <868rzbrt5f.fsf@linuxsc.com> (permalink) |
| References | <c64a2f9b-4745-42f4-9bd4-ce093fbc2d68n@googlegroups.com> <87y27ch2id.fsf@nosuchdomain.example.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | reader02.eternal-september.org; posting-host="394007ef4ff0447a8819fcdafe9c3245"; logging-data="13950"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19zN41h2vacsxqiGNuAuxkgL5cp7uusJfM=" |
| User-Agent | Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) |
| Cancel-Lock | sha1:JkBx5iLR04Dv5m3W9+3r4YUYQYM= sha1:uQ/nMTrLfxfjje4C8E59FEswrKs= |
| Xref | csiph.com comp.lang.c:162946 |
Show key headers only | View raw
Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
> wij <wyniijj@gmail.com> writes:
>
>> To simply the question of "a+b":
>>
>> ssize_t add(ssize_t a, size_t b) {
>> if(a+b would overflow) { set errno=ERANGE; }
>> a+=b; // ?
>> return a;
>> }
>>
>> Another example:
>> ssize_t a=SSIZE_T_MIN;
>> size_t b=SIZE_T_MAX;
>> a+=b; // Is this OK? Or, How the addition is done correctly?
>
> In standard C, you don't, because ssize_t doesn't exist (unless you
> define it yourself).
>
> However, POSIX does define a type ssize_t, which is a signed integer
> type that can represent values up to SSIZE_MAX.
>
> The rules are given in N1570 6.3.1.8, "Usual arithmetic conversions".
>
> It's likely, but not guaranteed, that size_t and ssize_t have the same
> rank. If so, then the ssize_t value is converted to size_t, and
> assuming SSIZE_MAX <= SIZE_MAX the conversion does not change the
> value. [...]
If size_t and ssize_t have the same integer conversion rank then
SSIZE_MAX <= SIZE_MAX is guaranteed. However, converting a value
of type ssize_t to type size_t may indeed change the value,
because the value might be negative.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How to add ssize_t a by size_t b? wij <wyniijj@gmail.com> - 2021-10-01 08:32 -0700
Re: How to add ssize_t a by size_t b? Guillaume <message@bottle.org> - 2021-10-01 19:39 +0200
Re: How to add ssize_t a by size_t b? scott@slp53.sl.home (Scott Lurndal) - 2021-10-01 18:31 +0000
Re: How to add ssize_t a by size_t b? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-01 14:29 -0700
Re: How to add ssize_t a by size_t b? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-01 20:17 -0400
Re: How to add ssize_t a by size_t b? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-01 18:42 -0700
Re: How to add ssize_t a by size_t b? Manfred <noname@add.invalid> - 2021-10-02 20:17 +0200
Re: How to add ssize_t a by size_t b? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-02 15:03 -0400
Re: How to add ssize_t a by size_t b? James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-10-01 14:53 -0400
Re: How to add ssize_t a by size_t b? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-02 15:13 -0700
Re: How to add ssize_t a by size_t b? Keith Thompson <Keith.S.Thompson+u@gmail.com> - 2021-10-01 14:13 -0700
Re: How to add ssize_t a by size_t b? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-02 14:52 -0700
Re: How to add ssize_t a by size_t b? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-02 14:47 -0700
Re: How to add ssize_t a by size_t b? Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-10-06 04:06 -0700
csiph-web