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

ciuly 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.
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.


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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

746 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now