type
   Ta= record
      a : Extended;
    end;
begin
  ShowMessage(IntToStr(SizeOf(Extended)));
  ShowMessage(IntToStr(SizeOf(Ta)));
end;
Extended类型占10字节,但为何Ta却是16字节?

解决方案 »

  1.   

    编译器按字节对齐处理
      
    ta=packed record
        x:extended;
      end;
    这样就是10字节
      

  2.   

      Ta= record
          a : Extended;
        end; 
    Ta是 record 类型啊, 它应该还要开内存,存储一些东西,如,Ta 中有几个成员,等等 
      

  3.   

    这也说不通啊,如果将Extended改为Integer,那结果是4,正好是Integer占用的内存大小
      

  4.   

    这也说不通啊,如果将Extended改为Integer,那结果是4,正好是Integer占用的内存大小
      

  5.   

    这也说不通啊,如果将Extended改为Integer,那结果是4,正好是Integer占用的内存大小
      

  6.   

    就是字节对齐问题
    record为了优化寻址速度,会把成员按4字节进行对齐(猜的)
      

  7.   

    理解了一半,默认是8字节对齐
    但是:
    Ta=record
      a:Extended;
    end;
    ShowMessage(IntToStr(SizeOf(Ta)));//结果为16,证明成员为一个时也会对齐。而且以8字节为单位。

    Ta=record
      a:Integer;
    end;
    ShowMessage(IntToStr(SizeOf(Ta)));//结果为4,没有以8字节单位对齐,不知如何理解???
      

  8.   


    我试了一下以下情况
    Ta=record 
      a:Byte; 
    end; Ta=record 
      a:Word; 
    end; Ta=record 
      a:Integer; 
    end; 测试下来,感觉当记录只有一个域,且该域类型占用字节小于4个字节时,就不进行对齐了。
    而以下情况,因为域占用字节大于4个字节了,就进行对齐了。
    Ta=record 
      a:Real48; 
    end; Ta=record 
      a:Extended; 
    end; 
      

  9.   

    When a record type is declared in the {$A+} state (the default), and when the declaration does not include a packed modifier, the type is an unpacked record type, and the fields of the record are aligned for efficient access by the CPU. The alignment is controlled by the type of each field and by whether fields are declared together. Every data type has an inherent alignment, which is automatically computed by the compiler. The alignment can be 1, 2, 4, or 8, and represents the byte boundary that a value of the type must be stored on to provide the most efficient access. The table below lists the alignments for all data types.Type alignment masks 
    Type Alignment 
    Ordinal types size of the type (1, 2, 4, or 8)
    Real types 2 for Real48, 4 for Single, 8 for Double and Extended
    Short string types 1
    Array types same as the element type of the array.
    Record types the largest alignment of the fields in the record
    Set types size of the type if 1, 2, or 4, otherwise 1
    All other types determined by the $A directive.
    To ensure proper alignment of the fields in an unpacked record type, the compiler inserts an unused byte before fields with an alignment of 2, and up to three unused bytes before fields with an alignment of 4, if required. Finally, the compiler rounds the total size of the record upward to the byte boundary specified by the largest alignment of any of the fields.
    If two fields share a common type specification, they are packed even if the declaration does not include the packed modifier and the record type is not declared in the {$A-} state. Thus, for example, given the following declarationtype
      TMyRecord = record
        A, B: Extended;
        C: Extended;
      end;A and B are packed (aligned on byte boundaries) because they share the same type specification. The compiler pads the structure with unused bytes to ensure that C appears on a quadword boundary.When a record type is declared in the {$A-} state, or when the declaration includes the packed modifier, the fields of the record are not aligned, but are instead assigned consecutive offsets. The total size of such a packed record is simply the size of all the fields. Because data alignment can change, it's a good idea to pack any record structure that you intend to write to disk or pass in memory to another module compiled using a different version of the compiler.
    ==============
    没有时间细细翻译了。
      

  10.   

    英文看不懂呢。
    又如:
       Ta= record
          b : Char;
          c : Char;
          a : Integer;
        end;按我的理解,以最大的成员字节数对齐,上面应该是4+4+4=12,但结果是8。不知如何理解呢
      

  11.   


    //最大字节对齐是并不是按最大字节分配内存.
    type
      Ta= record
        b : Char;
        c : Char;
        a : Integer;
      end;
    //上述结构存放如下图所示
    1 2 3 4 5 6 7 8 
    b c     a a a a
    如果改为
    type
      Ta= record
        b : Char;
        c : Char;
        a : Integer;
        d : Char;
      end;
    则存放结构为
    1 2 3 4 5 6 7 8 9 10 11 12
    b c     a a a a d 
      

  12.   


    对于此例,Extended的长度为10个字节,其对齐是要求在8个字节边界上,当然这里只有一个field,所以没有特别的对齐问题,但是当record长度不是8个字节的整数倍时,delphi还会自动将整个的record长度延长到8字节的整数倍,故为2×8=16=10+6。
      

  13.   

    如果将Extended改为Integer,那么delphi看到integer的对齐要求是4字节,整个record长度已经是4字节的整数倍(1×4)故不在延长record的长度。delphi是根据record中所有字段中对齐要求中最大的值的整数倍决定是否延长和延长多大record长度的。这里最大对齐要求值就是4字节的整数倍,而在有extended的情况下是8字节的整数倍。
      

  14.   

    如果将Extended改为Integer,那么delphi看到integer的对齐要求是4字节,整个record长度已经是4字节的整数倍(1×4)故不在延长record的长度。delphi是根据record中所有字段中对齐要求中最大的值的整数倍决定是否延长和延长多大record长度的。这里最大对齐要求值就是4字节的整数倍,而在有extended的情况下是8字节的整数倍。
      

  15.   

    最近,我才明白过来 packed 的用处。
    长见识。
      

  16.   

    record中各field的对齐要求中最大的那个如果是4字节地址的,那么就是4的倍数;
    record中各field的对齐要求中最大的那个如果是8字节地址的,那么就是8的倍数;
    具体与field的数据类型的关系:
    Ordinal types: size of the type (1, 2, 4, or 8) 
    Real types: 2 for Real48, 4 for Single, 8 for Double and Extended