Adriaan van Os wrote:
In fact, my exact need is to be able to compare records of the same type but if something can be done about that, it must be seen in a more generic way.
Now, for the memcmp() call, can I do it directly in my Pascal code? I used to call C code from Pascal through Pascal declarations and a C module to make the implementation, all with careful Pascal/C type mappings on prameters. If it is possible to call memcmp() directly, it would be a nice solution for my problem.
function memcmp( var b1, b2; len: Integer): CInteger; external; attribute ( name = 'memcmp');
len: SizeType
But no need to declare it yourself, anyway. MemComp is declared in the GPC module already.
But you have been warned.
Indeed!
Pascal Viandier wrote:
I know I can create new operators "=" and "<>" but in this case I will have to do it for the 600+ record types of the applications. There must be a better way to do it... In C, a simple memcmp() call could do the job :-(
Out of curiosity, what kind of application is this where you have to compare more than 600 different record types for equality? In my whole code base, I probably have just a handful of such cases (which I do field by field then).
I agree only partially on this one. If I want to know if two records of the same type have the same contents, they have to be identical byte-for-byte, even on strings length and record variants. If it is not the case, they are different and that's ok like this. The real problem here is the random content of the padding. It can make "equal" records different. At least it cannot make different records equal ;-)
I don't know exactly how SUN Pascal does this, but it works. By dumping records in binary format, I have always seen binary zeroes in the padded zones.
How carefully have you tested it? Did you try global, local and heap-allocated variables? Did you intentionally garbage-fill the memory used before allocating the records (not applicable to global ones, but to local and dynamic ones, with some tricks)?
Did you try variant records? Nested variants? Writing to one (larger) variant, then changing the selector to a (smaller) variant and checking that the (now) unused memory was cleared? What about schemata or other kinds of dynamically-sized types (as Sun Pascal supports them)?
Anyway, it would be possible to make GPC generate code for a proper field-by-field comparison of records and arrays, including variant records (checking only the active variant) or strings (checking only the active characters). If would be some effort for me to implement it, and I don't have any use for it myself. But I could do it as a paid job. If it would really save you so much time, it might be worth it to you. If interested, please contact my by private mail.
Frank