Board index » delphi » Dynamic memory usage

Dynamic memory usage

  Is there any way to do this:

  (in C:)
  unsigned char *m = malloc( xx );    /* xx is a certain # of bytes */
  m[10] = 5;                  

  in pascal?

  I'll start it:

  type byteptr = ^byte;
  var  m : byteptr;
  begin
    getmem( m, xx );
    ...

  can someone fill in the rest?

  Thanks.

------------------------------------------------------------------------
Patrick O'Malley       http://grove.ufl.edu/~pomalley       Silicon Slim
          pomal...@grove.ufl.edu               pomal...@mil.ufl.edu
------------------------------------------------------------------------
Misanthrope: One who does not suffer fools but loves to see fools suffer
  -- Unknown(?)

 

Re:Dynamic memory usage


Quote
pomal...@grove.ufl.edu wrote:
>   Is there any way to do this:

>   (in C:)
>   unsigned char *m = malloc( xx );    /* xx is a certain # of bytes */
>   m[10] = 5;

>   in pascal?

>   I'll start it:
> [snip!]

Scratch that, start over (though you do use getmem()).  Try:

type
  Tbytearray = array[0..65519 div sizeof(byte)];
  Pbytearray = ^Tbytearray;

var
  m : Pbytearray;

begin
  getmem (m,xx*sizeof(byte));
  {...}

Of course, this leaves an open-ended array, and it's possible to get an
index which is out of the range of allocated memory.  In C, you'd have
to check the array bounds, and you must do that in TP, too.

The sizeof(byte) may seem redundant because it should return 1, but this
shows how you'd apply this if you were using arrays of words, longints,
records, reals, etc.

Quote
>   Thanks.

> ------------------------------------------------------------------------
> Patrick O'Malley       http://grove.ufl.edu/~pomalley       Silicon Slim
>           pomal...@grove.ufl.edu               pomal...@mil.ufl.edu
> ------------------------------------------------------------------------
> Misanthrope: One who does not suffer fools but loves to see fools suffer
>   -- Unknown(?)

--
Scott Earnest        | We now return you to our regularly |
set...@ix.netcom.com | scheduled chaos and mayhem. . . .  |

Re:Dynamic memory usage


-=[ In:pomal...@grove.ufl.edu was heard to say... ]=-

 In> Is there any way to do this:

 In> (in C:)
 In> unsigned char *m = malloc( xx );    /* xx is a certain # of bytes */
 In> m[10] = 5;                  

 In> in pascal?
 In>
 In> I'll start it:
 In>
 In> type byteptr = ^byte;

This will make each byte consume 5 bytes. One for the byte itself and four
for the pointer to it.

 In> var  m : byteptr;
 In> begin
 In> getmem( m, xx );
 In> ...

 In> can someone fill in the rest?

I'm not sure what the C code is doing, but I imagine you want to create
a pointer to an array of chars? Try this:

    type
       bytearray = array[1..MAXARRAY] of char;

    var
       m: ^bytearray;  { declares m as a pointer of type bytearray }

    begin
       getmem(m,sizeof(bytearray));
       { ...etc... }

You can give any amount as the second parameter to GetMem up to the size
of the variable m.

 In> - Patrick O'Malley       http://grove.ufl.edu/~pomalley       Silicon

  /-=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=-\
  $                  Kim Forwood  <kim.forw...@crcn.net>                 %
  %        Author of Disk Ease v0.9.2 Floppy Disk Indexer/Database       $
  $              http://goodship.cn.camriv.bc.ca/~kforwood/              %
  \-=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=--=oOo=-/
            WARNING:  Unsolicited advertising will be reported
                  to your ISP. Flames merely laughed at.

___ Blue Wave/QWK v2.20

Re:Dynamic memory usage


Quote
pomal...@grove.ufl.edu wrote:

>   Is there any way to do this:

>   (in C:)
>   unsigned char *m = malloc( xx );    /* xx is a certain # of bytes */
>   m[10] = 5;

>   in pascal?

>   I'll start it:

>   type byteptr = ^byte;
>   var  m : byteptr;
>   begin
>     getmem( m, xx );
>     ...

>   can someone fill in the rest?

Nope, but start over, yes.

This is a kide rude way, i guess, and you have to make sure you
don't make any mistakes, but it workes (with the range check off,
of coz :).

type
    lame_array: array[0..0] of byte;
    lame_array_ptr: ^lame_array;

var
   m: lame_array_ptr;

begin
     getmem(m, xx); { where xx is the # of bytes to allocate }
     m^[10]:= 5;
...

- Asbj?rn

Other Threads