|
|
Log in / Subscribe / Register

Would you like signs with those chars?

Would you like signs with those chars?

Posted Oct 24, 2022 22:13 UTC (Mon) by Sesse (subscriber, #53779)
In reply to: Would you like signs with those chars? by NYKevin
Parent article: Would you like signs with those chars?

It's fairly common to use these types to save memory. Just to take a random example from work: A counting Bloom filter will almost never need to count higher than 255, so why waste four times the memory (and cache space)?

I do wish C had made a separate “byte” type, though, for aliasing reasons. char has too many tasks.


to post comments

Would you like signs with those chars?

Posted Oct 24, 2022 23:26 UTC (Mon) by NYKevin (subscriber, #129325) [Link]

1. short is very often good enough for use cases like that. Not always, but often.
2. If you must use char, unsigned will work perfectly well (does your Bloom Filter have a negative count?!), so this isn't actually a use case for signed char.
3. If you need a sentinel value, you can use 255; there is no magical rule that says sentinel values have to be negative.

Would you like signs with those chars?

Posted Oct 25, 2022 7:30 UTC (Tue) by gspr (subscriber, #91542) [Link] (2 responses)

Re the aliasing of char: wouldn't uint8_t be a nicely named type for your use?

Would you like signs with those chars?

Posted Oct 25, 2022 7:34 UTC (Tue) by Sesse (subscriber, #53779) [Link] (1 responses)

It would, except it maps onto unsigned char, which can alias on anything as it stands. And it's about fifty years too late to change that :-)

Would you like signs with those chars?

Posted Oct 25, 2022 19:18 UTC (Tue) by wahern (subscriber, #37304) [Link]

uint8_t isn't required to be typedef'd to unsigned char. An implementation could choose to treat it differently from unsigned char, precisely to avoid the aliasing behavior of char. It seems that in the case of GCC, it may have been C++ partly to blame for the current state of affairs: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66110#c13


Copyright © 2026, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds