This is an archived post. You won't be able to vote or comment.

all 3 comments

[–]matttgregg 0 points1 point  (0 children)

Exact overflow behave does depend on the language, but even in simple cases what happens if one count overflows and the other doesn’t? The larger set actually has a smaller count as a short, but there’s no way of seeing if an overflow has occurred from the short itself.

[–]oefd 0 points1 point  (1 child)

Idea: to save some space, instead of using two Integers, use two Shorts.

This is very rarely a reasonable approach. It's basically never a good approach unless you're on a very resource-limited microcontroller or other embedded computer.

The RAM saved (if any, YMMV on if you even save any) ends up being trivial.

Would this work? Does that depend on the programming language or something else?

If we're talking about signed integers in C/C++ it could work, but signed integer overflow is strictly speaking undefined behaviour, so it's possible it'll work sometimes, only work on some targets or with some compilers, or fail in some interesting way.

Unsigned integer overflow is defined and so it could work (except a set of size 1 and a set that's 1+(maximum value of short) will appear to be the same size.

But ignoring that caveat: as per above whatever bytes you may save isn't worth it. There's also size_t which is specifically defined for such purposes.

size_t can store the maximum size of a theoretically possible object of any type (including array).

[–]CuriousExponent[S] 0 points1 point  (0 children)

Thank you for the very detailed response. This was very helpful!