Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Accesing Allocated Memory like Arrays

104 views
Skip to first unread message

Christian Reinhard

unread,
Nov 7, 1997, 3:00:00 AM11/7/97
to

Hi.

In C i can allocate memory for a pointer like
this

pointer = (byte*) malloc(100)

and then access the bytes with pointer[index] without incrementing or
decrementing the pointer.
Is this possible with object pascal, too?

Thanks.

Steve Griffiths

unread,
Nov 7, 1997, 3:00:00 AM11/7/97
to

Check out Lloyds Help File. Has a bunch of info on this under Arrays.
File name is ldelphi.zip.

You can find it at
http://www.borland.com/devsupport/delphi/downloads/index.html

Steve Griffiths.

Michel Brazeau

unread,
Nov 7, 1997, 3:00:00 AM11/7/97
to Christian Reinhard

Christian Reinhard wrote:
>

> In C i can allocate memory for a pointer like
> this
>
> pointer = (byte*) malloc(100)
>
> and then access the bytes with pointer[index] without incrementing or
> decrementing the pointer.
> Is this possible with object pascal, too?

In pascal you must first declare a type, ie,

TByteArray = array[0..64000] of Byte;
TByteArrayPtr = ^TByteArray;

{ in 32 bit Delphi, there is no limit on the upper range }

Now use GetMem and FreeMem,

var
pByteArray : TByteArrayPtr;

begin
GetMem(pByteArray, 100);
pByteArray^[Index] := ??
...
FreeMem(pByteArray, 100);

Like in C, you are responsible to prevent memory overruns.

Hope this helps,

Michel
------
Container and Persistent classes for Delphi
http://www.cam.org/~mibra/spider

Tab Transcriber
http://www.cam.org/~mibra

Michael Day (TeamB)

unread,
Nov 7, 1997, 3:00:00 AM11/7/97
to

On Fri, 07 Nov 1997 12:06:54 +0100, Christian Reinhard
<cnre...@cip.informatik.uni-erlangen.de> wrote:

> access the bytes with pointer[index] without incrementing or
>decrementing the pointer. Is this possible with object pascal, too?

Not only is it possible, but you are supposed to use indexed accessing in
Pascal. Pointer manipulation is not considered proper coding in Pascal.

type RecType = integer; {<-- array item type goes here}
const MaxRecItem = 65520 div sizeof(RecType);
type = MyArrayType = array[0..MaxRecItem] of RecType;
type = MyArrayTypePtr = ^MyArrayType;
var MyArray : MyArrayTypePtr;
begin
ItemCnt := 10; {number of array items to allocate}
GetMem(MyArray,ItemCnt*sizeof(MyArray[1])); {allocate the array}
MyArray^[3] := 10; {access the array}

FreeMem(MyArray,ItemCnt*sizeof(MyArray[1])); {deallocate array when done}
end;

- Mike


Sun Jiangang

unread,
Nov 8, 1997, 3:00:00 AM11/8/97
to

Michel Brazeau wrote:
>
> Christian Reinhard wrote:
> >
>
> > In C i can allocate memory for a pointer like
> > this
> >
> > pointer = (byte*) malloc(100)
> >
> > and then access the bytes with pointer[index] without incrementing or

> > decrementing the pointer.
> > Is this possible with object pascal, too?
>
> In pascal you must first declare a type, ie,
>
> TByteArray = array[0..64000] of Byte;
> TByteArrayPtr = ^TByteArray;
>
> { in 32 bit Delphi, there is no limit on the upper range }
>
> Now use GetMem and FreeMem,
>
> var
> pByteArray : TByteArrayPtr;
>
> begin
> GetMem(pByteArray, 100);
> pByteArray^[Index] := ??
> ...
> FreeMem(pByteArray, 100);
>
> Like in C, you are responsible to prevent memory overruns.
>
> Hope this helps,
>
> Michel
> ------
> Container and Persistent classes for Delphi
> http://www.cam.org/~mibra/spider
>
> Tab Transcriber
> http://www.cam.org/~mibra

What if I want a memory larger than 64k bytes ?

Sun Jiangang
jg...@email.bnu.edu.cn

Jose Sebastian Battig

unread,
Nov 10, 1997, 3:00:00 AM11/10/97
to

This is a multi-part message in MIME format.
--------------C6A2FBFA603EDC8A87D1DA9A
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

In D2 there's no problem because it's 32 bits, in D1 the things chages
because D1 cannot handle structures larger than 64K, in that case you
have to split the struct into parts and handle every part separately.

Cheers

--
Jose Sebastian Battig

---------------------------------------------------

Important:

Please, when replying to me delete the "STOPSPAM-"
part of the "reply to" e-mail address.

Por favor cuando me contestes usando "reply" borra
la parte de la direccion que dice "STOPSPAM-".
--------------C6A2FBFA603EDC8A87D1DA9A
Content-Type: text/x-vcard; charset=us-ascii; name="vcard.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Jose Sebastian Battig
Content-Disposition: attachment; filename="vcard.vcf"

begin: vcard
fn: Jose Sebastian Battig
n: Battig;Jose Sebastian
org: Softech
adr: ;;;San Miguel de Tucumán;Tucumán;4000;Argentina
email;internet: STOPSP...@iname.com
note: It's important to make you note that you should delete the "STOPSPAM-" part of my E-Mail address to send me a message.
x-mozilla-cpt: ;0
x-mozilla-html: FALSE
version: 2.1
end: vcard


--------------C6A2FBFA603EDC8A87D1DA9A--


Michael Day (TeamB)

unread,
Nov 10, 1997, 3:00:00 AM11/10/97
to

On Sat, 08 Nov 1997 17:07:15 +0800, Sun Jiangang <jg...@email.bnu.edu.cn>
wrote:


>What if I want a memory larger than 64k bytes ?

In 32 bit, you just make the array size declaration the largest that you would
ever use. In 16bit you have to break it up into 64KB max size pieces.

- Mike


0 new messages