본문 바로가기

Academy I/Tech Academy

[Delphi]Pointer

var
  s:String;
  p:pointer;
begin
  p:=pointer(s);


  p := @s[1];
  // oder
  p := PChar(s);
  // zurück mit
  s := PChar(p);

  P := GetMem(Length(S) + 1);
  MoveMemory(P, PChar(S), Length(S) + 1);

  // und wenn man den Speicher/Text von P nicht mehr braucht,
  // dann muß man den natürlich wieder freigeben
  FreeMem(P);
end;


=========================================================

 var
iValue, j : integer;
pIntValue : ^integer;
begin
iValue := 2001;
pIntValue := @iValue;
...
j:= pIntValue^;
end;

=========================================================

var
   myString  : string;
   myCharPtr : PChar;
   i : Integer;
 
 begin
   // Create a string of Char's
   myString  := 'Hello World';
 
   // Point to the first character in the string
   i := 1;
   myCharPtr := Addr(myString[i]);
 
   // Display all characters in the string
   while i <= Length(myString) do
   begin
     ShowMessage(myCharPtr^);  // Display the string characters one by one
     Inc(i);
     Inc(myCharPtr);
   end;
 end;

=========================================================

var
   myRecordPtr : ^TMyRecord;
type
   TMyRecord = Record
     name : String[20];
     age  : Integer;
   end;
 
 var
   myRecord    : TMyRecord;
   myRecordPtr : ^TMyRecord;
 
 begin
   myRecord.name := 'Fred Bloggs';
   myRecord.age  := 23;
 
   myRecordPtr := @myRecord;
 
   ShowMessage(myRecordptr.name);  // Displays 'Fred Bloggs'
 end;

=========================================================

var
   msCount  : Integer;   // Count of numbers in the list
   maxCount : Integer;   // Maximum numbers that can fit into current storage
   memStart : Pointer;   // Start of the memory holding the list
   nextSlot : PInt64;    // Points to the next free slot in memory
 
 const
   ALLOCATE_SIZE = 20;   // How many numbers to store in first memory block
 
 // Constructor - initialise everything
 constructor TNumberList.Create;
 begin
   msCount  := 0;    // No numbers in the list yet
 
   // Allocate space for a limited number of numbers
   GetMem(memStart, ALLOCATE_SIZE * SizeOf(Int64));
 
   // Indicate how many numbers that we can add before acquiring more memory
   maxCount := ALLOCATE_SIZE;
 
   // And point to the next free memory slot - the first!
   nextSlot := memStart;
 end;

=========================================================

// Add a number to the list
 procedure TNumberList.Add(const number : Int64);
 begin
   // Store the number at the next slot in our memory block
   nextSlot^ := number;
 
   // And update things to suit
   Inc(msCount);
   Inc(nextSlot);
 end;

=========================================================

// Get the number at the index position (starting at 0)
 function TNumberList.GetValue(index : Integer): Int64;
 var
   numberPtr : PInt64;
 begin
   // Simply get the value at the given Int64 index position
   numberPtr := memStart;
   Inc(numberPtr, index);   // Point to the index'th Int64 number in storage
   Result := numberPtr^;    // And get the Int64 number it points to
 end;

=========================================================

// Add a number to the list
 procedure TNumberList.Add(const number : Int64);
 var
   newMemoryStart : Pointer;
   oldPtr, newPtr : PInt64;
   i : Integer;
 begin
   // if we do not have enough space to add the number, then get more space!
   if msCount = maxCount then
   begin
     // First allocate a bigger memory space
     GetMem(newMemoryStart, (maxCount + ALLOCATE_SIZE) * SizeOf(Int64));
 
     // Copy the data from the old memory here
     oldPtr := memStart;
     newPtr := newMemoryStart;
     for i := 1 to maxCount do
     begin
       // Copy one number at a time
       newPtr^ := oldPtr^;
       Inc(oldPtr);
       Inc(newPtr);
     end;
 
     // Free the old memory
     FreeMem(memStart);
 
     // And now refer to the new memory
     memStart := newMemoryStart;
     nextSlot := memStart;
     Inc(nextSlot, maxCount);
     Inc(maxCount, ALLOCATE_SIZE);
   end;
 
   // Now we can safely add the number to the list
   nextSlot^ := number;
 
   // And update things to suit
   Inc(msCount);
   Inc(nextSlot);
 end;

=========================================================

var
   list  : TNumberList;
   value : Int64;
   i     : Integer;
 begin
   // Create a number list object
   list := TNumberList.Create;
 
   // Add the first 30 even numbers to the list, each doubled in size
   for i := 0 to 29 do
     list.Add(i * 2);
 
   // Get the 22nd value = 44 (22 * 2)
   value := list[22];
   ShowMessage('22nd value = '+IntToStr(value));
 end;