-4
function TFormMain.GetMyTBytes(const AFileName: string): TBytes;
begin
  with TFileStream.Create(AFileName, fmOpenRead or fmShareDenyNone) do
  try
    SetLength(Result, Size);
    ReadBuffer(PByte(Result)^, Size);
  finally
    Free;
  end;
end;  

// ... 

var
  TBA, TBB: TBytes;

// ...

TBA := GetMyTBytes('C:\mydoc.docx');
TBB := Copy(TBA);

Can it be excluded in this case that TBB shares any memory with TBA at the end of this operation?

user1580348
  • 5,721
  • 4
  • 43
  • 105
  • 2
    You could test this yourself with two lines of code: `TBB[0] := TBA[0] + 1; ShowMessage(BoolToStr(TBB[0] = TBA[0], True));` in less time than it took you to open your browser, come here, and click the *Ask Question* button. – Ken White Apr 16 '17 at 02:14
  • 1
    @user1580348: You could have checked the docs too, e.g. [here](http://docwiki.embarcadero.com/Libraries/Seattle/en/System.Copy): "The substring or subarray is a unique copy". If you checked in code, like Ken said, or if you had read the docs, you should have mentioned that, e.g. "The documentation says ..., but can I rely on this to be true in all circumstances?" or "it says *subarray*, is this true for full arrays too?" or something like that, etc. – Rudy Velthuis Apr 16 '17 at 10:15
  • There are many *special cases* where code may not follow exactly what is said in the documentation. A large part of the discussion on SO (my question included) is about those possible special cases. – user1580348 Apr 16 '17 at 10:22
  • @user1580348: Then you should have asked *that*, i.e. "The docs say X, but can I rely on that in all cases?" or "I tried Y and they were indeed different, but is this generally true?", or something like it. The way you stated it did not indicate you tried or looked up anything. – Rudy Velthuis Apr 16 '17 at 10:42

1 Answers1

4

Copy() creates a duplicate of the array data in memory. There is nothing shared between TBA and TBB once the copy is made.

Also, your GetMyTBytes() is redundant, the System.IOUtils unit has a TFile.ReadAllBytes() method available.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770