Memory access; relative position (dynamic array)
Author Message
Memory access; relative position (dynamic array)

Hi!

I've got a dynamically allocated variabel "x" of type ^byte, which is
allocated with GetMem function. The total size of the allocated memory
is always greater than 1 byte (typically several thousands).
I want the byte at the n:th position from the allocated memory; in C it
would be rather easy to accomplish, but how can I do this in Pascal?

/tj

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)

Quote:

> I've got a dynamically allocated variabel "x" of type ^byte, which is
> allocated with GetMem function. The total size of the allocated memory
> is always greater than 1 byte (typically several thousands).
> I want the byte at the n:th position from the allocated memory; in C it
> would be rather easy to accomplish, but how can I do this in Pascal?

In turbo Pascal, you need a typecast:

type
pbytearray = ^tbytearray;
tbytearray = array[0..65519] of byte;

var x: ^byte;

begin
...
writeln(pbytearray(x)^[9999]);
...
end.

Under freepascal (<http://www.freepascal.org>), you can use x^[9999]
and (x+9999)^, like in C (this also works for words, longints etc, the
value you add to x is multiplied by sizeof(x)).

Jonas

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)

Quote:
>In turbo Pascal, you need a typecast:

>type
>  pbytearray = ^tbytearray;
>  tbytearray = array[0..65519] of byte;

>var x: ^byte;

>begin
>  ...
>  writeln(pbytearray(x)^[9999]);
>  ...
>end.

>Under freepascal (<http://www.freepascal.org>), you can use x^[9999]
>and (x+9999)^, like in C (this also works for words, longints etc, the
>value you add to x is multiplied by sizeof(x)).

x[9999] probably. ^(Simple types) don't need^

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)

Quote:

> >type
> >  pbytearray = ^tbytearray;
> >  tbytearray = array[0..65519] of byte;

> >var x: ^byte;

> >begin
> >  ...
> >  writeln(pbytearray(x)^[9999]);
> >  ...
> >end.

> >Under freepascal (<http://www.freepascal.org>), you can use x^[9999]
> >and (x+9999)^, like in C (this also works for words, longints etc, the
> >value you add to x is multiplied by sizeof(x)).

> x[9999] probably. ^(Simple types) don't need^

Indeed, typo.

Jonas

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)

Quote:
> Hi!

> I've got a dynamically allocated variabel "x" of type ^byte, which is
> allocated with GetMem function. The total size of the allocated memory
> is always greater than 1 byte (typically several thousands).
> I want the byte at the n:th position from the allocated memory; in C
> it would be rather easy to accomplish, but how can I do this in
> Pascal?

{\$G+}
function ptr_2_long(p: pointer): longint; assembler;
asm
mov   ax, word ptr p
mov   dx, word ptr p + 2
mov   bx, dx
shr   dx, 12
shl   bx, 4
and   dx, 0fh
end; {ptr_2_long}

function long_2_ptr(l: longint): pointer; assembler;
asm
mov   ax, word ptr l
mov   dx, word ptr l + 2
shl   dx, 12
mov   bx, ax
shr   bx, 4
and   ax, 0fh
end; {long_2_ptr}

procedure incptr(var p; w: word); assembler;
asm                       {^^^^}
les   di, p
mov   ax, es:[di]
mov   dx, ax
shr   dx, 4
and   ax, 0fh
mov   bx, ax
shr   bx, 4
and   ax, 0fh
mov   es:[di], ax
mov   es:[di + 2], dx
end; {incptr}

procedure decptr(var p; w: word); assembler;
asm                       {^^^^}
les   di, p
mov   ax, es:[di]
mov   dx, ax
shr   dx, 4
and   ax, 0fh
sub   dx, 00fffh
sub   ax, w
mov   bx, ax
shr   bx, 4
and   ax, 0fh
mov   es:[di], ax
mov   es:[di + 2], dx
end; {decptr}

Robert
--
Robert AH Prins

Sent via Deja.com http://www.deja.com/

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)

Quote:
>> allocated with GetMem function. The total size of the allocated memory
>> is always greater than 1 byte (typically several thousands).
>> I want the byte at the n:th position from the allocated memory; in C
>> it would be rather easy to accomplish, but how can I do this in

In a 16-bit C? How would you do that so easily?

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)
U can use this, it is simple and needs NO typecasting (because typasting
may give you unwanted resutls if u need more memory than your typecasted
variable....

var MyMem : Pointer;
pbyte : ^byte;
begin
Getmem (MyMem,60000 {or how much you need});
pByte:=Mymem;
// go no to Nth element
inc(pByte,n)
// Put the value 5 on nth element
pByte^:=5;
Freemem (MyMem);
end;

Of course, you need to write more code if you need to use Pbyte lots of
times ( because every time u have to do : Pbyte:=Mymem;  inc (PByte,n);  )

Lazikas.

Quote:

> Hi!

> I've got a dynamically allocated variabel "x" of type ^byte, which is
> allocated with GetMem function. The total size of the allocated memory
> is always greater than 1 byte (typically several thousands).
> I want the byte at the n:th position from the allocated memory; in C it
> would be rather easy to accomplish, but how can I do this in Pascal?

> /tj

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)
On Mon, 24 Jan 2000 13:02:55 +0100, Thomas Jonsson

Quote:

> I've got a dynamically allocated variabel "x" of type ^byte, which is
> allocated with GetMem function. The total size of the allocated memory
> is always greater than 1 byte (typically several thousands).
> I want the byte at the n:th position from the allocated memory; in C it
> would be rather easy to accomplish, but how can I do this in Pascal?

Instead of a ^byte define a pointer to array and get as much memory
for it as you need.

type
pbarr=^tbarr;
tbarr=array[0..65519] of byte;

var
x:^pbarr;
begin
getmem(x,1000); { x^[0]...x^[999] are now usable }
...
x^[999]:=42;
...
freemem(x,1000);
end.

Regards
Horst

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)

Quote:
> >> allocated with GetMem function. The total size of the allocated memory
> >> is always greater than 1 byte (typically several thousands).
> >> I want the byte at the n:th position from the allocated memory; in C
> >> it would be rather easy to accomplish, but how can I do this in

> In a 16-bit C? How would you do that so easily?

p[n]

It's only several thousands of *bytes* at the most - a 16-bit compiler
should be able to handle that. Did you by any chance read kilobytes into
that?

FP

Wed, 18 Jun 1902 08:00:00 GMT
Memory access; relative position (dynamic array)

Quote:
> p[n]

> It's only several thousands of *bytes* at the most - a 16-bit compiler
> should be able to handle that. Did you by any chance read kilobytes into
> that?

I guess I did, or mixed the original message up with Robert Prins' (in
which he gives a 32-bit workaround)

Wed, 18 Jun 1902 08:00:00 GMT

 Page 1 of 1 [ 10 post ]

Relevant Pages