Dynamic array declaration 
Author Message
 Dynamic array declaration

Hope someone can give me some help with this:

Is there any way to use a variable to dimension an array in BP7?

I know you can't use statements such as :

procedure Gubbins(Size : integer);
var anArray : array[1..Size] of integer;
begin
...
end;

So can I get round this? - is there a method, using pointers, of creating
a variable sized array dynamically?

Any help much appreciated - if there isn't a way to do this, I may have to use
C++ (*/gasp*/) !! E-mail or newsgroup response is fine by me.

Si.
--
*******************************************************************************
* SIMON J. WALKE-------------UNIVERSITY OF EXETER-----------DEPARTMENT OF MSOR*
*******************************************************************************
* Thought for the week: "Be Alert - your country needs lerts."                *
*******************************************************************************

*******************************************************************************



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array declaration

Quote:

>Hope someone can give me some help with this:
>Is there any way to use a variable to dimension an array in BP7?
>I know you can't use statements such as :
>procedure Gubbins(Size : integer);
>var anArray : array[1..Size] of integer;
>begin
>...
>end;
>So can I get round this? - is there a method, using pointers, of creating
>a variable sized array dynamically?
>Any help much appreciated - if there isn't a way to do this, I may have to use
>C++ (*/gasp*/) !! E-mail or newsgroup response is fine by me.

I'm not sure if this is possible either.  If you find out, please let
me know.  The solution I'm using right now is to create an array of
pointers as big as I think my array can get, and then assign the
pointers when I need to add to the array.

For example:

type    FormInformation = record
                psFormName : PString;
                psFormDescription : PString;
        end;

var     MyFormInfo : array[1..300] of FormInformation;

begin   ...
        MyFormInfo[ index ].psFormName := NewStr( 'AFORMNAME');
        MyFormInfo[ index ].psDescription := NewStr( 'AFORMDESCRIPTION');
        ...
        DisposeStr(MyFormInfo[index].psFormName);
        DisposeStr(MyFormInfo[index].psFormName);
end;

At least this way, the worst that can happen is that I've only wasted
memory on an array of 300 pointers instead of 300 form names and
descriptions.

Good luck, Mark.

...........................................................................................................................................................................................................................................................



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array declaration
Quote:

>Is there any way to use a variable to dimension an array in BP7?

>I know you can't use statements such as :

>procedure Gubbins(Size : integer);
>var anArray : array[1..Size] of integer;
>begin
>...
>end;

>So can I get round this? - is there a method, using pointers, of creating
>a variable sized array dynamically?

[snip]

Funny you should ask about dynamic arrays.

I've had the same problem for years and have been working around it
in various ways, all of which have seemed reasonable, but none of
which have turned out to be great {:-(} ...

The simplest way is to declare an array of pointers and then dynamically
allocate memory for each element that you need to use. The main drawback is
that your data array can never have more elements than the number of
pointers you've declared. This number, of course, is compiled into the
program (or unit). Other disadvantages are: [1] if you don't use all those
pointers then you're wasting memory, [2] you can't declare an array of more
than 16K pointers (because such an array would be larger than 64K), and [3]
this technique adds a level of indirection, making your program (at least
a bit) more difficult to write, debug and maintain.

If you're willing (and able) to work within these limitations, the
array-of-pointers method described above will do the job. Otherwise,
you'll have to get a little bit fancier.

Recently I've been working on an object-oriented array-simulator. I'm
not quite finished with it, but so far it allows me to create array-like
objects with the following properties:

[1] the size of each element can be anywhere from 1 to 65520 bytes.
[2] the number of elements is limited only by the memory available.
[3] no memory is wasted (for example, there are no pointers to unused
array elements).

If you think this will help you, let me know (via e-mail, please!).
The code is not quite finished, but I'm willing to share it, and you
can adapt it if necessary.


Postings reflect my opinions, not those of my employer.
(I don't even use Pascal at work.)



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array declaration

Quote:

>Hope someone can give me some help with this:
>Is there any way to use a variable to dimension an array in BP7?
>I know you can't use statements such as :
>procedure Gubbins(Size : integer);
>var anArray : array[1..Size] of integer;
>begin
>...
>end;
>So can I get round this? - is there a method, using pointers, of creating
>a variable sized array dynamically?
>Any help much appreciated - if there isn't a way to do this, I may have to use
>C++ (*/gasp*/) !! E-mail or newsgroup response is fine by me.

It depends on the size of the elements. If the elements are records,
which take considerable more memory than a pointer, I recommend to use
"collections", which are actually dynamically growing arrays. However,
if just want an array of integer, the use of "collections " might
introduce too much overhead, since for every integer (2 bytes) a
pointer (4 bytes) is required. In this case you could declare an array
type with the maximum number of elements but allocate only as many
bytes as needed.

type
  TIntArray = array[1..32767] of integer;
  PIntArray = ^TIntArray;

var
  A: PIntArray;
  ElementCount: integer;

begin
 {...}
  GetMem(A,ElementCount*SizeOf(integer));
  {...}
  FreeMem(A,ElementCount*SizeOf(integer));
  {...}
end.

To change the number of elements, you can do the following

1. Allocate an array with the new number of elements
2. Move the elements of the old array to the new array
3. Dispose off the old array.



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array declaration
: Hope someone can give me some help with this:

: Is there any way to use a variable to dimension an array in BP7?

: I know you can't use statements such as :

: procedure Gubbins(Size : integer);
: var anArray : array[1..Size] of integer;
: begin
: ...
: end;

: So can I get round this? - is there a method, using pointers, of creating
: a variable sized array dynamically?

Sure there is.  Try this :

procedure Gubbins(Size : integer);
type TBigArray = Array[1..16384] of Integer;  { Maximum 65536-16 bytes }
var anArray : ^TBigArray;
begin
GetMem(anArray,Size * SizeOf(Integer));

 ... use anArray^[X] instead of anArray[X] ...

FreeMem(anArray,Size * SizeOf(Integer));
end;

: Any help much appreciated - if there isn't a way to do this, I may have to use
: C++ (*/gasp*/) !! E-mail or newsgroup response is fine by me.

Oh lord no, don't use C++ !!!

Erik Turner



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

 Relevant Pages 

1. Constants in an Array declaration.

2. dynamic array

3. Dynamic array in database

4. How to store dynamic array to memofield?

5. Dynamic Type array

6. Dynamic Array puzzle

7. How to declare dynamic array in fpc ??

8. dynamic arrays

9. Memory access; relative position (dynamic array)

10. Heap error while allocating dynamic array in protected.

11. How do you do a dynamic array in oascal

12. Dynamic (list-like) arrays in Delphi

 

 
Powered by phpBB® Forum Software