Forum > Beginners

Populate a dynamic array

(1/7) > >>

justnewbie:
I have a dynamic array (empty in the beginning) and I want to populate it with unknown number of elements. How should it be?
My bad code:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TValue = packed record    tvVal: integer;    tvString: string;     end;   tvArray = array of TValue;  pTVArray = ^tvArray; // ... procedure PopulateArray(var myArray: pTVArray);var i: integer;    counter: integer = 0;begin  for i := 0 to 100 do    begin      if {something} then       begin         inc(counter);         SetLength(myArray,counter);         myArray[counter-1].tvVal := i;         myArray[counter-1].tvString := 'Hi friend!';      end;    end;end;

Nitorami:
If you are a newbie then by all means use simple native pascal code and don't fuff with pointers. Why do you need pTVArray ? It's completely unnecessary.

justnewbie:

--- Quote from: Nitorami on March 25, 2018, 10:41:05 pm ---If you are a newbie then by all means use simple native pascal code and don't fuff with pointers. Why do you need pTVArray ? It's completely unnecessary.

--- End quote ---
I have to communicate between a DLL (written in FP) and MT4. This is why I need pointers (I guess). Example here (with some formatting noise): https://www.forexfactory.com/showthread.php?t=219576 (see "3. Giving the DLL access to all bars in the chart")

furious programming:
First off, you must allocate memory for an array, before you try to set length of this array. Then, you need to set the length of array, and then, allocate memory for every array cell, before you write some data to them.

Below is a simple example – works correctly. Change the code for your purpose.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$MODE OBJFPC}{$LONGSTRINGS ON} uses  HeapTrc; type  PItem = ^TItem;  TItem = record    Value: Integer;    Text: String;  end; type  PItems = ^TItems;  TItems = array of PItem;   procedure PopulateItems(AItems: PItems);  var    LLength: Integer = 0;  begin    SetLength(AItems^, LLength + 1);     New(AItems^[LLength]);    AItems^[LLength]^.Value := 100;    AItems^[LLength]^.Text := 'got it';  end; var  LItems: PItems;begin  New(LItems);  PopulateItems(LItems);   WriteLn('Value: ', LItems^[0]^.Value);  WriteLn('Text:  ', LItems^[0]^.Text);   Dispose(LItems^[0]);  Dispose(LItems);end.

justnewbie:
Thank you, I will study it!

Navigation

[0] Message Index

[#] Next page

Go to full version