Solved

access violation on tbits.bits when tbits.size = high(integer)

Posted on 2011-09-16
5
623 Views
Last Modified: 2012-05-12
I'm using a list of boolean flags to indicate primenumbers by index
This works fine for lists up to 2 billion entries but as soon as I set the size of the list (tbits.size) to high(integer
) I get an access violation when I try to set the flags, like so:
for d := 0 to list.size-1 do  list[d] := true;
Where d is a local variable of type integer
As an added bonus when I trace throught the code up to the point where the exception occurs the evaluator claims that d is an undeclared identifier this bit of strangeness can be avoided by making d global instead of local this however does not fix the access violation-issue. I use Delphi XE on Wondows 7 x64 SP1. Access violation would sugest a nil-pointer but I checked and the object is created fine. Any ideas anybody.
TIA
Psydo.
0
Comment
Question by:Psychododo
[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
  • 4
5 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 36554436
I'll have to check in Delphi code for TBits, of course I suspect some calculation that goes over some inherent limit.

does it makes AV for a big value for v or even the first ones, just because Size is too big ?
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36554453
ok, there is ASM code here, it will take some time to get documentation (and possible limitations) of operator BTS (Bit Set)

Other than that I can only see the possible overflow when calculating the size of the memory.
If it can work with Size := High(Integer) Div 4 but not just above, then the problem is here.

Can't do tests right now, can you ?
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 36554490
When you have values > High(Integer)-31, the calculated size (which is not done on 64bits int) will overflow (and then be negative). Which will fail on GetMem, thus returning nil for pointer.

So accept this limitation or copy TBits code in Classes unit and fix this limitation
0
 
LVL 25

Expert Comment

by:epasquier
ID: 36554501
replacing Integer with Cardinal will double the capacity,
and stating Int64 in the formula will force 64 bit precision for the values around the limit. That would be the 31 last values, not sure that it's worth it. But Cardinal trick is totally free and easy
procedure TBits.SetSize(Value: Cardinal);
var
  NewMem: Pointer;
  NewMemSize: Cardinal;
  OldMemSize: Cardinal;

  function Min(X, Y: Integer): Integer;
  begin
    Result := X;
    if X > Y then Result := Y;
  end;

begin
  if Value <> Size then
  begin
    if Value < 0 then Error;
    NewMemSize := ((Int64(Value) + BitsPerInt - 1) div BitsPerInt) * SizeOf(Cardinal);
    OldMemSize := ((Int64(Size) + BitsPerInt - 1) div BitsPerInt) * SizeOf(Cardinal);
    if NewMemSize <> OldMemSize then
    begin
      NewMem := nil;
      if NewMemSize <> 0 then
      begin
        GetMem(NewMem, NewMemSize);
        FillChar(NewMem^, NewMemSize, 0);
      end;
      if OldMemSize <> 0 then
      begin
        if NewMem <> nil then
          Move(FBits^, NewMem^, Min(OldMemSize, NewMemSize));
        FreeMem(FBits, OldMemSize);
      end;
      FBits := NewMem;
    end;
    FSize := Value;
  end;
end;

Open in new window

0
 

Author Closing Comment

by:Psychododo
ID: 36554566
Excellent Epasquier, I think I'll just accept the limitation for now as my version of Delphi comes without sources there will be no fixing of code. However I am relieved to know the voodoo occurs in the Tbits-object and is not of my own doing.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamically Created Query 3 78
Firemonkey android show image from resource ? 1 71
Firemonkey allowing RTL on android 6 75
Delphi android app hide keyboard 3 81
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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

737 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