Record, packed record, and Variant record

Source: Internet
Author: User
// The integer type is exactly four bytes, And the integer int type is one byte, but the memory in Windows is 4 bytes allocated,
// Therefore, there are actually four bytes. We can see that the size of this record is 8 bytes with sizeof.
// Free space, but faster (Boundary alignment principle in Windows Memory Allocation)
Tperson =   Record
Age: integer;
Sex: writable int;
End ;

Tpackedperson= Packed Record
Age: integer;
Sex: writable int;//Use packedRecordThe size of the record is 5 bytes;
End;

Temployee=   Record
ID: integer; // Integer is 4 bytes
Case Integer Of
0 :( Yearmoney: integer ); // Yearmoney and monthmoney share memory, allocated by maximum memory
1 :( Monthmoney: Random INT ); // The size of the record is 8 bytes.
End ;

Ttagemployee =   Record
ID: integer;
Case Grade: integer Of // The grade variable is added here.
0 :( Yearmoney: integer ); // Yearmoney and monthmoney share memory, allocated by maximum memory
1 :( Monthmoney: Random INT ); // The size of the record is 12 bytes (ID + Grade + Yearmoney)
End ;

 

 

1. in Delphi, we use the record keyword to indicate a record. Sometimes we can see records declared using packed record. The difference between the two lies in the different storage methods; in Windows, memory is allocated in 4 bytes at a time, while packed applies for and allocates memory in bytes, Which is slower, it takes extra time to locate the pointer. Therefore, if you do not need packed, Delphi will apply for memory in 4 bytes at a time. Therefore, if a variable does not have 4 bytes in width, it will take 4 bytes! This wastes some space, but increases the efficiency.

2. Variant record rules:

 

(1) The size of long string, widestring, dynamic array, and interface is the pointer size. olevariant is actually the Variant Structure in the com SDK, And the size is 16 bytes. But in Object Pascal, they all need to be automatically terminated. If they appear in the variant part, the compiler cannot know whether they should be finalized-because they do not know the type currently stored, they cannot appear in the variant record type, or it can be defined using a string similar to string [10;

(2) All variant fields share a piece of memory, and the size of the shared memory is determined by the maximum variant field-the "longest" variable;

(3) When a tag exists, it is also a record field. Or no tag.

(4) The condition field of the variant part of the record must be an ordered type. The type followed by the case must be boolean, integer, and so on.

(5) The record type may contain the variant part, which is similar to the case statement but does not have the final end. The variant part must be declared after other fields in the record.

3. the most classic variant record. The tmessage structure in Delphi:

 

Tmessage =   Packed   Record
MSG: Cardinal;
Case Integer Of
0 :(
Wparam: longint;
Lparam: longint;
Result: longint );
1 :(
Wparamlo: word;
Wparamhi: word;
Lparamlo: word;
Lparamhi: word;
Resultlo: word;
Resulthi: Word );
End ;

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.