In this section:
Overview
Porting code between differing machine architectures can be difficult for many reasons. A particularly tricky problem occurs when the sizes of data objects, particularly pointers, differ from that for which the software was created. This error occurs when a pointer is cast to a type with fewer bits, causing information to be lost, and is designed to help in porting codes to architectures where pointers and integers are of different lengths.
Code Description Enabled Reported Platform BAD_CAST Cast of pointer loses precision Compilation Windows/Unix suppressEDGWarning off
option must be used in order for Insure++ to find this error.
Modern compilers will often catch this problem unless the user has explicitly forced the conversion with an appropriate type cast.
Problem
The following code shows a pointer being copied to a variable too small to hold all its bits.
/* * File: badcast.c */ main() { char q, *p; p = "Testing"; q = (char)p; return 0; }
Diagnosis at Compilation
[badcast.c:9] **BAD_CAST** Cast of pointer loses precision: >> q = (char) p;
- Line 1: Source line at which the problem was detected.
- Line 2: Description of the problem and the expression that is in error.
Repair
This error normally indicates a significant portability problem that should be corrected by using a different type to save the pointer expression. In ANSI C the type void *
will always be large enough to hold a pointer value.