0

We know in C++ programming language, int type occupies 4 bytes, while long type occupied 8 bytes, and could be verified via following code snippet.

cout << "sizeof(int) = " << sizeof(int) << endl;
cout << "sizeof(long) = " << sizeof(long) << endl;
>>> sizeof(int) = 4
>>> sizeof(long) = 8

Thus, we can further deduce the maximum number that can be represented by int type is 2^31-1, if we increase this by 1, it would raise integer overflow issue.

int a = (1<<31)-1;
int b = a+1;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
>>> a = 2147483647
>>> b = -2147483648

In order to prevent the potential overflow issue, we can use long type, as it supports much wider range compared with int type from previous test result. However, I found following style would still fail due to integer overflow why coding in leetcode.

long c = a+1;
long d = (long)(a+1);

enter image description here

And currently I have to write it in a more tedious way.

long e = c;
e = e+1;

Does anyone know why this happens, help explain and how can we resolve this via a more neat way? Thanks.

x86_64
  • 95
  • 1
  • 10
  • Fun exercise: Put a 1 in the zero-bit slot (i.e. rightmost) of a 32bit *signed* integer, then shift it left **31** bits. Where does it end up? – WhozCraig Aug 29 '22 at 02:54
  • @WhozCraig (1<<31)-1==2147483647 – x86_64 Aug 29 '22 at 02:56
  • 1
    "_We know in C++ programming language, int type occupies 4 bytes, while long type occupied 8 bytes_": There is no guarantee that this is true and it is platform-dependent. The only requirement is that `int` is at least 16 bits and `long` at least 32 bits with a range not smaller than that of `int`, everything else is up to the C++ implementation. – user17732522 Aug 29 '22 at 03:02
  • 1
    You need to cast one of the operands to the addition to long, like `a`, not the result after the int math has been done. – Retired Ninja Aug 29 '22 at 03:03
  • 1
    Didn't answer my question, but here's another one: what is your expectation adding `INT_MAX` + 1 (which is effectively what you're doing)? How do you expect that to *not* be signed overflow condition? At least one of the operands needs to be upsized (by cast or otherwise), Also, platform-int doesn't always occupy 4-bytes either. The standard doesn't mandate byte occupation; it mandates specific minimal *ranges* of representation (inherits it from C, in truth, last I checked anyway). – WhozCraig Aug 29 '22 at 03:04
  • 2
    The overflow is happening while adding `1` to the `int` value. Casting the result to some other type later on doesn't change the fact that you performed an overflowing operation. – user17732522 Aug 29 '22 at 03:05
  • 1
    `a+1`, if `a` is an `int` and is the maximum value for an `int`, will be calculated as an `int` and overflow. Assigning the end result to a long does not change this. See the linked question for a complete overview of integer promotion rules in C++. – Sam Varshavchik Aug 29 '22 at 03:05
  • *We know in C++ programming language, int type occupies 4 bytes* -- Wherever you got this information from, stop going there. If it something you got from Leetcode, then I'm not surprised. Back in the "old" MSDOS days, an `int` was 2 bytes and `long` was 4 bytes. – PaulMcKenzie Aug 29 '22 at 03:07
  • @user17732522 Sorry for not making it clear, let's say we are just working with gcc on Linux platform, e.g., Ubuntu. – x86_64 Aug 29 '22 at 03:35
  • @WhozCraig Really appreciate your detailed explanation. – x86_64 Aug 29 '22 at 03:39
  • If you want full control over the size of your numbers use types from ``, eg. use `std::uint64_t`. On an unrelated note: first learn C++ then start training problem solving skills at leetcode. Don't use leetcode to learn C++ (because you are going to pick up some bad C++ habits if you do that). – Pepijn Kramer Aug 29 '22 at 03:39
  • @x86_64 No problem. I am just mentioning it because it is not uncommon to see stuff like this stated as true without any qualification of the platform and for beginners to assume that it is fundamentally true in C++. Then there are big surprises when it is suddenly not true when e.g. working on Windows. – user17732522 Aug 29 '22 at 03:45

0 Answers0