I want to compare two Byte arrays.
byInputBuffer is an array which I filled by external device. 256 bytes comes in every main loop iteration and is always put in the beginning of byInputBuffer. It is declared like here:
byInputBuffer: array [0..1023] of byte;
DataArray is an array with dynamically declared size. So array is declared like here:
DataArray : Array of Byte;
and then in code size is declared like here:
SetLength(DataArray, DataLengthInt);
where DataLengthInt is 130952.
Main loop is iterated from 0 to 511, but when i=0 I get BoolToStr(verify)=-1 in code presented below:
leftBytes := 256;
verify := CompareMem(@byInputBuffer, @DataArray[(i*256)], leftBytes);
But I checked and contents of both arrays when i=0 is equal for the first 256 elements.
My question is why CompareMem returns -1?
Other used declarations:
leftBytes : Integer;
verify : Boolean;
You are calling
CompareMemto compare the first 256 bytes ofbyInputBufferagainst the first 256 bytes ofDataArray.You are then taking the return value of
CompareMemand passing it throughBoolToStr. That mapsFalseto'0'andTrueto'-1'.The conclusion therefore is that the first 256 bytes of the two arrays are equal. Because
CompareMemreturnedTrue.Note that your statement in the question that
CompareMemreturned-1is obviously incorrect. That is a big part of our confusion. Precision and accuracy are crucial when programming.