Delphi Array of Pointers -
i have problems when try write array of pointers file:
type pint = ^int64; tarray = array[ 0..5 ] of pint; procedure save( afilehandle: thandle; aarray: tarray ); begin filewrite( afilehandle, aarray[ 0 ]^, length( aarray ) * sizeof( pint ) ); end; procedure load( afilehandle: thandle; aarray: tarray ); var i: int32; begin := 0 length( aarray ) - 1 aarray[ ] := allocmem( sizeof( pint ) ); fileread( afilehandle, aarray[ 0 ]^, length( aarray ) * sizeof( pint ) ); end;
can avoid writing every single item writing @ once?
thanks.
your question confusing. can't imagine trying write pointers (they invalid when read them back), int64
s point to. if case, not doing right.
if array contains pointers, can't write items in 1 go. the pointers in 1 contiguous block, not items point to. have write them 1 one:
procedure save(afilehandle: thandle; aarray: tarray); var i: integer; begin := low(aarray) high(aarray) filewrite(afilehandle, aarray[i]^, sizeof(int64)); end;
or, alternatively:
procedure save(afilehandle: thandle; aarray: tarray); var p: pint; begin p in aaray filewrite(afilehandle, p^, sizeof(p^)); end;
and reading back:
procedure load(afilehandle: thandle; var aarray: tarray); var i: integer; begin := low(aarray) high(aarray) begin new(aarray[i]); fileread(afilehandle, aarray[i]^, sizeof(int64)); end; end;
you read , write sizeof(pint)
, write size of pointer, not size of int64
.
as sertac writes, write them in 1 go (and read them too), avoiding having call filewrite
or fileread
repeatedly, if copy int64
s contiguous block first (assuming each call file i/o slower copying values single array):
type psavearray = ^tsavearray; tsavearray = array[0..5] of int64; procedure save(afilehandle: thandle; aarray: tarray); var save: tsavearray; i: integer; begin := 0 5 save[i] := aarray[i]^; filewrite(afilehandle, save, sizeof(save)); end;
reading similar:
procedure load(afilehandle: thandle; var aarray: tarray); var items: psavearray; i: integer; begin new(items); fileread(afilehandle, items^, sizeof(items^)); := low(items) high(items) aarray[i] := addr(items^[i]); end;
Comments
Post a Comment