Nothing but “packed” records — should I fix it?

纵饮孤独 提交于 2019-11-30 20:46:09

There is no way to answer this question without a full understanding of how each of those packed records are used in your application code. It is the same as asking "Should I change this variable declaration from Int64 to Byte ?"

Without knowing what values that variable will be expected and required to maintain the answer could be yes. Or it could be no.

Similarly in your case. If a record needs to be packed then it should be left packed. If it does not need to be packed then there is no harm in not packing it. If you are not sure or cannot tell, then the safest course is to leave them as they are.

As a guide to making this determination (should you decide to proceed), situations where record packing is required or recommended include:

  • persistence of record values
  • sharing of record values with [potentially] differently compiled code
  • strict compatibility with externally defined structures
  • deliberately overlaying a type layout over differently structured memory

This isn't necessarily an exhaustive list, and what these all have in common is:

  • records comprising a series of values in adjacent bytes that must and can be relied upon by any potential producer or consumer of the record without possibility of interference from the compiler or other factors

What I would recommend is that (if possible and practical) you determine what purpose packing serves in each case and add documentation to that effect to the record declaration itself so that anyone in the future with the same question doesn't have to go through that discovery process, e.g.:

  type
    TSomeRecordType = packed record
      // This record must be packed as it is used for persistence
      ..
    end;

    TSomeExternType = packed record
      // This record must be packed as it is required to be compatible
      //  in memory with an externally defined struct (ref: extern code docs)
      ..
    end;

The main idea of using packed records is not that you save a few bytes of memory! Instead, it is about guaranteeing that the variables are where you expect them to be in memory. Without such a guarantee, it would be impossible (or, at least, difficult) to manage memory manually on the heap and write to and read from files.

Hence, the program might malfunction if you 'unpack' the records!

If the record is stored/retrieved as packed or transfered in any way to a receiver that expects it to be packed, then do not change it.

Update :

There is a string type declared in your example. It looks suspicious, since storing the record in a binary file will not preserve the string content.

  • Packed record have length exactly size like members are.
  • No packed record are optimised (thay are aligned -> consequently higher) for better performance.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!