Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Static and Dynamic Array of Byte 1

Status
Not open for further replies.

topcat01

Programmer
Jul 10, 2003
83
GB
Hi,

I am experimenting with static and dynamic arrays, reading bytes from a file.

Code:
var
  Buffer1: array[0..341] of Byte;
begin

  ...
  
  ReadCount := SourceFile.Read(Buffer1, SizeOf(Buffer1));
  for k := 0 to ReadCount-1 do
    TempResult := ((TempResult shr 8) and $FFFFFF) xor ValueTable[(TempResult xor Longword(Buffer1[k])) and $FF];
  ...
end;

The above code reads a number of bytes stored in Buffer1, a CRC check is then performed on those bytes (crc code etc has been omitted). This code works but has the limitation of only holding 342 bytes so I want to use a dynamic array so that the size of data processed can be changed during runtime, here is the new code which does not work - I receive an Access violation every time...

Code:
var
  Buffer2: array of Byte;
  Buffersize : integer;
begin

  ...
  Buffersize := 342;
  
  SetLength(Buffer2, Buffersize);
  ReadCount := SourceFile.Read(Buffer2, SizeOf(Buffer2));
  for k := 0 to ReadCount-1 do
    TempResult := ((TempResult shr 8) and $FFFFFF) xor ValueTable[(TempResult xor Longword(Buffer2[k])) and $FF];
  ...
end;

I've allocated the space prior to reading/processing so I don't see what the problem can be?

Thanks for any help
 
try this :

ReadCount := SourceFile.Read(@Buffer2[0], Length(Buffer2));


-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Thanks I tried your suggestion but received the error:

'Constant object cannot be passedas var parameter'

I removed the '@' and tried the following:

Code:
ReadCount := SourceFile.Read(Buffer1[0], Length(Buffer1));

Removing the '@' char fixed the problem.

What is the '@' character as I have never used this before?

Thanks
 
the @ means address of the variable...
it has the same effect as the addr() function.

Cheers,
Daddy

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top