Link to home
Start Free TrialLog in
Avatar of Elvis1
Elvis1

asked on

Dynamic Array

Hi,

how should i make a dynamic array in pascal ???
how do i code that ? should by a pointer array also ...
according to that site http://deadlock.et.tudelft.nl/~fpc/docs-html/ref/refsu14.html it should be something like

PROGRAM Test;
Type
 DynArray:Array Of Byte;

Var
 a:^DynArray;

Begin
 New(a)
 SetLength(A, 10);
 ...
 Dispose(a);
End.


can someone help me out of that ???

Elvis1
Avatar of BdLm
BdLm
Flag of Germany image

what about this demo:
c
onst   max= 512;
type     MyRecord = record
                               S1: String;
                               i1 : Integer;
                              end;

            MyRecordPtr =^MyRecord;
           

var     RecordCount  :  Integer;
          MyRecordArray : array[1...max] of   MyRecordPtr;

procedure CreateNewRecord;
begin
       inc(recordCount)
       New(MyRecordArray[recordCount]);

end;


read Pointer:
      if MyRecordArray[i] = nil then writeln ('no data here')
                                       else
                                      writeln( 'data of S1:'  MyRecordArray[i]^ .S1);
Avatar of Elvis1
Elvis1

ASKER

i don't need all this stuf, can you just give me the code of the pseudocode i said abose ???

thanks
Elvis
you need a pseudo implementation of New, Dispose & Setlength ?
That came from the FreePascal manual, which Turbo Pascal is not compatable with (I'll assume you're using that since you didnt specify, and it's the most popular)

So to use that code, you have to switch to FreePascal.  If you want to stick with your compiler, the code will be significantly "uglier" since you'll have to do things the way BdLm suggested.
Avatar of Elvis1

ASKER

To MannSoft,
yes that is the point, i use Turbo Pascal 7.0, but how do i "translate" the snip of code above ???
Thanks a lot

Elvis
Well that's the problem...since TP7 doesn't support dynamic arrays, you can't really translate it, it requires a whole different approach.

The most common approach is to create a static array as big as you'll ever need.  But if you might need a huge number of elements, that won't work because you just can't fit 1000 strings on the stack (for example).  

So that's where BdLm's suggestion comes in.  Instead of allocating a huge array of strings, you allocate a huge array of pointers to strings.  This takes up much less space on the stack, so will fit (again there is a limit, but you will definitely be able to fit more pointers to strings than actual strings).  Then when you want to increase the size of the array, you New() another string pointer, which creates the actual string on the heap (which is much larger than the stack).  When you want to shrink the array, you Dispose() the string pointer.

It's all a little clumsy at first, but once you get the hang of it, it's not so bad.  I agree that a truly dynamic array would be much easier, but as I said, you'd have to switch compilers for something like that.
Avatar of Elvis1

ASKER

I understand a bit more now,
but can you show me how should i code array of string pointer ???
thanks

ELvis1
ASKER CERTIFIED SOLUTION
Avatar of MannSoft
MannSoft

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike McCracken
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I will leave the following recommendation for this question in the Cleanup topic area:
   Split - Centauri(11039405) & Mannsoft(10963261)

Any objections should be posted here in the next 4 days. After that time, the question will be closed.

mlmcc
Pascal PE