You are not logged in.

#1 2010-06-13 20:03:46

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

Behavior of overflowing integers. [particularly C]

Is the behavior of overflowing integers (of any size) standard across platforms? What exactly happens if I overfill or oversubtract unsigned integers? I'm asking this because I'm writing an emulator which may encounter arithmetic situations where this may occur.


Personally, I'd rather be back in Hobbiton.

Offline

#2 2010-06-13 20:11:06

falconindy
Developer
From: New York, USA
Registered: 2009-10-22
Posts: 4,111
Website

Re: Behavior of overflowing integers. [particularly C]

I believe it's consistent in that it's undefined behavior. Why not check for overflow before doing the calculation and handle it accordingly?

/* given A + B */
if (INT_MAX - A < B) {
  /* overflow occurred */
}

Offline

#3 2010-06-13 20:24:41

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: Behavior of overflowing integers. [particularly C]

ISO C99 says that overflow are "undefined behavior" i.e. anything can happen and its ok. Wrap around? OK Program abort? OK, Pregnant cat? OK.

Offline

#4 2010-06-13 22:47:17

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

Re: Behavior of overflowing integers. [particularly C]

The question now is how to handle it. Thanks for the info, we'll see how it goes.


Personally, I'd rather be back in Hobbiton.

Offline

#5 2010-06-13 23:11:04

Xyne
Administrator/PM
Registered: 2008-08-03
Posts: 6,963
Website

Re: Behavior of overflowing integers. [particularly C]

kazuo wrote:

ISO C99 says that overflow are "undefined behavior" i.e. anything can happen and its ok. Wrap around? OK Program abort? OK, Pregnant cat? OK.

Good thing it only applies to cats and not humans, otherwise bad programmers all over the world would have to have this conversation:

Girl: Are you sure it will be ok?
Guy: Don't worry, I know what I'm doing... my integers never overflow. Just relax and trust me.

*fast forward*

Guy: OH FSCK, OH FSCK, OH FSCK!
Girl: WHAT? What happened?
Guy: My program just crashed. I swear this has never happened to me before!
Girl: omg I can't believe I was so stupid. I knew I shouldn't have let you run that. My mother told me this would happen.
Guy: Hey, you wanted to run it just as much as I did. I don't remember you asking to check the source first. You were just as eager to "chmod +x" as I was.
Girl: Whatever. I'm never letting so much as compile code near me ever again.


My Arch Linux StuffForum EtiquetteCommunity Ethos - Arch is not for everyone

Offline

#6 2010-06-13 23:33:53

kazuo
Member
From: São Paulo/Brazil
Registered: 2008-03-18
Posts: 413
Website

Re: Behavior of overflowing integers. [particularly C]

Xyne wrote:
kazuo wrote:

ISO C99 says that overflow are "undefined behavior" i.e. anything can happen and its ok. Wrap around? OK Program abort? OK, Pregnant cat? OK.

Good thing it only applies to cats and not humans, otherwise bad programmers all over the world would have to have this conversation:

Girl: Are you sure it will be ok?
Guy: Don't worry, I know what I'm doing... my integers never overflow. Just relax and trust me.

Lol!! Hahaha

@Anikom15 You can get the needed info on google, this is a very common topic (its is a FAQ at comp.lang.c FAQ). For example look at http://www.fefe.de/intof.html

EDIT, now re-reading your last message looks like you question now is in a more general sense. You can look at something like http://en.wikipedia.org/wiki/Arbitrary- … arithmetic , but the final solution (bignum, or safe overflow, using bigger types) is per case basis. For example, if you know that you are limited to 64bit sizes you can use it (simple use a 64 bit type) if the needed type is not available you can create then (using for example two smaller types), as I said all depends of the user case.

Last edited by kazuo (2010-06-14 00:40:10)

Offline

#7 2010-06-15 07:37:30

PirateJonno
Forum Fellow
From: New Zealand
Registered: 2009-04-13
Posts: 372

Re: Behavior of overflowing integers. [particularly C]

Although it would be more portable to check for overflow, almost all CPUs handle overflow in the same way, which is by truncating the result to fit. So if you are looking to just emulate the behaviour of a CPU which does no overflow checking, just AND the result with, say, 0xff. However, some GPUs use saturation arithmetic instead, which is slightly harder to implement.


"You can watch for your administrator to install the latest kernel with watch uname -r" - From the watch man page

Offline

#8 2010-06-17 14:53:00

Lux Perpetua
Member
From: The Local Group
Registered: 2009-02-22
Posts: 69

Re: Behavior of overflowing integers. [particularly C]

Anikom15 wrote:

Is the behavior of overflowing integers (of any size) standard across platforms? What exactly happens if I overfill or oversubtract unsigned integers? I'm asking this because I'm writing an emulator which may encounter arithmetic situations where this may occur.

It actually depends on whether your integers are signed or not. Unsigned integers are supposed to be truncated, throwing away the extra MSBs. In other words, unsigned arithmetic is arithmetic mod 2^n, where n is the number of bits for that particular type. In theory, this is standard across all correct implementations of the C89 standard. In practice, even signed arithmetic behaves similarly, but it's technically undefined behavior.

Offline

#9 2010-06-17 20:03:52

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Behavior of overflowing integers. [particularly C]

Lux Perpetua wrote:
Anikom15 wrote:

Is the behavior of overflowing integers (of any size) standard across platforms? What exactly happens if I overfill or oversubtract unsigned integers? I'm asking this because I'm writing an emulator which may encounter arithmetic situations where this may occur.

It actually depends on whether your integers are signed or not. Unsigned integers are supposed to be truncated, throwing away the extra MSBs. In other words, unsigned arithmetic is arithmetic mod 2^n, where n is the number of bits for that particular type. In theory, this is standard across all correct implementations of the C89 standard. In practice, even signed arithmetic behaves similarly, but it's technically undefined behavior.

Can you point me to that paragraph?  I believe you, but that would be a useful reference to have.  C99 would work equally well.

Edit:  never mind, found it myself.  6.2.5p9 in C99.

Last edited by Trent (2010-06-17 20:15:03)

Offline

Board footer

Powered by FluxBB