Dynamic array question ... 
Author Message
 Dynamic array question ...

I have a problem which could look silly to some of you
Pascal experts, but it is quite troublesome for me.

I need an array of 'something' of an arbitrary size.

As my C is much better than my Pascal, here's the solution
for it in C:

- declare a pointer to 'something'
- allocate memory for a certain number of 'something' and
  associate the returned address with the pointer
- access the desired item with the 'pointer[index]' syntax

When trying to accomplish this task in Pascal (Delphi 2.0),
a get an error saying that 'pointer[index]' is not allowed
because I shouldn't index anything but arrays!

OK compiler, be it your way. My next try is (substituting
'something' for integer) ...

type
  TArray = array[0..NumberOfItems] of integer;

... but helas, this doesn't work either because compiler
complains about 'NumberOfItems' not being a constant!
(In my code there's no way fot it to be a constant.)

Well, that's it.
Many thanks for the help,
        Marko

-------------------------
Marko Curko



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array question ...

Quote:

>I have a problem which could look silly to some of you
>Pascal experts, but it is quite troublesome for me.

>I need an array of 'something' of an arbitrary size.

The typical Pascal solution is to define an array having a maximum
number of items and a type pointer to that array.  

Quote:
>As my C is much better than my Pascal, here's the solution
>for it in C:
>- declare a pointer to 'something'

TYPE trSomething = <appropriate record definition>;
     tpSomething = ^tSomething;
     tSomething = Array[1..65521 div Sizeof(trSomething)] of trSomething;

VAR Something: tpSomething;

or a zero-based array:

     tSomething = Array[0..65520 div Sizeof(trSomething)] of trSomething;

Quote:
>- allocate memory for a certain number of 'something' and
>  associate the returned address with the pointer

   GetMem(Something, longint(NumberofElements) * Sizeof(Something^[1]));

Quote:
>- access the desired item with the 'pointer[index]' syntax

   SomeThing^[Index]

Note the longint typecast for NumberOfElements to prevent the
the evaluation from overflowing when NumberOfElements *
Sizeof(trSomething) is larger than 32767!

    ...red



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array question ...


Quote:
> TYPE tSomething = Array[1..65521 div Sizeof(trSomething)] of trSomething;

> or a zero-based array:

>      tSomething = Array[0..65520 div Sizeof(trSomething)] of trSomething;

I don't want to be too pedantic, but this should rather be
       tSomething = Array[0..65521 div Sizeof(trSomething)-1] of trSomething;
Otherwise you'll get a problem with somewhat bigger Somethings.

Quote:
> >- allocate memory for a certain number of 'something' and
> >  associate the returned address with the pointer

>    GetMem(Something, longint(NumberofElements) * Sizeof(Something^[1]));

> Note the longint typecast for NumberOfElements to prevent
> the evaluation from overflowing when NumberOfElements *
> Sizeof(trSomething) is larger than 32767!

>     ...red

Wouldn't a Word typecast suffice here? Why use 32 bit when the result can only
be 16 bit anyway? If NumberOfElements is of type Word, no cast is necessary at
all, I think.

Frank



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array question ...

Quote:


>> TYPE tSomething = Array[1..65521 div Sizeof(trSomething)] oftrSomething;

>> or a zero-based array:

>>      tSomething = Array[0..65520 div Sizeof(trSomething)] oftrSomething;

>I don't want to be too pedantic, but this should rather be
>       tSomething = Array[0..65521 div Sizeof(trSomething)-1] oftrSomething;
>Otherwise you'll get a problem with somewhat bigger Somethings.

You are correct.

Quote:
>> >- allocate memory for a certain number of 'something' and
>> >  associate the returned address with the pointer

>>    GetMem(Something, longint(NumberofElements) *Sizeof(Something^[1]));

>> Note the longint typecast for NumberOfElements to prevent
>> the evaluation from overflowing when NumberOfElements *
>> Sizeof(trSomething) is larger than 32767!

>>     ...red

>Wouldn't a Word typecast suffice here? Why use 32 bit when the resultcan only
>be 16 bit anyway? If NumberOfElements is of type Word, no cast isnecessary at
>all, I think.

Of course this is also correct.  However, NumberOfElements wasn't
specified in the example so IMO a typecast is appropriate. If you
were testing to see if the structure was too large, a longint would
be required, otherwise a word would suffice.  The actual type to
use is one of preference.  You chose the more efficient for the
task :)

    ...red



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array question ...


Quote:

>I have a problem which could look silly to some of you
>Pascal experts, but it is quite troublesome for me.

>I need an array of 'something' of an arbitrary size.

>As my C is much better than my Pascal, here's the solution
>for it in C:

>- declare a pointer to 'something'
>- allocate memory for a certain number of 'something' and
>  associate the returned address with the pointer
>- access the desired item with the 'pointer[index]' syntax

>When trying to accomplish this task in Pascal (Delphi 2.0),
>a get an error saying that 'pointer[index]' is not allowed
>because I shouldn't index anything but arrays!

You need to define the pointer as a pointer to the array not to the
element. The size of the array should be maximum possible (making the
array little under 64K) to avoid range checks (alternatively you could
use just array[0..0] and make sure that range checking is off, but that
is IMO not so nice).

After you have declared the pointer, you use it as pointer^[index]. In
Pascal arrays and pointers are separate things and arrays are treated in
most cases like simple variable. (parameter passing for example).

Quote:
>OK compiler, be it your way. My next try is (substituting
>'something' for integer) ...

>type
>  TArray = array[0..NumberOfItems] of integer;

What you need is something like

type
  TArray = array[0..65520 div sizeof(integer)] of integer;
  PArray = ^Tarray;

var a:PArray;
...

    getmem ...
     ...
    a^[index]:=x
    ...

Osmo



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array question ...

Pascal, being quite strong on the type checking, does not allow the
method you use in C for a dynamic array... in fact, it provides no
provisions for dynamic arrays(Standard pascal, anyway, perhaps there
is an extension in some version).  Linked lists/Skips lists/Binary
trees are your best alternatives... (or, of course, a large array
which you know is wasting memory but at least it works. :) )

Sorry to bear bad news!

-Travis

** Travis J. Galoppo
** CS Student - Niagara University
** Programmer - NASA *EOS* (OTD/LIS missions)  (http://wwwghcc.msfc.nasa.gov)
** (http://www.niagara.edu/~s943022)



Wed, 18 Jun 1902 08:00:00 GMT  
 Dynamic array question ...


: Pascal, being quite strong on the type checking, does not allow the
: method you use in C for a dynamic array... in fact, it provides no
: provisions for dynamic arrays(Standard pascal, anyway, perhaps there
: is an extension in some version).  Linked lists/Skips lists/Binary
: trees are your best alternatives... (or, of course, a large array
: which you know is wasting memory but at least it works. :) )

: Sorry to bear bad news!

No bad news, and Borland Pascal (Turbo pacal) DOES have dynamic arrays,
and dynamic variables.. jsut gotta know how to use them.. it's simple..
I'm writing this online, so it's something like this

Type
    tdArray = Array[0..40960] of Byte;
    ptdArray = ^tdAray;

Var
   Buffer: ptdArray;

PROCEDURE Init_Buffer(Var ThisOne: Pointer; ThisMuch: Word);
          Begin
               GetMem(ThisOne, ThisMuch);
          End;

PROCEDURE Deinit_Buffer(Var ThisOne: Pointer; ThisMuc: Word);
          Begin
               FreeMem(ThisOne, ThisMuch);
          End;

BEGIN
     Init_Buffer(Buffer, 2048);
     {....
       ...

    use it somehow here

 ...
....}
     Deinit_Buffer(Buffer, 2048);
END.

There ya go.. I think that should run perfectly.. it's a maximum of 40k
array.. but I've only allocated 2k of it.. You can do is for a lot of
things!... Ta-Da..

Like I say, you gotta do things in programming nobody would think of but
has done already

Later,
Bojan Landekic
--
-----
The meaning of life is to help others for the sheer pleasure of helping them.
I live by this, and so far, I'm very happy... try it!



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

 Relevant Pages 

1. Question: Dynamic arrays in Turbo Pascal

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