Recent

Author Topic: Practical differences  (Read 2556 times)

ezlage

  • Guest
Practical differences
« on: October 26, 2016, 11:57:33 pm »
Hi!

Which are the practical differences between the declaration ways below?

Code: Pascal  [Select][+][-]
  1. DoSomething(var AStream: TStream);
  2. DoSomething(const AStream: TStream);
  3. DoSomething(AStream: TStream);

I know the theoretical difference between "var" and "const" only.

I would like to assign to the variable AStream all variants of the type (TMemoryStream, TFileStream, TBytesStream, etc.) without allocating unnecessary memory (without duplicating the data inside the stream).

Thank you.




lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Practical differences
« Reply #1 on: October 27, 2016, 12:31:07 am »
Var: it passes an address not a copy of the value. In your case is the same since objects are always passed as reference.

Const: the value is a constant and should not be assigned or freed inside the procedure. Is the recommended for your case. You allocate the memory outside the procedure.

Normal: for normal types it passes a copy of the value except for objects as already mentioned.

lainz

  • Hero Member
  • *****
  • Posts: 4473
    • https://lainz.github.io/
Re: Practical differences
« Reply #2 on: October 27, 2016, 12:48:39 am »
http://www.marcocantu.com/epascal/English/ch06proc.htm

That website explains it better. It corrects something I said: objects are passed by value because are actually references.

totya

  • Hero Member
  • *****
  • Posts: 720
Re: Practical differences
« Reply #3 on: October 27, 2016, 01:11:32 am »
I would like to assign to the variable AStream all variants of the type (TMemoryStream, TFileStream, TBytesStream, etc.) without allocating unnecessary memory (without duplicating the data inside the stream).

Hi, no "duplication data" in this case, because TStream is a simple pointer.

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  
  12.   { TForm1 }
  13.  
  14.   TForm1 = class(TForm)
  15.     Memo1: TMemo;
  16.     procedure FormCreate(Sender: TObject);
  17.   private
  18.     procedure DoSomethingV (var AStream: TStream);
  19.     procedure DoSomethingC (const AStream: TStream);
  20.     procedure DoSomethingN (AStream: TStream);
  21.   end;
  22.  
  23. var
  24.   Form1: TForm1;
  25.  
  26. implementation
  27.  
  28. {$R *.lfm}
  29.  
  30. { TForm1 }
  31.  
  32. procedure TForm1.DoSomethingV (var AStream: TStream);
  33. begin
  34.   Memo1.Lines.Add(IntToHex(PtrUInt(AStream), SizeOf(PtrUInt)*2));
  35. end;
  36. procedure TForm1.DoSomethingC (const AStream: TStream);
  37. begin
  38.   Memo1.Lines.Add(IntToHex(PtrUInt(AStream), SizeOf(PtrUInt)*2));
  39. end;
  40. procedure TForm1.DoSomethingN (AStream: TStream);
  41. begin
  42.   Memo1.Lines.Add(IntToHex(PtrUInt(AStream), SizeOf(PtrUInt)*2));
  43. end;
  44.  
  45. procedure TForm1.FormCreate(Sender: TObject);
  46. var
  47.   MS: TMemoryStream;
  48. begin
  49.   MS:= nil;
  50.   MS:= TMemoryStream.Create;
  51.   if Assigned(MS) then
  52.     try
  53.       DoSomethingV(TStream(MS));
  54.       DoSomethingC(TStream(MS));
  55.       DoSomethingN(TStream(MS));
  56.     finally
  57.       MS.Free;
  58.     end;
  59. end;
  60.  
  61. end.
  62.  

 

TinyPortal © 2005-2018