Board index » delphi » accessing memory allocated with GETMEM

accessing memory allocated with GETMEM

How do I access the memory allocated by this statement :

getmem(apointer, 1024);

Please don't tell me it works like this :

apointer^[0] := 56;

because this doesn't work - I'm using TP6 if it helps.

squigger hops again

 

Re:accessing memory allocated with GETMEM


Quote
squigger wrote:

> How do I access the memory allocated by this statement :

> getmem(apointer, 1024);

> Please don't tell me it works like this :

> apointer^[0] := 56;

> because this doesn't work - I'm using TP6 if it helps.

You should declare or typecast apointer to whatever it
is you're storing in your memory.  The most general way
would be:

type
   memarray = array[0..$FFF0] of byte;
   memptr = ^memarray;
var
   apointer : memptr;

...

 getmem(apointer, 1024);
 apointer^[57] := 42;

Not that in this general case apointer^[1025] will not
give you a range check error.

Hope this helps,
Jochen

Re:accessing memory allocated with GETMEM


In article <332FD303....@berlin.snafu.de>, joch...@berlin.snafu.de
says...

Quote

>squigger wrote:

>> How do I access the memory allocated by this statement :

>> getmem(apointer, 1024);

>> Please don't tell me it works like this :

>> apointer^[0] := 56;

>> because this doesn't work - I'm using TP6 if it helps.

>You should declare or typecast apointer to whatever it
>is you're storing in your memory.  The most general way
>would be:

>type
>   memarray = array[0..$FFF0] of byte;
>   memptr = ^memarray;
>var
>   apointer : memptr;

>...

> getmem(apointer, 1024);
> apointer^[57] := 42;

>Not that in this general case apointer^[1025] will not
>give you a range check error.

>Hope this helps,
>Jochen

Thanks.  Now, how do I use this for a random amount (as in a sprite
programme)?
Or can't it be done (wouldn't surprise me...)

squigger hops again

Re:accessing memory allocated with GETMEM


On 19 Mar 97 22:02:36 GMT, jas...@athena.sunitafe.edu.au (squigger)
wrote:

Quote
>In article <332FD303....@berlin.snafu.de>, joch...@berlin.snafu.de
>says...

>>squigger wrote:

>>> How do I access the memory allocated by this statement :

>>> getmem(apointer, 1024);

>>> Please don't tell me it works like this :

>>> apointer^[0] := 56;

>>> because this doesn't work - I'm using TP6 if it helps.

>>You should declare or typecast apointer to whatever it
>>is you're storing in your memory.  The most general way
>>would be:

>>type
>>   memarray = array[0..$FFF0] of byte;
>>   memptr = ^memarray;
>>var
>>   apointer : memptr;

>>...

>> getmem(apointer, 1024);
>> apointer^[57] := 42;

>>Not that in this general case apointer^[1025] will not
>>give you a range check error.

>>Hope this helps,
>>Jochen

>Thanks.  Now, how do I use this for a random amount (as in a sprite
>programme)?
>Or can't it be done (wouldn't surprise me...)

>squigger hops again

Try the MOVE procedure ...

move(var source, dest; count : word)

move copies a block of  'count' bytes starting at the first byte of
'source' into the space starting at the first byte of  'dest'  .  

'source' could be the entity you generate w/ getmem,  an element
within an array, a position within a string, etc.  likewise with
'dest'.

BTW, using the sizeof  function is the safe way to define count

Nick

Re:accessing memory allocated with GETMEM


In article <33308499.292411...@nntp.ix.netcom.com>,
skhow...@geocities.com? says...

Quote

>On 19 Mar 97 22:02:36 GMT, jas...@athena.sunitafe.edu.au (squigger)
>wrote:

>>In article <332FD303....@berlin.snafu.de>, joch...@berlin.snafu.de
>>says...

>>>squigger wrote:

>>>> How do I access the memory allocated by this statement :

>>>> getmem(apointer, 1024);

>>>> Please don't tell me it works like this :

>>>> apointer^[0] := 56;

>>>> because this doesn't work - I'm using TP6 if it helps.

>>>You should declare or typecast apointer to whatever it
>>>is you're storing in your memory.  The most general way
>>>would be:

>>>type
>>>   memarray = array[0..$FFF0] of byte;
>>>   memptr = ^memarray;
>>>var
>>>   apointer : memptr;

>>>...

>>> getmem(apointer, 1024);
>>> apointer^[57] := 42;

>>>Not that in this general case apointer^[1025] will not
>>>give you a range check error.

>>>Hope this helps,
>>>Jochen

>>Thanks.  Now, how do I use this for a random amount (as in a sprite
>>programme)?
>>Or can't it be done (wouldn't surprise me...)

>>squigger hops again

>Try the MOVE procedure ...

>move(var source, dest; count : word)

>move copies a block of  'count' bytes starting at the first byte of
>'source' into the space starting at the first byte of  'dest'  .  

>'source' could be the entity you generate w/ getmem,  an element
>within an array, a position within a string, etc.  likewise with
>'dest'.

>BTW, using the sizeof  function is the safe way to define count

>Nick

Yes, but how do I do this without the type structure - I won't know what
size to make the arrays.

Thanks otherwise - I can use that with preset tiles.

squigger hops again

Re:accessing memory allocated with GETMEM


In article <3330627...@ren2.netconnect.com.au> of Wed, 19 Mar 1997
22:02:36 in comp.lang.pascal.borland, squigger <jas...@athena.sunitafe.e

Quote
du.au> wrote:

>Thanks.  Now, how do I use this for a random amount (as in a sprite
>programme)?
>Or can't it be done (wouldn't surprise me...)

The following compiles and runs, apparently correctly.  It makes V point
to a random-sized byte array and reads a random byte which may or may
not be within the allocated array.  ISTM that it it is range-check safe
:-( .
        program xx ;
        type AB = array [0..$FFF0] of byte ;
          PAB = ^AB ;
        var V : PAB ; B : byte ;
        begin Write('A') ; Randomize ;
          GetMem(V, random($FFF1)) ;
          B := V^[random($FFF1)] ;
          Writeln('B') ; Readln end.

Is this random enough as a demonstration?
--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Standard signature separator is as above, a line comprising "-- " (SoRFC1036)
  Standard quoter : ">" / "> " recognised by most good news readers (SoRFC1036)

Re:accessing memory allocated with GETMEM


In article <9911ULA7nRMzE...@merlyn.demon.co.uk>, j...@merlyn.demon.co.uk
says...

Quote

>In article <3330627...@ren2.netconnect.com.au> of Wed, 19 Mar 1997
>22:02:36 in comp.lang.pascal.borland, squigger

<jas...@athena.sunitafe.e
Quote
>du.au> wrote:

>>Thanks.  Now, how do I use this for a random amount (as in a sprite
>>programme)?
>>Or can't it be done (wouldn't surprise me...)

>The following compiles and runs, apparently correctly.  It makes V
point
>to a random-sized byte array and reads a random byte which may or may
>not be within the allocated array.  ISTM that it it is range-check safe
>:-( .
>        program xx ;
>        type AB = array [0..$FFF0] of byte ;
>          PAB = ^AB ;
>        var V : PAB ; B : byte ;
>        begin Write('A') ; Randomize ;
>          GetMem(V, random($FFF1)) ;
>          B := V^[random($FFF1)] ;
>          Writeln('B') ; Readln end.

>Is this random enough as a demonstration?
>--
>John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12
   MIME.
>  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics
and links.
>  Standard signature separator is as above, a line comprising "-- "
(SoRFC1036)
>  Standard quoter : ">" / "> " recognised by most good news readers

(SoRFC1036)

Thanks, I can see how that works (for once!  Yippee! I understand it!).

But that wastes memory, doesn't it?  I'm not sure...

BTW, how does the graph unit handle memory allocated with getmem (as in
its sprite routines)?

squigger hops again

Re:accessing memory allocated with GETMEM


In article <3331dec8.381047...@nntp.ix.netcom.com>,
skhow...@geocities.comW says...

Quote

>On 20 Mar 97 21:59:14 GMT, jas...@athena.sunitafe.edu.au (squigger)
>wrote:

>>But that wastes memory, doesn't it?  I'm not sure...

>The creation of an array  type does NOT allocate memory for each
>element in the array ....  it just defines a structure that COULD
>access those elements.  Even if you define a [1..$FFF0] array, you
>can't use the $F000'th element unless you Getmem'd at least a $F000
>sized space...

>As to use, use it like any other pointer referenced string of bytes!

>Nick

GREAT!  Thanks, that was very worrying to me.

squigger hops again

Re:accessing memory allocated with GETMEM


On 20 Mar 97 21:59:14 GMT, jas...@athena.sunitafe.edu.au (squigger)
wrote:

Quote

>But that wastes memory, doesn't it?  I'm not sure...

The creation of an array  type does NOT allocate memory for each
element in the array ....  it just defines a structure that COULD
access those elements.  Even if you define a [1..$FFF0] array, you
can't use the $F000'th element unless you Getmem'd at least a $F000
sized space...

As to use, use it like any other pointer referenced string of bytes!

Nick

Re:accessing memory allocated with GETMEM


In article <3331b33...@ren2.netconnect.com.au> of Thu, 20 Mar 1997
21:59:14 in comp.lang.pascal.borland, squigger <jas...@athena.sunitafe.e

Quote
du.au> wrote:  AND

In message <199703202259.IAA05...@pegasus.sunitafe.edu.au>, squigger
<jas...@athena.sunitafe.edu.au> wrote

Quote

>Thanks, I can see how that works (for once!  Yippee! I understand it!).
>But that wastes memory, doesn't it?  I'm not sure...

Certainly; it's only a code demonstration, and there is no way of
undoing the GetMem.

Quote
>BTW, how does the graph unit handle memory allocated with getmem (as in
>its sprite routines)?

Don't know; I have not used Graph for years.

Please do not respond unnecessarily to News in both mail & news, as it
wastes time; and if you must, please make this clear at the beginning of
both.
--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Standard quoter : ">" / "> " recognised by most good news readers (SoRFC1036)

Other Threads