Board index » delphi » AllocMem in Delphi

AllocMem in Delphi

In article <4pn4eh$...@newsbf02.news.aol.com>, km...@aol.com says...

Quote

>mem[5]^:=255  OR mem^[5]:=255

To do pointer arithmetic do

mem : PInteger;

:

mem := pointer(cardinal(base) + 5 * SizeOf(Integer));

Will set mem to point to the 5th element in the array.  But there is better.

Quote
>TYPE
>    screen : ARRAY [1..64000] OF INTEGER;  
>                             (* it was actually BYTE (8 bits per pixel)*)
>    scrptr : ^screen;

>This DOES work, however it requires the program to know the size of the
>buffer at compile time. I want to be able to use a dynamic buffer at run
>time without the overhead of a dynamic data structure such as a linked
>list.

Note, you can turn off range check around the references to screen so you can
exceed the param 64000.  I often do this as array[0..0] of stuff because I want
to know if I'm writing to a dynamic array or not.  Then, just turn on/off the
$R- and $R+ params when accessing any element in an array.  It is reference via

scrptr^[20] := 230;

Hope this helps,

Jay Cole

 

Re:AllocMem in Delphi


Hi, can anyone help with the following????

        MemAlloc routines in Delphi (not ver 2)

I tried some test code to see if I could malloc a buffer and then
use the buffer as an array (like in C) in Delphi.
The following code was placed inside the click method of a button.

VAR
        mem : ^INTEGER;

BEGIN
        mem := MemAlloc(1024*2048)
        IF mem = NIL THEN        (* WRITE TO A TEXT FIELD ON FORM *)
           edit1.text:=FALSE;
        ELSE
          BEGIN
           edit1.text:=TRUE;
           mem^:=255;
          END;

END;
etc.

This code worked well for using the first INTEGER value in the buffer.
However I had problems acessing the next n INTEGER's in the buffer.
I tried to index the buffer as I would an array
e.g.
mem[5]^:=255  OR mem^[5]:=255

But the compiler didn't like this.

I then looked at some PASCAL code I had on my hard drive that uses a
buffer
written by the Asphixia demo coders.
It worked like this

TYPE
    screen : ARRAY [1..64000] OF INTEGER;  
                             (* it was actually BYTE (8 bits per pixel)*)
    scrptr : ^screen;

VAR
    scrbase : scrptr;

BEGIN
    GetMEM(scrbase);

  etc.

This DOES work, however it requires the program to know the size of the
buffer at compile time. I want to be able to use a dynamic buffer at run
time without the overhead of a dynamic data structure such as a linked
list.

Can you please help???
Is the second PASCAL method the only way to do ths sort of thing in Delphi
without implementing a linked list or other similar data structure?????

Re:AllocMem in Delphi


Quote
> km...@aol.com (KMGee) wrote in article

<4pn4eh$...@newsbf02.news.aol.com>...

Quote
> Hi, can anyone help with the following????
[snip]
> This code worked well for using the first INTEGER value in the buffer.
> However I had problems acessing the next n INTEGER's in the buffer.
> I tried to index the buffer as I would an array
> e.g.
> mem[5]^:=255  OR mem^[5]:=255
[snip]
> Can you please help???
> Is the second PASCAL method the only way to do ths sort of thing in
Delphi
> without implementing a linked list or other similar data structure?????

There are two ways you can do this.  If you make your buffer a pointer to
an integer (i.e., mem: ^Integer), then you can simply use Inc() and Dec()
on the pointer and the compiler will automatically increment and decrement
by the size of an integer.  Or, alternatively, you can declare an array
type of size 0..0 (i.e., PseudoArray = array[0..0] of Integer), and wrap
your pointer with this type to index into it (i.e., PseudoArray(mem)[5] :=
255).  BUT, this will generate a range check error.  So, you can either
turn off range checking in the compiler options (not advised), or wrap any
function (or code block) which uses the PseudoArray with the {$R-} ...
{$R+} compiler directives to temporarily disable range checking for that
code, then re-enable it.

--
David S. Becker
ADP Dealer Services (Plaza R&D)
mailto:d...@plaza.ds.adp.com
(503) 402-3236

Re:AllocMem in Delphi


Quote
In article <4pn4eh$...@newsbf02.news.aol.com> km...@aol.com (KMGee) writes:
>From: km...@aol.com (KMGee)
>Subject: AllocMem in Delphi
>Date: 12 Jun 1996 15:05:53 -0400
>Hi, can anyone help with the following????

have a look at getmem, which allows you to pass the size of buffer you need.
Quote
>        MemAlloc routines in Delphi (not ver 2)
>I tried some test code to see if I could malloc a buffer and then
>use the buffer as an array (like in C) in Delphi.
>The following code was placed inside the click method of a button.
>VAR
>        mem : ^INTEGER;
>BEGIN
>        mem := MemAlloc(1024*2048)
>        IF mem = NIL THEN        (* WRITE TO A TEXT FIELD ON FORM *)
>           edit1.text:=FALSE;
>        ELSE
>          BEGIN
>           edit1.text:=TRUE;
>           mem^:=255;
>          END;
>END;
>etc.
>This code worked well for using the first INTEGER value in the buffer.
>However I had problems acessing the next n INTEGER's in the buffer.
>I tried to index the buffer as I would an array
>e.g.
>mem[5]^:=255  OR mem^[5]:=255
>But the compiler didn't like this.
>I then looked at some PASCAL code I had on my hard drive that uses a
>buffer
>written by the Asphixia demo coders.
>It worked like this
>TYPE
>    screen : ARRAY [1..64000] OF INTEGER;  
>                             (* it was actually BYTE (8 bits per pixel)*)
>    scrptr : ^screen;
>VAR
>    scrbase : scrptr;
>BEGIN
>    GetMEM(scrbase);
>  etc.
>This DOES work, however it requires the program to know the size of the
>buffer at compile time. I want to be able to use a dynamic buffer at run
>time without the overhead of a dynamic data structure such as a linked
>list.
>Can you please help???
>Is the second PASCAL method the only way to do ths sort of thing in Delphi
>without implementing a linked list or other similar data structure?????

Re:AllocMem in Delphi


In article <borising.601.000BE...@iafrica.com>, Boris Ingram
<boris...@iafrica.com> writes

Quote

>>I then looked at some PASCAL code I had on my hard drive that uses a
>>buffer
>>written by the Asphixia demo coders.
>>It worked like this

>>TYPE
>>    screen : ARRAY [1..64000] OF INTEGER;  
>>                             (* it was actually BYTE (8 bits per pixel)*)
>>    scrptr : ^screen;

>>VAR
>>    scrbase : scrptr;

>>BEGIN
>>    GetMEM(scrbase);

>>  etc.

>>This DOES work, however it requires the program to know the size of the
>>buffer at compile time. I want to be able to use a dynamic buffer at run
>>time without the overhead of a dynamic data structure such as a linked
>>list.

>>Can you please help???
>>Is the second PASCAL method the only way to do ths sort of thing in Delphi
>>without implementing a linked list or other similar data structure?????

Try doing this:

type
  MyArrayType = Array [0..0] of Integer;
  pMyArrayType = ^MyarrayType;
var
  pMyArray: pMyArrayType;

begin
  GetMem (pMyArray, 32000);
  {you should now be able to index into the memory like this }
  pMyArray^[1] := 255; etc
end;

The Range checking must be switched off.

--
Nicholas Robinson

Other Threads