Question: Dynamic arrays in Turbo Pascal 
Author Message
 Question: Dynamic arrays in Turbo Pascal

Maby this message doesn't belong in this group, but I guess that it
wouldn't hurt to ask anyway.

Does any experienced programmer know how to create dynamic arrays in Turbo
Pascal and is the same possible in ANSI Pascal?

I will really appreciate an answer. Would be nice if you could include a
code fragment as well.

Thanks again in advance.

                        Alex Khinkis




Sun, 30 Apr 2000 03:00:00 GMT  
 Question: Dynamic arrays in Turbo Pascal

Well,

you can say

Const
  MaxSize = 1000 ;

Type
  Elem = Record
    Something : Integer ;
    More : Double ;
  End ;
  TMyArray = Array[1..MaxSize] of Elem ;
  PMyArray = ^TMyArray.

which declares a "regular" type. Then, when you decide you need 500
Elements, you allocate just enough for these:

Var
  P : PMyArray ;
 ...
GetMem (P,SizeOf (Elem)*500).
P^[1].Something:=1 ;
...

Of course, all you get is a dynamic array. You dont get management
routines for re-sizing array size of for moving elements.

You might want to look at TObjects.TCollection. That is a container,
implemented as a variably-sized array with insertion, deletion, sorting and
searching of elements, supporting the process of "growing" and "shrinking"
the array (that is implemented as creating a newly sized copy of the array
and copying all elements from the original to the copy, then disposing the
original. This still is quite fast since the item array is an array of
pointers to the elements, not of the elements themselves).

I use TCollection descendants all the time. Meanwhile, I implemented more
TCollection variants (one that support 2^32 elements, one that interfaces
to a tradititional linked list, and more) and dont do any other dynamic
lists.

ANSI Pascal does not have dynamic arrays (as far as I know).



Quote:
> Does any experienced programmer know how to create dynamic arrays in
Turbo
> Pascal and is the same possible in ANSI Pascal?



Mon, 01 May 2000 03:00:00 GMT  
 Question: Dynamic arrays in Turbo Pascal


Quote:
>ANSI Pascal does not have dynamic arrays (as far as I know).

Extended Pascal from 1989 has a feature called schema types that
give you true run-time sized variables.

First you define a template for the family of types you want:

type TMyArray(p:integer) = array [1..p] of elem;

then you can 'select' (discriminate is the standard's term for it)
any of the types at run-time.

Alternative 1)
var PMyArray = TMyArray(500);

Alternative 2)
var PMyArray = ^TMyArray;
new(PMyArray, 500);

or even

Alternative 3)
procedure x(a,b:integer);
  var PMyArray : TMyArray(a*b);

You can do lots of interesting things with schema types...

The nice thing about schema types is that you still get all the
run-time bounds checking (if you want it) for the array indexing.
With the "GetMem" hack that you need in BP, you have to give up
the bounds checking.

--
John Reagan
DEC Pascal Project Leader
Application Compilers and Environments
Digital Equipment Corporation

Disclaimer:  The opinions and statements expressed by me are not
             necessarily those of Digital Equipment Corporation.
--



Mon, 01 May 2000 03:00:00 GMT  
 Question: Dynamic arrays in Turbo Pascal

Quote:
>Does any experienced programmer know how to create dynamic arrays in Turbo
>Pascal and is the same possible in ANSI Pascal?

OK.  I am not quite sure I understand your question.  When you say dynamic
 arrays, do you mean an array that gets memory allocated for it at runtime, or
 do you mean an array whose length changes dynamically.  Either way, here are a
 few posibilities.

First, if you want an array that gets memory allocated for it at runtime, then
 just use a pointer to an array.  e.g.

TYPE
     PArrayType = ^ArrayType;
     ArrayType = ARRAY [ 0..10 ] OF INTEGER;
VAR
     pArray : PArrayType;
     i : INTEGER;
BEGIN
     NEW ( pArray );
     FOR i := 0 TO 10 DO
           pArray^[i] := i;
     DISPOSE ( pArray)  {Always remember to dispose of pointers}
END.

All this does is declare an array of 11 elements and fills it with the numbers
 0 through 10.  This is a dynamic structure because memory to hold pArray
 doesn't get allocated until the program rexecutes the statement "NEW ( pArray
 );".  When it reaches "DISPOSE ( pArray )" it frees up the memory used by
 pArray to be used by the rest of your program.

Now, if you meant an array whose length changes dynamically, this is a little
 more complex but it possible (sort-of).  Also, it is more like a linked list
 that can be accessed by indexes. (indicies?)

What you need is a pointer to an array whose range is [1 TO 1 ]
e.g.:
TYPE
     PArrayType = ^ArrayType;
     ArrayType = ARRAY [ 1 TO 1 ]  OF INTEGER;
VAR
     pArray : PArrayType;
     length, i : INTEGER;

Now, instead of using NEW to allocate memory, use GetMem.

BEGIN
     length := 10;
     GetMem ( pArray, length * SizeOf ( INTEGER ) );
     FOR i :=  1 TO length DO
           pArray^[i] := i;
      FreeMem ( pArray, length * SizeOf ( INTEGER ) );  {same as              

           DISPOSE}
END.

This creates a pointer to an array of 1, but it allocates memory for 10
 elements.  You can access these elements with indicies just like an array (you
 may have to take range checking off).  This is a lot more complicated and you
 have to be carefull because it is easy to go past the end of the array or such
 and that can cause big problems.

I hope this answered your question.  If you have anymore questions just ask.

-Kanryl



Tue, 02 May 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Dynamic array question ...

2. Dynamic Arrays in Borland Pascal

3. dynamic array

4. Dynamic array in database

5. How to store dynamic array to memofield?

6. Dynamic Type array

7. Dynamic Array puzzle

8. How to declare dynamic array in fpc ??

9. dynamic arrays

10. Memory access; relative position (dynamic array)

11. Heap error while allocating dynamic array in protected.

12. How do you do a dynamic array in oascal

 

 
Powered by phpBB® Forum Software