Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Published by Scroll Versions from space INSDEVEL and version 7.5.4

In this section:

Table of Contents
maxLevel1
minLevel1

Overview

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.


Excerpt

Scroll Table Layout
sortDirectionASC
repeatTableHeadersdefault
widths10%,60%,10%,10%,10%
sortByColumn1
sortEnabledfalse
cellHighlightingtrue

CodeDescriptionEnabledReportedPlatform
EXPR_UNRELATED_ PTRDIFF

Expression subtracts unrelated pointers

(tick)RuntimeWindows/Unix


Problem

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

Code Block
languagecpp
themeEclipse
linenumberstrue
/*
 * File: expudiff.c
 */

#include <stdlib.h>

main() 
{
	char a[10], *b;
	int d;

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

Diagnosis at Runtime

Code Block
languagetext
linenumberstrue
[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
  • Line 2: Source line at which the problem was detected.
  • Line 3: Description of the problem and the expression that is in error.
  • Line 5: 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.
  • Line 12: 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 suppressing EXPR_UNRELATED_PTRDIFF in the Suppressions Control Panel.