We help IT Professionals succeed at work.

Can I use Pointer as an Array in pascal like C++?

gardenia
gardenia asked
on
I want to use an FileMaping Pointer as an Array of Record,
But I dont know how to do that,somebody help me?
Comment
Watch Question

Commented:
Try this:

type
   PYourArray=^TYourArray;
   TYourArray=array[0..YourArraySize] of TYourRecord;

var
   FileMapping: PYourArray; //instead of Pointer

Commented:
Just adding a little to AvonWyss's comment:

The delphi runtime declares pointers to a couple of similar arrays (ex: PByteArray, PCharArray) that are often used, in D2 & D3 in particular, to approximate dynamicaly resizable arrays. They're declared like so (I don't have the source handy, but if my implementation isn't detail perfect, the concept stands):

TByteArray = array[0..32767] of byte;
PByteArray - ^TByteArray;

so you can do this:

var
 bytes : PByteArray;
 index : integer;
begin
 getMem(bytes,1000);//now we've got 0..999 elements
 index := 999;
 bytes[index] := random(256);//bytes[999] is now in 0..255
 dispose(bytes);


So you can extend this like so:

RMyRec = record
 fooStr : array[0..10] of char;
 fooInt : integer;
end;
TMyRecArray = array[0..some_big_number] of RMyRec;
PMyRecArray = ^TMyRecArray;

var
 recs : PRecArray;
 ix : integer;
begin
 getMem(recs,sizeOf(RMyRec)*100);//For an array of 0..99 Records
 

GL
Mike
Commented:
Because the size of the array is not known at compile time, I would simply use this approach:

type
  PMyRecord = ^TMyRecord;
  TMyRecord = record
    Field1: Integer;
    Field2: Boolean;
    Field3: array[0..255] of Char;
  end;

var
  Map: Pointer;
  P: PMyRecord;
begin
  ...
  Map := MapViewOfFile(...);
  try
    P := Map;
    while P < Map + MapSize do
    begin
      with P^ do
        ...
      Inc(P);
    end;
  finally
    UnmapViewOfFile(...);
  end;
end;
gardenia:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
CERTIFIED EXPERT

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

split points between AvonWyss and TOndrej

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Thanks,

geobul
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.