In this section:


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.


Expression subtracts unrelated pointers



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

 * File: expudiff.c

#include <stdlib.h>

	char a[10], *b;
	int d;

	b = (char *)malloc(10);
	d = b - a;
	return (0);

Diagnosis at Runtime

[expudiff.c:12] **EXPR_UNRELATED_PTRDIFF**
>>	 		d = b - a;
	Expression subtracts unrelated pointers: b - a
	Left hand side	 : 0x00013878
	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
	Stack trace where the error occurred:
		main() expudiff.c, 12


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 suppressing EXPR_UNRELATED_PTRDIFF in the Suppressions Control Panel.