Solved

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

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

19 Experts available now in Live!

Get 1:1 Help Now