pack record /record 区别

delphi中Record 和Packed Record的区别   .

分类: delphi  2011-06-25 09:33 3669人阅读 评论(2) 收藏 举报 

delphiinteger编译器windows


      Record 和Packed Record 第一种不带packed关键字的结构体表明编译器编译时要求进行字对齐,而第二种带packed关键字的结构体表明编译器编译该结构体时不需要进行字对齐,这种方式对结构体中的字段访问会比第一种方式慢!但是更节约空间。有Packed 的占用内存小,但是速度慢一点。没Packed 的占用内存大,但是速度快一点   

     比如:        

     TA   =   record             

         a:   Char;             

         b:   Integer;         

     end;     

     和        

     TB   =   packed   record             

        a:   Char;             

        b:   Integer;         

     end;     

     中:     

     TA.b位于TA结构开始处第4个字节,   TB.b位于TB结构开始处第二个字节.   即TA结构中在TA.a和TA.b中间插了3个无用字节,   为TB.b在内存中的地址是按字对齐的--即这个地址能被4整除,   而TB中则没有这些无用字节,   但是TB.b不是字对齐的,   对它的访问比对TA.b慢.   

     例子1-----------------------------------------------------    

     type t=  packed record   和 type t=   record        

a:char;  {1}                         a:char;{8}   --因为a的长度不够一个字(8个字节),                                                                   而紧接则的d为一个字(8个字节)所以要补齐8     

d : double ;{8}                      d : double ;{8}     

b:smallint;{2}                       b:smallint; {8}                  

end;     :Sizeof(t) =13             end;    :  Sizeof(t) =24 的区别:看后面的注释{**} ;

例子2------------------------------------  

 type t=  packed record     和    type t=   record        

a:char;  {1}                         a:char;   //因为a的长度为1个字节,后面的b为两个字节累加3个字节,

                                                           //再后面d为8 个字节,所以a+b要补齐到8个字节     

b:smallint;{2}                       b:smallint; a+b占8个字节(即一个字)       

d : double ;{8}                      d : double ;{8}               

end;     :Sizeof(t) =13             end;    : Sizeof(t) =16 的区别:看后面的注释{**} ;

     在Windows中内存的分配一次是4个字节的。而Packed按字节进行内存的申请和分配,这样速度要慢一些,因为需要额外的时间来进行指针的定位。因此如果不用Packed的话,Delphi将按一次4个字节的方式申请内存,因此如果一个变量没有4个字节宽的话也要占4个字节!这样就浪费了

 

转载于:https://www.cnblogs.com/moonwind/p/4507628.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
好的,以下是一个基本的通过图形化界面运行的main函数: ``` import javax.swing.*; public class MedicalRecordGUI extends JFrame { private JTextField patientNameField; private JTextArea visitRecordArea; private JTextArea checkRecordArea; private JTextArea medicalAdviceArea; public MedicalRecordGUI() { initComponents(); } private void initComponents() { JLabel patientNameLabel = new JLabel(); JLabel visitRecordLabel = new JLabel(); JLabel checkRecordLabel = new JLabel(); JLabel medicalAdviceLabel = new JLabel(); JScrollPane visitRecordScrollPane = new JScrollPane(); JScrollPane checkRecordScrollPane = new JScrollPane(); JScrollPane medicalAdviceScrollPane = new JScrollPane(); JButton saveButton = new JButton(); patientNameLabel.setText("Patient Name:"); patientNameField = new JTextField(); visitRecordLabel.setText("Visit Record:"); visitRecordArea = new JTextArea(5, 20); visitRecordScrollPane.setViewportView(visitRecordArea); checkRecordLabel.setText("Check Record:"); checkRecordArea = new JTextArea(5, 20); checkRecordScrollPane.setViewportView(checkRecordArea); medicalAdviceLabel.setText("Medical Advice:"); medicalAdviceArea = new JTextArea(5, 20); medicalAdviceScrollPane.setViewportView(medicalAdviceArea); saveButton.setText("Save"); saveButton.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { saveButtonActionPerformed(evt); } }); GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(patientNameLabel) .addComponent(visitRecordLabel) .addComponent(checkRecordLabel) .addComponent(medicalAdviceLabel)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(patientNameField) .addComponent(visitRecordScrollPane, GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE) .addComponent(checkRecordScrollPane) .addComponent(medicalAdviceScrollPane)) .addContainerGap()) .addGroup(GroupLayout.Alignment.TRAILING, layout.createSequentialGroup() .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(saveButton) .addContainerGap()) ); layout.setVerticalGroup( layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addContainerGap() .addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(patientNameLabel) .addComponent(patientNameField, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(visitRecordLabel) .addComponent(visitRecordScrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(checkRecordLabel) .addComponent(checkRecordScrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) .addGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(medicalAdviceLabel) .addComponent(medicalAdviceScrollPane, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) .addComponent(saveButton) .addContainerGap()) ); pack(); } private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) { String patientName = patientNameField.getText(); String visitRecord = visitRecordArea.getText(); String checkRecord = checkRecordArea.getText(); String medicalAdvice = medicalAdviceArea.getText(); MedicalRecord record = new MedicalRecord(patientName, visitRecord, checkRecord, medicalAdvice); // 进行保存操作 } public static void main(String[] args) { MedicalRecordGUI gui = new MedicalRecordGUI(); gui.setVisible(true); } } ``` 需要注意的是,这个GUI只是一个演示,具体的保存操作需要根据实际需求进行实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值