Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Data Overflow

Posted on 2006-10-19
10
Medium Priority
?
250 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

610 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