Recent

Author Topic: Populate a dynamic array  (Read 4872 times)

jamie

  • Hero Member
  • *****
  • Posts: 990
Re: Populate a dynamic array
« Reply #15 on: March 27, 2018, 12:48:19 am »
I don't know the interface you are attempting to work with however, it is customary for a remote set of code functions
to supply a function that will return data to your code..

 The function will expect the size of the block you have, if it is not enough, it will return false but return the actual size it
needs, then you can reallocate the memory and then try it again with now the amount of memory it needs for the
call..

 I am sure there are some specs somewhere that indicates this?

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Populate a dynamic array
« Reply #16 on: April 30, 2018, 08:46:20 pm »
Earlier I got some solution, but those are not good for me, because I need a version where the array size is UNKNOWN in advance.
So, how can I dynamically populate (and increase its size) the array properly?
My insufficient code:

Code: Pascal  [Select]
  1. var myArray: array of byte;
  2. while Something1 do
  3. begin
  4.   if Something2 then
  5.   begin
  6.      myArray[]:=Random(255);
  7.   end;
  8. end;
« Last Edit: April 30, 2018, 09:02:22 pm by justnewbie »

Thaddy

  • Hero Member
  • *****
  • Posts: 7199
Re: Populate a dynamic array
« Reply #17 on: April 30, 2018, 09:13:26 pm »
Something like this?:
Code: Pascal  [Select]
  1. program untitled;
  2. {$ifdef fpc}{$mode delphi}{$H+}{$endif}
  3. var
  4.   myArray: array of byte;
  5.   i,b:byte;
  6. begin
  7.   Randomize;
  8.   Setlength(myArray,Random(100)); //create a dynamic array of random length
  9.   writeln('Array size is ',length(myArray));
  10.   for i:=Low(myArray) to High(myArray) do // i is index of an array element, not content
  11.      myArray[i]:=Random(255); // fill the array with random values
  12.   writeln('Array content is :');
  13.   for b in myArray do  // b is content of an array element, not an index
  14.      write(b,' ');
  15.   writeln;
  16. end.

Run it a few times...

« Last Edit: April 30, 2018, 09:16:15 pm by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Populate a dynamic array
« Reply #18 on: April 30, 2018, 09:24:37 pm »
@Thaddy: no, I no need random size array.
I have an empty array. I don't know in advance what size it will have.
When a certain "if" condition is met, then this array gets a newer value.
I know how to add the new array value, but how should I manage the array's size properly?

I think this is not good, is it?
Code: Pascal  [Select]
  1. var myArray: array of byte;    
  2. begin  
  3.     while Something1 do
  4.     begin
  5.       if Something2 then
  6.       begin
  7.         SetLength(myArray, Length(myArray)+1);
  8.         myArray[i]:=Random(255);
  9.       end;    
  10.     end;
  11. end;
« Last Edit: April 30, 2018, 09:31:21 pm by justnewbie »

Thaddy

  • Hero Member
  • *****
  • Posts: 7199
Re: Populate a dynamic array
« Reply #19 on: April 30, 2018, 09:37:46 pm »
That would be simply
Code: Pascal  [Select]
  1. var myArray: array of byte;    
  2. begin  
  3.     while Something1 do
  4.     begin
  5.       if Something2 then
  6.       begin
  7.         SetLength(myArray, Succ(High(myArray)));  // increase size by one
  8.         myArray[High(myArray)]:=Random(255); // fill the new element
  9.       end;    
  10.     end;
  11. end;
Note this is very slow code if it happens often. Better to have a large array and maintain an index to the last element used, and increase that.
Results in much faster code.
« Last Edit: April 30, 2018, 09:41:42 pm by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Populate a dynamic array
« Reply #20 on: April 30, 2018, 09:38:57 pm »
OK, thanks!

Thaddy

  • Hero Member
  • *****
  • Posts: 7199
Re: Populate a dynamic array
« Reply #21 on: April 30, 2018, 09:41:07 pm »
I made a small mistake. Corrected.
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

howardpc

  • Hero Member
  • *****
  • Posts: 2834
Re: Populate a dynamic array
« Reply #22 on: April 30, 2018, 09:48:20 pm »
If you want the array to cover the full byte value range that would need to be
Code: Pascal  [Select]
  1. ...
  2. myArray[High(myArray)] := Random(256);
  3. ...

justnewbie

  • Full Member
  • ***
  • Posts: 135
Re: Populate a dynamic array
« Reply #23 on: April 30, 2018, 09:55:46 pm »
@howardpc: thanks, it was just a quick example, not the real code

ASerge

  • Hero Member
  • *****
  • Posts: 998
Re: Populate a dynamic array
« Reply #24 on: May 01, 2018, 09:58:49 am »
Code: Pascal  [Select]
  1. ...
  2.         SetLength(myArray, Succ(High(myArray)));  // increase size by one
  3. ...
It's equal to SetLength(myArray, Length(myArray)); and does not increase it.
I prefer to use an additional variable:
Code: Pascal  [Select]
  1. H := Length(myArray);
  2. SetLength(myArray, H + 1);
  3. myArray[H] := ...

Thaddy

  • Hero Member
  • *****
  • Posts: 7199
Re: Populate a dynamic array
« Reply #25 on: May 01, 2018, 05:30:04 pm »
Since when? succ(High( increases index. just like +1. Silly conclusion and again not taken the time to test it.
« Last Edit: May 01, 2018, 05:37:02 pm by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

bytebites

  • Full Member
  • ***
  • Posts: 164
Re: Populate a dynamic array
« Reply #26 on: May 01, 2018, 08:30:39 pm »
Code: Pascal  [Select]
  1. writeln(Succ(High(myArray))=Length(myArray));

Thaddy

  • Hero Member
  • *****
  • Posts: 7199
Re: Populate a dynamic array
« Reply #27 on: May 01, 2018, 10:57:11 pm »
Which is correct, but real programmers count from zero...... succ() increases by one. Always.
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.

Bart

  • Hero Member
  • *****
  • Posts: 3167
    • Bart en Mariska's Webstek
Re: Populate a dynamic array
« Reply #28 on: May 01, 2018, 11:55:27 pm »
Since when? succ(High( increases index. just like +1. Silly conclusion and again not taken the time to test it.

That would be simply
Code: Pascal  [Select]
  1. ...
  2.         SetLength(myArray, Succ(High(myArray)));  // increase size by one
  3. ...
  4.  

Did you test your own claim "// increase size by one" at all?

Code: Pascal  [Select]
  1. program Project1;
  2. {$mode objfpc}
  3. {$h+}
  4.  
  5. var
  6.   arr: array of integer;
  7. begin
  8.   setlength(arr,1);
  9.   writeln('Before: Length(arr) = ',length(arr));
  10.   setlength(arr, succ(high(arr)));
  11.   writeln('After : Length(arr) = ',length(arr));
  12. end.

Gives:
Code: [Select]
Before: Length(arr) = 1
After : Length(arr) = 1

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 7199
Re: Populate a dynamic array
« Reply #29 on: May 02, 2018, 01:16:14 pm »
Strange...Since the succ() should not be related to the high in any way at all, but just take the value and return High + 1.
And the inner evaluation has precedence. Or maybe I fell unto my own trap: counting from zero  8-)
« Last Edit: May 02, 2018, 01:18:57 pm by Thaddy »
inline variables like in D10.3 are a bit like Brexit: if you are given the wrong information it sounds like a good idea. Every kid loves candy, but it makes you fat and your teeth will disappear.