Frank Heckenbach wrote:
- Compiling with gpc produces two warnings as indicated in the
comment. The compiler warns if the "casting" pointer type (outside the parenthesis) points to a larger type than the "casted" pointer type (inside the parenthesis).
Not really. As the message says, it warns that the alignment, not size, of the target type is larger.
Gale Paper brought to my attention that the same holds on the left side of an assignment and I am inclined to regard this as a bug - the compiler should check here for a smaller type, rather than a larger type.
While casting to a smaller type can be dangerous in some cases, so can any pointer conversion, so I'm not convinced that this case deserves particular attention.
Well, OK, the warning is about alignments not about sizes. Thanks for pointing that out.
Still I am not convinced that
theIntPtr:= IntPtr( theBytePtr); {testcast.pas:11: warning: cast increases required alignment of target type}
should produce a warning and left-side equivalent shouldn't
BytePtr( theIntPtr) := theBytePtr;
Both statements have the same effect. Actually, the target is in both cases a variable of type Integer. So, at the left side of an assignment, the target is the innermost type of the cast, not the outermost.
The same remarks apply for
theBytePtr:= BytePtr( theIntPtr); IntPtr( theBytePtr):= theIntPtr; {testcast.pas:14: warning: cast increases required alignment of target type}
but then the other way round.
Regards,
Adriaan van Os