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 - EXPR_UNRELATED_PTRDIFF



EXPR_UNRELATED_PTRDIFF

Expression subtracts unrelated pointers

This error is generated whenever an expression tries to compute the difference between pointers that do not point into the same memory block.

The ANSI C language specification declares this construct undefined except in the special case where a pointer points to an object one past the end of a block.

Problem

The following code illustrates the problem by subtracting two pointers to different data objects.


	1:	/*
	2:	 * File: expudiff.c
	3:	 */
	4:	#include <stdlib.h>
	5:	
	6:	main()
	7:	{
	8:		char a[10], *b;
	9:		int d;
	10:
	11:		b = (char *)malloc(10);
	12:		d = b - a; bug
	13:		return (0);
	14:	}

Diagnosis (at runtime)


	[expudiff.c:12] **EXPR_UNRELATED_PTRDIFF**
1.	>> 		d = b - a;
	
2.		Expression subtracts unrelated pointers: b - a
	
		Left hand side	: 0x00013878
3.		In block	: 0x00013878 thru 0x00013881 (10 bytes)
				b, allocated at:
				malloc()    (interface)
			  	  main()    expudiff.c, 11
	
		Right hand side	: 0xf7fffb8c
		In block	: 0xf7fffb8c thru 0xf7fffb95 (10 bytes)
				a, declared at expudiff.c, 8

4.		Stack trace where the error occurred:
				  main() expudiff.c, 12
  1. Source line at which the problem was detected.
  2. Description of the problem and the expression that is in error.
  3. Description of the two pointers involved in the expression. For each pointer the associated block of memory is shown together with its size and the line number at which it was declared or allocated.
  4. Stack trace showing the function call sequence leading to the error.

Repair

While this construct is undefined according to the ANSI C language specification, it is supported on many machines and its use is fairly common practice. If your application genuinely needs to use this construct, you can suppress error messages by adding the option

	insure++.suppress EXPR_UNRELATED_PTRDIFF

to the .psrc file.


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