Forum > FPC development
Possible Bug (Mac M1/AArch)?
Grahame Grieve:
I don't see how this from TBytesStream is different to my first code:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function TBytesStream.Realloc(var NewCapacity: PtrInt): Pointer;begin // adapt TMemoryStream code to use with dynamic array if NewCapacity<0 Then NewCapacity:=0 else begin if (NewCapacity>Capacity) and (NewCapacity < (5*Capacity) div 4) then NewCapacity := (5*Capacity) div 4; NewCapacity := (NewCapacity + (TMSGrow-1)) and not (TMSGROW-1); end; if NewCapacity=Capacity then Result:=Pointer(FBytes) else begin SetLength(FBytes,Newcapacity); Result:=Pointer(FBytes); if (Result=nil) and (Newcapacity>0) then raise EStreamError.Create(SMemoryStreamError); end;end;
ASerge:
--- Quote from: Grahame Grieve on November 04, 2022, 07:48:34 am ---I don't see how this from TBytesStream is different to my first code:
--- End quote ---
This code does not use the Read method, in which an error occurs when parameters are passed incorrectly.
Grahame Grieve:
no but it uses the pointer to the TBytes directly, rather than a pointer to TBytes[0]. I don't see how that's different?
Jonas Maebe:
A dynamic array is internally a pointer. TBytesStream.Realloc changes that pointer to point to a different block of memory of larger/smaller size. That is the purpose of a pointer, so this code is correct.
Your code was reading array data and storing it in the pointer itself, spilling out in memory coming after that pointer. That is wrong, since the read data should be stored in the memory block to which the pointer points (and the pointer just contains the address of that block). That memory can be addressed using tbytesvar[0].
PascalDragon:
You can use the following class helper with the FPC 3.2.x series to achieve the same that's possible in FPC 3.3.1 now:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// interface section{$ifdef ver3_2}type TStreamHelper = class helper for TStream public function Read(var Buffer: TBytes; Count: Longint): Longint; overload; function Read(Buffer: TBytes; aOffset, Count: Longint): Longint; overload; function Write(const Buffer: TBytes; Offset, Count: Longint): Longint; overload; function Write(const Buffer: TBytes; Count: Longint): Longint; overload; end;{$endif} // implementation section{$ifdef ver3_2}function TStreamHelper.Read(var Buffer: TBytes; Count: Longint): Longint;begin Result := Read(Buffer, 0, Count);end; function TStreamHelper.Read(Buffer: TBytes; aOffset, Count: Longint): Longint;begin Result := Read(Buffer[aOffset], Count);end; function TStreamHelper.Write(const Buffer: TBytes; Offset, Count: Longint): Longint;begin Result := Write(Buffer[Offset], Count);end; function TStreamHelper.Write(const Buffer: TBytes; Count: Longint): Longint;begin Result := Write(Buffer, 0, Count);end;{$endif}
Just make sure that the unit is in scope when you want to use the added methods.
Navigation
[0] Message Index
[#] Next page
[*] Previous page