Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2005-11

question on pointers

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

I have a question on copying data to a pointer array. I'm not that into pointers, so I can't figure out what I'm missing here. I have included sample code below, and I get a SF on the line where I assign the data to the pointer.
Is it perhaps that I need to assign memory to the pointer data??

Darius

procedure TForm1.Button1Click(Sender: TObject);
type
  myfloat = extended;
  pmyfloat = ^myfloat;
var
  test: array of pmyfloat;
  test2: array of myfloat;
begin
  setlength(test, 1);
  setlength(test2, 1);
  test2[0] := 3.141;
  test[0]^ := test2[0];
  ShowMessage(floattostr(test[0]^));
end;
Adriaan van Os
to Unknown author
Show quoted text 24 lines
I have a question on copying data to a pointer array. I'm not that into pointers, so I can't figure out what I'm missing here. I have included sample code below, and I get a SF on the line where I assign the data to the pointer. Is it perhaps that I need to assign memory to the pointer data?? Darius procedure TForm1.Button1Click(Sender: TObject); type myfloat = extended; pmyfloat = ^myfloat; var test: array of pmyfloat; test2: array of myfloat; begin setlength(test, 1); setlength(test2, 1); test2[0] := 3.141; test[0]^ := test2[0];
;

I suggest you read a tutorial on pointers. They can be very dangerous, so you should know what you are doing.

Regards,

Adriaan van Os

Florian Klaempfl
to Adriaan van Os
Adriaan van Os wrote 31 lines
wrote: > I have a question on copying data to a pointer array. I'm not that into > pointers, so I can't figure out what I'm missing here. I have included > sample code below, and I get a SF on the line where I assign the data to > the pointer. > Is it perhaps that I need to assign memory to the pointer data?? > > Darius > > procedure TForm1.Button1Click(Sender: TObject); > type > myfloat = extended; > pmyfloat = ^myfloat; > > var > test: array of pmyfloat; > test2: array of myfloat; > begin > setlength(test, 1); > > setlength(test2, 1); > test2[0] := 3.141; > > test[0]^ := test2[0]; ; I suggest you read a tutorial on pointers. They can be very dangerous, so you should know what you are doing.

Even worse when combining them with dyn. arrays. As soon as you do a setlength(test,x); all previous references to elements of test might be invalid.