Board index » delphi » a pointer to an allocated byte memory area

a pointer to an allocated byte memory area

I created a byte pointer, and I want to access a given byte in the
allocated memory area it points to. In fact, I would like to be able
to write something like that:

var
   p: ^byte;

begin
     GetMem (p, 100);
     p[24] := $04;
end.

...But this doesn't work. I imagine something else:

type
    TTab = array[1..1000] of byte;
    TpTab = ^TTab;

var
   p:^byte;

begin
     GetMem (p, 100);
     TpTab (p)[24] := $04;
end.

... This works, but it's ugly because I use a pointer to a static type
to access a dynamicly allocated memory area.

Isn't there a better solution ?

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

PYGMALION PROJECT: The ultimate world simulation game

REPLY TO: yrag...@mail.imaginet.fr

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

 

Re:a pointer to an allocated byte memory area


Quote
>type
>    TTab = array[1..1000] of byte;
>    TpTab = ^TTab;

>var
>   p:^byte;

>begin
>     GetMem (p, 100);
>     TpTab (p)[24] := $04;
>end.

>... This works, but it's ugly because I use a pointer to a static type
>to access a dynamicly allocated memory area.

        This is the way it's done. (You say the above works - looks to me like
there's a "^" missing...) There's one thing you can do differently, although
it's irrelevant as far as your "use a pointer to a static type to access a
dynamicly allocated memory area" complaint goes: It would make more sense to me
to say simply

type
    TTab = array[1..1000] of byte;
    TpTab = ^TTab;

var
   p:TpTab;

begin
     GetMem (p, 100);
     p^[24] := $04;
end.

        (Also the convention would be PTab instead of TpTab.)

--
David Ullrich
Don't you guys find it tedious typing the same thing
after your signature each time you post something?
I know I do, but when in Rome...

Re:a pointer to an allocated byte memory area


Quote
yrag...@mail.imaginet.fr wrote:

> type
>     TTab = array[1..1000] of byte;
>     TpTab = ^TTab;

> var
>    p:^byte;

> begin
>      GetMem (p, 100);
>      TpTab (p)[24] := $04;
> end.

> ... This works, but it's ugly because I use a pointer to a static type
> to access a dynamicly allocated memory area.

> Isn't there a better solution ?

Hi (whatever your name is),

I use exactly the same code to access memory as an array. I never found a
better way. This is the difference between Pascal and C, there is no pointer
arithmetics except on PChar, and this is a compromise and not very coherent
to the Pascal philosophy I think.

In old Turbo Pascal for DOS there have been predefined arrays (Mem,
MemW, MemL) for memory access (they worked with Segment:Offset adresses). So
the developers at Borland's also didn't find another way, didn't they?

Volker

Re:a pointer to an allocated byte memory area


Quote
Koenigsbuescher <k...@crossair.ch> wrote:
>yrag...@mail.imaginet.fr wrote:

>> type
>>     TTab = array[1..1000] of byte;
>>     TpTab = ^TTab;

>> var
>>    p:^byte;

>> begin
>>      GetMem (p, 100);
>>      TpTab (p)[24] := $04;
>> end.

>> ... This works, but it's ugly because I use a pointer to a static type
>> to access a dynamicly allocated memory area.

>> Isn't there a better solution ?

I think so. Limiting yourself to Pascal in Delphi is like limiting
yourself to C in C++ ;-)

Quote
>Hi (whatever your name is),
>I use exactly the same code to access memory as an array. I never found a
>better way. This is the difference between Pascal and C, there is no pointer
>arithmetics except on PChar, and this is a compromise and not very coherent
>to the Pascal philosophy I think.
>In old Turbo Pascal for DOS there have been predefined arrays (Mem,
>MemW, MemL) for memory access (they worked with Segment:Offset adresses). So
>the developers at Borland's also didn't find another way, didn't they?

more ways than you are imagining ;-)

Quote
>Volker

How about:
type
    TMyHeapArrayObject = class(TObject)
    public
      MyArrayWithinTheObject:array[1..1000] of byte;
    end;

var
  MyHeapArrayObject : TMyHeapArrayObject;

begin
  MyHeapArrayObject := TMyHeapArrayObject.Create;
  MyHeapArrayObject.MyArrayWithinTheObject[24] := $04;
  // ...
  MyHeapArrayObject.Free;
end.

You can put several arrays in the object too, and
you can use shorter names, of course, so it looks more like
  aha:= THArr.Create;
  aha.x[24] := $04;
  aha.y[42] := $40;
  aha.Free;
And you could use a default array property in the object to
make the assignment syntax look just like that of an ordinary array:
  a[24]:=$04;
Properties also let you write arbitrary code that will get executed
when the array is read or write accessed. This is neat, but introduces
overhead. A plain array in the object should be as efficient as what
you do with pointers to get-mem areas.

Regards,
Bengt Richter

Re:a pointer to an allocated byte memory area


Quote
In article <318FDAD5.2...@crossair.ch>, Koenigsbuescher (k...@crossair.ch) writes:
>better way. This is the difference between Pascal and C, there is no pointer
>arithmetics except on PChar, and this is a compromise and not very coherent

Huh?  You managed to make me doubt...   I just tested inc(p) where
p was a pointer to a longint.  Worked like a charm.

--
Holger Friedrich
Chemnitz, Germany
E-Mail:         h...@glare.in-chemnitz.de
Second chance:  easy!glare...@thunder.hrz.tu-chemnitz.de

Other Threads