Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Programmieren allgemein (https://www.delphipraxis.net/40-programmieren-allgemein/)
-   -   Delphi 12 und SendMessage (https://www.delphipraxis.net/214670-delphi-12-und-sendmessage.html)

EscapedFromMatrix 19. Feb 2024 13:18

AW: Delphi 12 und SendMessage
 
[QUOTE=Kas Ob.;1533652]
Zitat:

Zitat von EscapedFromMatrix (Beitrag 1533647)
First i put SizeOf(TCopyDataStruct) not sizeof(aCopyData)...

Sorry, my mistake:

showmessage(inttostr(sizeof(TCopyDataStruct)));
D11, x86= 12
D11, x64= 24

D12, x86= 12
D12, x64= 24

himitsu 19. Feb 2024 16:01

AW: Delphi 12 und SendMessage
 
SizeOf(Variable) ist nicht falsch.
Der Typ und die Variable müssten sowieso gleich groß sein, da die Variable ja den "selben" Typ hätte.

Das sieht mehr wie ein Pointer aus (4 Byte = 32 Bit und 8 Byte = 64 Bit)
z.B. PCopyDataStruct statt TCopyDataStruct
aber auch da wären
Delphi-Quellcode:
SizeOf(PCopyDataStruct)
und
Delphi-Quellcode:
SizeOf(Variable) // var Variable: PCopyDataStruct;
gleich.

Kas Ob. 20. Feb 2024 08:19

AW: Delphi 12 und SendMessage
 
Zitat:

Zitat von himitsu (Beitrag 1533659)
SizeOf(Variable) ist nicht falsch.
Der Typ und die Variable müssten sowieso gleich groß sein, da die Variable ja den "selben" Typ hätte.

Das sieht mehr wie ein Pointer aus (4 Byte = 32 Bit und 8 Byte = 64 Bit)
z.B. PCopyDataStruct statt TCopyDataStruct
aber auch da wären
Delphi-Quellcode:
SizeOf(PCopyDataStruct)
und
Delphi-Quellcode:
SizeOf(Variable) // var Variable: PCopyDataStruct;
gleich.

You are right, but for best practice i prefer never to use variable, to avoid situations like the following
Code:
type
  PMyRecord = ^TMyRecord;

  TMyRecord = record
    Buffer: array[0..31] of Byte;
  end;

  TMyClass = class
  public
    Rec1: TMyRecord;
    Rec2: PMyRecord;
  end;
var
  C:TMyClass;
begin
  Writeln(IntToStr(SizeOf(C.Rec1)));  // 32 
  Writeln(IntToStr(SizeOf(C.Rec2)));  // 4 on 32bit, 8 on 64bit
  Readln;
end.
This thing will be dangerous or more impactful when TMyRecord is small like 4 or 8 and pass on one platform to fail on the other, can happen when updating legacy code or just redesigning data structures.

dummzeuch 20. Feb 2024 08:37

AW: Delphi 12 und SendMessage
 
Zitat:

Zitat von Kas Ob. (Beitrag 1533664)
You are right, but for best practice i prefer never to use variable, to avoid situations like the following
Code:
type
  PMyRecord = ^TMyRecord;

  TMyRecord = record
    Buffer: array[0..31] of Byte;
  end;

  TMyClass = class
  public
    Rec1: TMyRecord;
    Rec2: PMyRecord;
  end;
var
  C:TMyClass;
begin
  Writeln(IntToStr(SizeOf(C.Rec1)));  // 32 
  Writeln(IntToStr(SizeOf(C.Rec2)));  // 4 on 32bit, 8 on 64bit
  Readln;
end.
This thing will be dangerous or more impactful when TMyRecord is small like 4 or 8 and pass on one platform to fail on the other, can happen when updating legacy code or just redesigning data structures.

Unfortunately that opens the possibility for a different kind of error:
You check the size of the type you are using at some time and later on change the variable declaration to a different type. SizeOf(VariableName) will then automatically reflect that change while SizOf(TOldType) will still return the size of the original type. If you don't catch that, your code will be wrong. That's the reason I prefer using the variable instead of the type.

Kas Ob. 20. Feb 2024 09:30

AW: Delphi 12 und SendMessage
 
Zitat:

Zitat von dummzeuch (Beitrag 1533665)
Zitat:

Zitat von Kas Ob. (Beitrag 1533664)
You are right, but for best practice i prefer never to use variable, to avoid situations like the following
Code:
type
  PMyRecord = ^TMyRecord;

  TMyRecord = record
    Buffer: array[0..31] of Byte;
  end;

  TMyClass = class
  public
    Rec1: TMyRecord;
    Rec2: PMyRecord;
  end;
var
  C:TMyClass;
begin
  Writeln(IntToStr(SizeOf(C.Rec1)));  // 32 
  Writeln(IntToStr(SizeOf(C.Rec2)));  // 4 on 32bit, 8 on 64bit
  Readln;
end.
This thing will be dangerous or more impactful when TMyRecord is small like 4 or 8 and pass on one platform to fail on the other, can happen when updating legacy code or just redesigning data structures.

Unfortunately that opens the possibility for a different kind of error:
You check the size of the type you are using at some time and later on change the variable declaration to a different type. SizeOf(VariableName) will then automatically reflect that change while SizOf(TOldType) will still return the size of the original type. If you don't catch that, your code will be wrong. That's the reason I prefer using the variable instead of the type.

Well, you are right if the target is the variable itself not what it does represent, like in case data serialization against passing data between threads in the same process using thread safe list.
But for more accurate example :
We all familiar with Windows API's that takes structures (records), and many of them do need special initialization with cbSize or the very popular dwOSVersionInfoSize in https://learn.microsoft.com/en-gb/wi...osversioninfoa
Now if that record initially declared as local var with T, then it will not matter for using SizeOf the var itself, but in future change the code need to switch to dynamic allocated record to save it for future use, here the var will be a pointer and that function will fail.
Also and i witnessed this a lot, structure stack allocated then later the code needed to be multithreaded or just will save for future use, the var will be pointer, and will fail too.

I was talking about the best practice, but if the var itself is point (the needed) then using the type is useless here.


Alle Zeitangaben in WEZ +1. Es ist jetzt 18:10 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz