Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Array of (pointers to a record)

Tags:

delphi

I want to create a bunch of records (RWell) and to store them in an array in a certain order. Then I want to create a new array (different layout) and rearange the records in it. Of course, I don't want to duplicate data in RAM so I though that in the second array I should put pointers to the records in the first array. However, I can't do that. Anybody can tell what's wrong with the code below? Thanks

Type 
  RWell= record                                 
    x: string;  
    i: integer;
  end;

  PWell= ^RWell;

  RWellArray= Array[0..12, 0..8] of RWell;    


procedure TClass1.CreateWells
var 
  WellMX: RWellArray;
begin 
  { should I initialize the WellXM here? }
  { note: WellXM is a static array! }

  other stuff
end;



var Wells: array of PWell;
procedure TClass2.AddWell(aWell: RWell);
begin
 aWell.Stuff:= stuff; {aWell cannot be readonly because I need to change it here}

 SetLength(Wells, Length(Wells)+ 1);  { reserve memory } 
 Wells[High(Wells)]:= @aWell;
end;



procedure TClass3.DisplayWell;
var CurWell: RWell;
begin
 CurWell:= CurPrimer.Wells[iCurWell]^;       <--- AV here (but in debugger the address is correct)
end;

Solved by Rob K.

like image 505
Server Overflow Avatar asked Sep 08 '09 19:09

Server Overflow


People also ask

How do you create an array of pointers?

An array of pointers is an array that consists of variables of pointer type, which means that the variable is a pointer addressing to some other element. Suppose we create an array of pointer holding 5 integer pointers; then its declaration would look like: int *ptr[5]; // array of 5 integer pointer.

Why do we need array of pointers?

An array of pointers is useful for the same reason that all arrays are useful: it lets you numerically index a large set of variables. Below is an array of pointers in C that points each pointer in one array to an integer in another array. The value of each integer is printed by dereferencing the pointers.

Can we declare an array of pointers?

Declaration of an array of pointers: int *ptr[3]; We can make separate pointer variables which can point to the different values or we can make one integer array of pointers that can point to all the values.

How do you create an array of pointers in C++?

Consider this example: int *ptr; int arr[5]; // store the address of the first // element of arr in ptr ptr = arr; Here, ptr is a pointer variable while arr is an int array. The code ptr = arr; stores the address of the first element of the array in variable ptr .


1 Answers

In your AddWell function, you're passing the record by value. That means the function gets a copy of the actual parameter. You're storing a pointer to the formal parameter, which is probably just a location on the local stack of the function.

If you want a pointer to a well, then pass a pointer to a well:

procedure AddWell(AWell: PWell);
begin
  SetLength(Wells, Length(Wells) + 1);
  Wells[High(Wells)] := AWell;
end;

Another option is to pass the record by const value. For records, this means the actual parameter is passed as a reference. A pointer to the formal parameter is also a pointer to the actual parameter:

procedure AddWell(const AWell: RWell);
begin
  SetLength(Wells, Length(Wells) + 1);
  Wells[High(Wells)] := @AWell;
end;

I wouldn't really rely on that, though. When you want pointers, pass pointers. Some people try to avoid pointers in their code, but they're nothing to be afraid of.

like image 163
Rob Kennedy Avatar answered Nov 14 '22 09:11

Rob Kennedy