Thread (2 messages) 2 messages, 2 authors, 2021-07-09
  • (off-list ancestor, not in this archive)
  • Re: strlen · Martin Sebor <hidden> · 2021-07-09
  • Re: strlen · Alejandro Colomar (man-pages) <hidden> · 2021-07-09

Re: strlen

flat view

From: Martin Sebor <hidden>
Date: 2021-07-09 17:26:43

On 7/8/21 8:08 PM, LIU Hao via Gcc-help wrote:
在 7/8/21 8:05 PM, johnsfine--- via Gcc-help 写道:
quoted
  This is not the forum for such a discussion.  But I want to make people reading this aware that many expert C and C++ programmers (likely a majority) consider that advice to avoid unsigned types to be horrible advice.
I advise people to avoid signed types and I do so myself.  If an integer value won't be negative, it shouldn't be signed.  That makes your intent clearer to anyone reading your code, and (especially in x86-64) lets the compiler generate smaller and faster code.
That makes no sense. Would you prefer unsigned integers to signed ones, for something that can only be one of {1,2,3,4}? Just because something can't be negative, does not mean it should be unsigned.
There are benefits to making that explicit by choosing an unsigned
type: the result of converting a narrower unsigned type to a wider
unsigned type is unchanged.  The result of converting it to a wider
signed type may change.  This has an impact on value range propagation
which in turn can influence other optimizations as well as warnings.

Choosing an unsigned type has liabilities as well.  Because unsigned
arithmetic wraps around zero whereas signed arithmetic does not (it
overflows which is undefined), its results are less constrained than
those of signed arithmetic.

Martin
Conversion between `uint64_t` and `double` is much slower than `int64_t` on some platforms, e.g. x86, so 'smaller and faster code' is also groundless [1].


A signed integer is a value that denotes the number or amount of something, such as bytes, characters, files, seconds, kilometers, or even dollars. An unsigned integer is merely a fixed-sized collection of bits. That's the only fundamental difference.


[1] https://gcc.godbolt.org/z/6YY61rhGe
  
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help