Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: question about records

  1. #1

    question about records

    hi all

    i still don't understand this :

    TColor =packed record
    case integer of
    0 : (R, G, B, A : Byte);
    1 : (C : Cardinal);
    end;

    i mean 0 and 1 , whats the meaning of theme

  2. #2
    You can use the case statement to map different types of variables in the same memory space.The types used must be equal size, ie. Cardinal = 4 bytes in this case.

    Code:
    MyColor.R := $FF;
    MyColor.G := $00;
    MyColor.B := $FF;
    MyColor.A := $00;
    // Is equal to
    MyColor.C :=  $00FF00FF;
    Another example
    Code:
    TVector = record
       case Integer of
          0: (x, y, z: Single);
          1: (v: array [0..2] of Single);
    end;
    
    MyVector.x := 5.75;
    MyVector.y := 1.75;
    MyVector.z := 0.00;
    // Is equal to
    MyVector.v[0] := 5.75;
    MyVector.v[1] := 1.75;
    MyVector.v[2] := 0.00;
    Last edited by vgo; 02-10-2010 at 10:29 AM.

  3. #3
    The 1 and 0 don't really mean anything. They just show that there are two different ways to access that piece of memory. During program execution, you can access either R, G, B and A or C to change the whole color at once.
    Coders rule nr 1: Face ur bugz.. dont cage them with code, kill'em with ur cursor.

  4. #4
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    Yeah I found it funky that you can even stick functions and procedures into records as well.

    Actually what I've never been able to figure out what 'packed' really does, or maybe I simply forgot. What is the purpose of this keyword and what does it do?
    Jason McMillen
    Pascal Game Development
    Co-Founder





  5. #5
    This is called a variant record. It's similar to C++'s union, but it's not the same, so you can't confuse them and put them two in the same basket. Compare Wikipedia article about unions and variant records.

  6. #6

  7. #7
    Co-Founder / PGD Elder WILL's Avatar
    Join Date
    Apr 2003
    Location
    Canada
    Posts
    6,107
    Blog Entries
    25
    Quote Originally Posted by Brainer View Post
    I see now. Thanks!
    Jason McMillen
    Pascal Game Development
    Co-Founder





  8. #8
    embedded case in a record is a very useful thing

  9. #9

  10. #10
    Quote Originally Posted by WILL View Post
    I see now. Thanks!
    About the "packed" clause in record defination, the most important think is to know is that if you do not use "packed" then delphi/pascal is free to add "invisibles" fields to the record to adjust the total size of the structure for best perfomance; so take note that using for example "sizeof(myrecord)," could give you a record size different at what you think if you sum indididually every field size;

    Normaly there is not any problem using or not a packed record if you are going to use its members in traditional way. but if you are going to update/read data from records using move(), or blockread()/blockwrite() or similar function & procedure where you pass the record for read or write his members from a block memory then is adviced to use packed records so you as programmer are completly sure you record is aligned as you defined.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •