[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2011-09-16
5
Medium Priority
?
646 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 2000 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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 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…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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

650 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