Solved

Data Overflow

Posted on 2006-10-19
10
243 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
[X]
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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 106
I want to use librsync in my Delphi backup application. 3 73
PHP preg_replace code convert to Delphi 14 82
Firemonkey Queue list 1 32
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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