Search

ParaSoft

HOME
PRODUCTS
SUPPORT
ABOUT
WHAT'S NEW
EVENTS


Insure++

Quick facts

Add-on Modules:
   -INUSE
   -TCA

Comparisons

Technical Papers

Support & Manuals

FAQs

Recent Reviews

User Testimonials

Press Releases


Insure tool to debug c++





Insure++ Reference - BAD_CAST



BAD_CAST

Cast of pointer loses precision

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, for example, pointers and integers are of different lengths.

Note that compilers will often catch this problem unless the user has "carefully" added the appropriate typecast to make the conversion "safe".

Problem

The following code shows a pointer being copied to a variable too small to hold all its bits.


	1: 	/*
	2:	 * File: badcast.c
	3:	 */
	4:	main()
	5:	{
	6:		char q, *p;
	7:
	8:		p = "Testing";
	9:		q = (char)p; bug
	10:		return 0;
	11:	}


Diagnosis (during compilation)


1.	[badcast.c:9] **BAD_CAST**
2.		Cast of pointer loses precision: (char) p
	>> 		q = (char) p;


  1. Source line at which the problem was detected.
  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.


<- ALLOC_CONFLICT -> BAD_DECL
Tools to debug c++ and java
(888) 305-0041 info@parasoft.com Copyright © 1996-2001 ParaSoft