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.
You can find it at
http://www.borland.com/devsupport/delphi/downloads/index.html
Steve Griffiths.
> 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
> 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
What if I want a memory larger than 64k bytes ?
Sun Jiangang
jg...@email.bnu.edu.cn
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--
>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