[SOLVED] Array of Tpoint as proc parameter corrupted data lazarus / 39-Graphics ---------------------------------------------------------------------- whips · 2014-01-13 13:05 Hello I am fighting third night with this code without success. I need pass vector data for drawing procedure drawtest When is vector  element rendered on Canvas is totaly corrupted. mostly values array[3..7] index are wrong. Points are random values. What I am doing wrong? My array is small can be alocated on stack. I dont want use dynamic heap and free it ( i wish keep this on compiller) How to correctly pass small array of Tpoints or record (x,y:word) if will be longint Tpoint problem? Code: [Select]type   { TForm1 }   TForm1 = class(TForm)     button1: TButton;     procedure FormCreate(Sender: TObject);     procedure Button1Click(Sender: TObject);   private          procedure drawtest( var el : array of tpoint);     { private declarations }   public     { public declarations }   end; Button1Click Code: [Select]procedure Button1Click(Sender: TObject); var   horny  : array[1..7] of Tpoint;     c : byte; begin    ...    ...    ...    horny[1] := Point(  54,26  );    horny[2] := Point(  253,26 );    horny[3] := Point(  260,33 );    horny[4] := Point(  261,35 ); // from here point is bad in proc.    horny[5] := Point(  220,72 );    horny[6] := Point(  81,72  );    drawtest(horny);    ....    ....    { other vector should look like this    secondvector[1] := Point(  540,6  );    secondvector[2] := Point(  20,6 );    drawtest(secondvector);} Drawtest Code: [Select] procedure TForm1.drawtest(  var el : array of tpoint);  var c: byte;       el2 : array[1..7] of Tpoint; /reserver for move/copy  begin    canvas.Line(el[1].x,el[1].y, el[2].x,el[2].y);    canvas.Line(el[2].x,el[2].y, el[3].x,el[3].y);   canvas.Line(el[3].x,el[3].y, el[4].x,el[4].y);    canvas.Line(el[4].x,el[4].y, el[5].x,el[5].y);    canvas.Line(el[5].x,el[5].y, el[6].x,el[6].y);    canvas.Line(el[6].x,el[6].y, el[7].x,el[7].y);    canvas.Line(el[7].x,el[7].y, el[1].x,el[1].y);   end; ---------------------------------------------------------------------- howardpc · 2014-01-13 14:09 You declare drawtest as a procedure taking a parameter of type Code: [Select]array of TPoint but when you call it you pass a different (static) array Code: [Select] horny  : array[1..7] of Tpoint; Because you have unwisely (and uneccesarily) also declared the parameter as var, the compiler does not warn you about the mismatched types. You cannot mix dynamic and static arrays in this fashion. Decide which type to use and stick to it. If you go for a dynamic array you also have to initialise its length before use, such as: Code: [Select] SetLength(dynamicArray, 7); If you declared FHorny as a private field of the desired type in the TForm1 class, your DrawTest procedure would not need any parameters at all, and could work directly with its private data, which you set in the button click event. ---------------------------------------------------------------------- whips · 2014-01-14 04:16 Thank you for your reply, If I want to use static array without using encapsuling data into class. How I should right modify procedure declaration ? I have about 20 variables with statis data (line vectors)I wish to know how to right pass static array to function/procedure without OOP. Thank you. PS: horny means in my lang upper  or top (its not english horny) ---------------------------------------------------------------------- taazz · 2014-01-14 05:46 Change var to const and you should be good to go. egCode: [Select]procedure drawtest(Const el : array of tpoint);. Keep in mind that el will always be zero(0) based regardless of how your static array is declared. ---------------------------------------------------------------------- whips · 2014-01-14 06:28 I am not sure if I understand right but I changed it from var to const. Still same problem wrong data passed to procedure Code: [Select] procedure TForm1.drawtest(  const el : array of tpoint);  begin    canvas.Line(el[1].x,el[1].y, el[2].x,el[2].y);    canvas.Line(el[2].x,el[2].y, el[3].x,el[3].y);    canvas.Line(el[3].x,el[3].y, el[4].x,el[4].y);    canvas.Line(el[4].x,el[4].y, el[5].x,el[5].y);    canvas.Line(el[5].x,el[5].y, el[6].x,el[6].y);    canvas.Line(el[6].x,el[6].y, el[7].x,el[7].y);    canvas.Line(el[7].x,el[7].y, el[1].x,el[1].y);  end; I am calling it this way Code: [Select]procedure TForm1.DrawClick(Sender: TObject); var   upper : array[1..7] of Tpoint; begin    upper[1] := Point(  54, 26 );    upper[2] := Point(  253,26 );    upper[3] := Point(  260,33 );    upper[4] := Point(  261,35 );    upper[5] := Point(  220,72 );    upper[6] := Point(  81, 72 );       drawtest(upper);    ......    ...... end; is producing this wrong result ---------------------------------------------------------------------- User137 · 2014-01-14 07:50 Like taazz said, the parameter array el is zero based. If you pass array 1..7 to it, it will become 0..6. Easiest way to fix that with your current code is change Code: [Select]  upper : array[1..7] of Tpoint;to Code: [Select]  upper : array[0..7] of Tpoint;You'll be allocating 1 unnecessary array index (0), but you don't have to modify anything else then. Or proper solution would be to use indices 0..6 in drawtest(). ---------------------------------------------------------------------- Leledumbo · 2014-01-14 08:05 Make a good use of Low() and High(), they are there to help you forget about literal array index. ---------------------------------------------------------------------- whips · 2014-01-14 08:12 Oh,,, I understand now I cannot fix that this way zero index contain special point calculated from other I am using it for coordinates when rotate whole vertor object. Ok now is working i rewrited drawtest and object is rendered fine now. Thank you.