Integer overflow problems

Posted on 2006-06-22
Last Modified: 2012-05-05

I have a project with many calculations inside some timers.
After some seconds (~ 90000, almost a day) it pop up a message sayint "integer overflow"
and i have to terminate the program.

I've turned on Overflow checks in Options ---> Compiler.
I've put this line at the beggining : {$OVERFLOWCHECKS ON}

In EVERY calculation i've put :

Except on Exception do
  ErLog.Add('Error in "Section 1"');

(variables ErLog:TStringList; ErPath:String;)

BUT the program does NOT create an error log. It gives the error messages again and again.
It bypass the try ...except as it seems....

I tried to run the program from delphi with timer interval 1msec and let it run for 2million times !
No error. I can reproduce the error and i dont know how to find it.

Can you help ?
Thanks in advance.
Question by:CodedK
  • 4
  • 3
  • 2
  • +2
LVL 28

Assisted Solution

2266180 earned 75 total points
ID: 16966744
I would blame it on the "sizes"/ if you have D7+, then compile your project and check your hint messages. everywhere where you see that the sizes missmatch and that it was made larger: make sure you change the type so that the warning goes away. (sorry, but I cannot rember the hint message and can't seem to be able to reproduce it right away)

I also found an interesting article:
LVL 10

Accepted Solution

atul_parmar earned 75 total points
ID: 16966777
The error means that you are assining a large value (what can not be stored in integer) to integer variable.

just change the data type from integer to longint; or int64
LVL 16

Author Comment

ID: 16968399
Thanks ciuly :) . There are no hints !!!
(only a depricated command at some point BUT i've used it since the version 1
of this project without any problems).
Before i post this question i was reading that article :)
I've placed everywhere "*1.0" in multiplications. I did everything the article says.

Atul_parmar thank you, of course i know that, i could replace int with int64 but this would make the app,
use TOO much memory.
And i dont assign big values (2 or MAX 3 digits) and in every tick of the timer they turn to zero.

I'm trying to reproduce the problem by running the program from Delphi 7,
I've tried with interval '1 sec' (as it is) and let it run for 2 days.
I've tried with interval '1 msec' and let it run for 15 hours (15.000 hours of calculations in real life)
BUt there was no problem.

When i run the program from windows then at some point ~80.000 - 90.000 seconds (1 day)
there is that integer flow message popping up every second.

The problem is WHY it bypass the TRY... EXCEPT ?

Thanks for your help.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

LVL 16

Author Comment

ID: 16968411
Ciuly can you explain more about the sizes.. What do you mean by size mismatch?
LVL 28

Expert Comment

ID: 16968468
well .. when you have operands of some type and result is anotehr type.


byte*byte = word
but if you do byte = byte*byte you have a type size issue as byte*byte will not fit in a byte.

until we figure it out, install madhis madexcept. compile with debug info and all that, and that catch the exception like that (maybe it's not an exception but an error, in which case a simple catch exception deosn't help)

let the app run until it booms.

I'll try to think of other reasons that could cause an integer overflow in the OS as this is probably not delphi issue.
LVL 10

Expert Comment

ID: 16968491
you can also try with unsigned if you don't require -ve numbers

Assisted Solution

Scay7 earned 75 total points
ID: 16975609
After some seconds (~ 90000, almost a day) it pop up a message sayint "integer overflow"

This part where it works out the seconds, mabey you assigned a value as byte rather then int64 or longint
mabey its those counting of seconds thats causing the error :P

Just a thought...

Peace Scay7
LVL 11

Assisted Solution

ZhaawZ earned 75 total points
ID: 16978050
>> just change the data type from integer to longint; or int64
atul_parmar, I'm not sure about "new" Delphi versions, but in D6 longint and integer were synonyms (i.e., both are 32bit signed integers, with same range –2147483648..2147483647)

About sizes (i.e., assigning 16bit value to 8bit) - shouldn't it ignore waste bits instead of throwing an error?

CodedK, I'm not sure if it helps but maybe you should remove that "on Exception do"? It would look like this:

  // do something
  // save error log

Or try using "on EIntOverflow do" instead of "on Exception do".
LVL 11

Expert Comment

ID: 16978067
Another thing you could try is using 'try .. except .. end' for whole app instead of using it only for calculations (just in case if u get overflow somewhere else).

Project -> View Source -> ...

  Application.CreateForm(TForm1, Form1);
  // save log
LVL 11

Expert Comment

ID: 16978092
And one more thing - do not use "compile and run" for testing "try .. except" stuff. Run an app without involving Delphi (or disable debugger before running an app from Delphi).
LVL 16

Author Comment

ID: 16980076
Thanks Zhaawz.
I'll give it a try without on Exception to see if it can create the log.
LVL 16

Author Comment

ID: 17002330
Thank you all for your help and sorry for the delay.
I've searched thoroughly the code to find errors. I didnt found any...

So i've replaced all integers with Int64 and the error didnt occur...
This is half measure but at least the app is running fine.
Though i cant understand why it doesnt go in the try...except when the error occur.

I will split points equally and accept atul_parmar suggestion.
Thank you.

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
oracle global variables 4 76
URL for downloading Google Chrome for Win XP 2 191
Dev express lookupcombo 3 35
Installshield for Embarcadero EX 10.1 Berlin 4 60
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question