dynamic arrays 
Author Message
 dynamic arrays

I need help creating dynamic structures or arrays. Could someone point me to
some example code or a book.  For example, how would you dynamically create an
array of integers at runtime?

Thanks,

Ruel Little



Wed, 18 Jun 1902 08:00:00 GMT  
 dynamic arrays

Quote:
> I need help creating dynamic structures or arrays. Could someone point me to
> some example code or a book.  For example, how would you dynamically create an
> array of integers at runtime?

   First, realize that Pascal does not support dynamic array definition,
and what you can do subverts the language - the compiler must know how
large every declaration/definition is.  Given that, Borland has provided
some features which allow us to bypass some of the checking and control
which with language imposes.
   Next, you must work within the PC architecture, which limits
addressing to 64K.  Thus, you can define/declare some data for special
purposes (you're going to use it as an array which you control), but it
must be 64K or less.
   Now, to your specific point, there are 2 basic things you can do:
declare an array of the largest possible type/dimension and allocate
only whatever part of it applies to the situation; or declare an array of
_no_ dimension ([0..]) and allocate what's actually needed, but also
override Range Checking.  Both techniques have their risks, and both
should probably be done on the Heap, to avoid overusing the 64K Data
segment.
   In your specific case, here's what you can do:

Type Int_Array_Type = array[1..(65520 div SizeOf(Integer)); { maximum }
Var  IATPtr         : ^Int_Array_Type;     { pointer to dynamic array }
....
  I := 20000;                                    { dimension of array }
  GetMem (IATPtr,I*SizeOf(Integer));         { allocate dunamic array }
....
  for J := 1 to I do IATPtr^[J] := 0;                   { clear array }

   I won't post the "undefined dimension" variation, since it demands
overriding Range Checking and thus is more risky.  However, it follows
the same concept as the above, since GetMem is used to allocate a
variable amount of array area.



Wed, 18 Jun 1902 08:00:00 GMT  
 dynamic arrays


Quote:

>I need help creating dynamic structures or arrays. Could someone point me to
>some example code or a book.  For example, how would you dynamically create an
>array of integers at runtime?

Just use Getmem.

type integerarray=array [1..1] of integer;
var p:^Intergerarray;
...
  getmem(p,elements_wanted*sizeof(integer));
  ...
  (process p^)
  ...
  freemem(p,elementts_wanted*sizeof(integer));

Set the range checking off. ( {$R-} )

Quote:
>Thanks,

>Ruel Little


Osmo


Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. dynamic array

2. Dynamic array in database

3. How to store dynamic array to memofield?

4. Dynamic Array puzzle

5. How to declare dynamic array in fpc ??

6. Memory access; relative position (dynamic array)

7. Heap error while allocating dynamic array in protected.

8. How do you do a dynamic array in oascal

9. Dynamic arrays?????

10. Question: Dynamic arrays in Turbo Pascal

11. Dynamic arrays...

12. dynamic arrays

 

 
Powered by phpBB® Forum Software