Solved

Data Overflow

Posted on 2006-10-19
10
240 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 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

28 Experts available now in Live!

Get 1:1 Help Now