Type1=Record  //4+13=17
   id :longword;
   terminal_id:array[0..12] of char;
  end;
在程序中sizeof(type1)=20
 Type2=Record  //4+4=8
   id :longword;
   id2 :longword;
  end;
在程序中sizeof(type2)=8
 Type3=Record  //4+13+50=67
   id :longword;
   terminal_id:array[0..12] of char;
   dest:array[0..49] of char;
  end;
在程序中szieof(type3)=68
这是为什么?害得我调了几天,老是读数据错位

解决方案 »

  1.   

    把longword类型换成其它类型看看
      

  2.   

    Type3= packed Record  //4+13+50=67
       id :longword;
       terminal_id:array[0..12] of char;
       dest:array[0..49] of char;
      end;
    就不会了.
      

  3.   

    这是因为DELPHI的编译器默认会对结构进行数据对齐.这样有利于速度.
    但只要加上packed就不会(如上)这样有利于结约内存空间,但速度会慢一点.
      

  4.   

    楼上说得对,其实terminal_id:array[0..12] of char;占了16位
      

  5.   

    其实很简单,DELPHI帮助里面有说的,未压缩的Record与压缩的Record的区别。Instances of a structured type hold more than one value. Structured types include sets, arrays, records, and files as well as class, class-reference, and interface types. Except for sets, which hold ordinal values only, structured types can contain other structured types; a type can have unlimited levels of structuring.
    By default, the values in a structured type are aligned on word or double-word boundaries for faster access. When you declare a structured type, you can include the reserved word packed to implement compressed data storage. For example,type TNumbers = packed array[1..100] of Real;Using packed slows data access and, in the case of a character array, affects type compatibility. For more information, see Memory management.
      

  6.   

    结构的起始地址为4的倍数。
    在未压缩的结构中,单字节类型域从任何地址开始,双字节类型域从偶地址开始,四字节类型从4的倍数地址开始。
    例1:
    rec= record
      a: char;
      b: word;
    end;
    因为word型是双字节,需从偶地址开始存放,所以a后面会有一个字节空间不用,整个结构总共占用4个字节。
    例2:
    rec= record
      a: char;
      b: integer;
    end;
    因为integer型是4字节,需从4倍地址开始存放,所以a后面会有三个字节空间不用,整个结构总共占用8个字节。
      

  7.   

    这是Delphi编译器搞的鬼!!!默认情况下Delphi是按照字长度对齐的,这样可以提高程序的执行效率,所以你的结构得到的长度肯定是你的计算机字长的整数倍,目前的计算机都是32位的,所以你看看你的SizeOf的结果,肯定都是4的整数倍!!!避免这种情况有两种方法:1、取消Delphi的编译优化选项,但这样讲损失程序执行的效率(至少我感觉不到);2、使用Packed的关键字!!!