Forum > General

structured types structure fpc/kylix problem

(1/3) > >>

Чебурашка:
Hello,
I have a .pas containing a record definition which is

1. used in fpc to define variables from this record definition
2. used in kylix to define variables from this record definition
3. there is a variable from this record definition created in a program compiled with kylix, which is streamed in a file and later read by a program compiled using fpc.

I saw that there are some differences in the way kylix and fpc compiler arrange the internal record structure (for example, if there is a enum, kylix used one byte, while fpc uses 4 as default).

I managed to make the structures generated by the two compilers exactely coincident and everithing works (I substituted the enums with integer, and later I did that needed casts).

I am now concerned with that fact that in future several things could happen:

1. I could unwittingly modify the record definition in a improper way
2. the fpc compiler might change the way records are structured (kylix compiler I believe will not change as borland is dead)

I would like to see if I can put something in the record structure that helps me to understand if the two generated structures are still aligned or not, so that I can put appropriate checks in code that help me understand at runtime if I broke smtg.

I was thinking to put some variable at the end of the record with fixed values that I can read before processing the received stream that guarantees with good reliance that record is aligned. Something like:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TSharedRecord = packed record      // Many variables...      //       CheckVariable: string[8];   end; const  CheckVariableValue = 'ILOVEFPC'; 
sending side:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  x: TSharedRecord;begin  // x. set all values  x.CheckVariable := ControlVariableValue;  Send(x); 
receiving side:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var  x: TSharedRecord;begin  Receive(x);  if x.CheckVariable <> ControlVariableValue then // tell big problem  else // x. read all values 
Any ideas/suggestions/critics?

JuhaManninen:

--- Quote from: Чебурашка on June 07, 2023, 09:15:57 am ---Any ideas/suggestions/critics?

--- End quote ---
Just dump Kylix and use FPC.

Чебурашка:

--- Quote from: JuhaManninen on June 07, 2023, 10:02:30 am ---Just dump Kylix and use FPC.

--- End quote ---

Thanks for the suggestion.

Others that do not foresee the dismission of kylix, as it is used to compile the software that operates the machines that my company sells worldwide since many years, and for which there is no plan for dismission in the near future (despite the long term goal is to switch to fpc)?

440bx:

--- Quote from: Чебурашка on June 07, 2023, 09:15:57 am ---Any ideas/suggestions/critics?

--- End quote ---
Given that FPC likely does everything you want Kylix to do, JuhaManninen's suggestion is likely the best course of action.

If, for some reason, that is not an option for you then you can add code to calculate the offset of every field in the record definition and compare those offsets with the values you expect them to be.  A bit tedious to maintain but, it guarantees the offsets match.

Also, just checking the offset of the last field isn't sufficient because one alignment difference of one field could be compensated by another one for some other field.

Чебурашка:

--- Quote from: 440bx on June 07, 2023, 10:09:09 am ---If, for some reason, that is not an option for you then you can add code to calculate the offset of every field in the record definition and compare those offsets with the values you expect them to be.  A bit tedious to maintain but, it guarantees the offsets match.

--- End quote ---

If it was an option I would not have asked.


--- Quote from: 440bx on June 07, 2023, 10:09:09 am ---Also, just checking the offset of the last field isn't sufficient because one alignment difference of one field could be compensated by another one for some other field.

--- End quote ---

I did not consider the mutual compensation, thanks for the suggestion.

I start thinking that another possibility could be to make a md5 or sha1 of the raw memory area used by the record and transmit it separately? Could it work?

Navigation

[0] Message Index

[#] Next page

Go to full version