Question

I have a program here which was working fine in Delphi 3 that I compiled and tested on Turbo Delphi 2006 and found a problem. The problem is this: I was using "cardinal" data types as an index for something. It worked in Delphi 3, but I found values were greater than they should be in the Turbo Delphi 2006 compiled version by about 128-256 or so depending on the specific data. Changing these data types to "longint" fixed the problem so the program worked correctly with both compilers.

The question: Why is this?

My understanding was that Cardinal data types were just typical unsigned integer data. This is consistent with the application of them in this program, especially proved by the fact that the Delphi 3 compilation worked correctly. So why did the Turbo Delphi 2006 compilation not work?

Was it helpful?

Solution

In Delphi, unsigned types are just subrange types of the next larger signed type. In Delphi 3, there is no 64-bit type, so there is no next larger type for Cardinal to be a subrange of. Cardinal is a signed type in Delphi 3 because of a technical limitation in the language. Delphi 4 introduced Int64, and Cardinal was made to be an unsigned subrange of that type (and then the limitation was that the was no unsigned 64-bit type).

In short, you were never getting true unsigned behavior in the first place. Now that you've upgraded, you've exposed long-present problems that have always been in your code.

That your code compiled is no proof that your code was correct. Delphi 3 cannot enforce rules that require types it doesn't really have.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top