Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Data Overflow

Posted on 2006-10-19
10
242 Views
Last Modified: 2010-04-05
I am looking at a delphi routine and it seems the delphi routine would create an Overflow. What happens in Delphi when a variable overflows. Does it error out or does delphi truncate the data some how? I am using Delphi 7.
0
Comment
Question by:sk33v3
  • 5
  • 5
10 Comments
 
LVL 28

Expert Comment

by:2266180
ID: 17770138
it is usually ignored.
but I think it could depend also on how you configured the compiler: project options, compiler: overflow checking.
I jsut made a quick test
var b:byte;
begin
  b:=255;
  b:=b+1;
  showmessage(inttostr(b));
end;
with that checked and unchecked, I always got 0 with no error. so I guess not all overflows generate errors.
0
 
LVL 9

Author Comment

by:sk33v3
ID: 17770203
Well I am trying to Convert a delphi routine into another programming language and when I try to convert I keep getting overflow issues.

Cache := (b + Cache) * FirstCache + LastCache;

Cache is a Word variable type
b is a byte variable type
FirstCache is a constant with the value of 32768
and Last Cache is a constant with the value of 16382

Now from my understanding of that line if b+cache results in anything larger than 1 a buffer overflow would result. Is that correct?
0
 
LVL 28

Expert Comment

by:2266180
ID: 17771866
that is correct
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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 9

Author Comment

by:sk33v3
ID: 17773507
Ok, so on every round Cache gets set back to 0 because fo the buffer overflow? When compiled the application seems to work just fine and I know that b does get larger than 1.
0
 
LVL 28

Expert Comment

by:2266180
ID: 17773759
not 0. either firstcache + LastCache or LastCache
0
 
LVL 9

Author Comment

by:sk33v3
ID: 17773793
Is there a way to predict which one it is going to get?
0
 
LVL 28

Expert Comment

by:2266180
ID: 17773838
well ... I guess yes
if b+cache mod 2 = 0 then LastCache
else
firstcache+LastCache
0
 
LVL 9

Author Comment

by:sk33v3
ID: 17775935
Even though the actual line of code doesn't have the mod in it delphi assumes mod 2?
0
 
LVL 28

Accepted Solution

by:
2266180 earned 500 total points
ID: 17776274
oh no. you asked for a prediction. that is the prediction.

if b+cache = 0 then Cache := 0 * FirstCache + LastCache; => lastcache
if b+cache = 1 then Cache := 1 * FirstCache + LastCache; => firstcache+lastcache
if b+cache = 2 then Cache := 2 * FirstCache + LastCache; => lastcache
if b+cache = 3 then Cache := 3 * FirstCache + LastCache; => firstcache+lastcache
and so on
from there we see that because firstcache is constant and has the value 32768, at every second case, it will overflow.( 2k*32768 = k*65536, for each k from 0 to n and since 65536 is 10000000000000000 in binary (1 followed by 16 0's) it results that all even b+cache will cause the multiplication to overflow and thus leaving only lastcache in the equation and on the other hand, all even b+cache will also cause an overflow, but will leave one instance of firstcache so it will result in firstcache+lastcache)
so from there the prediction:
if (b+cache) mod 2 = 0 then LastCache
else firstcache+LastCache
0
 
LVL 9

Author Comment

by:sk33v3
ID: 17777127
Ok, that is what I wanted to know. Thanks.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi Form ownership 4 111
When i run adoquery my application freezes 26 178
Delphi Yen format 3 45
Firemonkey DbLookupComboBox equivalent ? 2 55
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

856 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