Solved

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

Posted on 2011-09-16
5
609 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
  • 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

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

Suggested Solutions

Title # Comments Views Activity
Filemaker 14 vs Delphi Embarcadero 7 185
Delphi - replicating a form 8 62
Comparing dates in Delphi. Greater than/ Less than 3 110
select query - oracle 16 91
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…
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…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

930 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

9 Experts available now in Live!

Get 1:1 Help Now