Bad Code Generated for Difference Between Huge Pointers

ID Number: Q62912

6.00 | 6.00

MS-DOS | OS/2

buglist6.00 fixlist6.00a

Summary:

The sample code below demonstrates a case in which the C Compiler

version 6.0 generates incorrect results when calculating the

difference between two huge pointers.

Note that the pointer must address an array element inside of a

structure for this problem to occur. Changing the types of the array

elements, and so on, does not solve the problem.

More Information:

When you run the program, the first two addresses should be the same

because they are pointing to the same location. The third printf()

should return 0 (zero) bytes. However, under a huge model, you will

get an incorrect return value.

One workaround is to use the quick compiler (/qc option) because this

will generate the correct code.

Microsoft has confirmed this to be a problem in C version 6.0. This

problem was corrected in C version 6.0a.

Sample Code

-----------

/* Compile options needed: /AH /W4 /Od

*/

#include <stdio.h>

struct s_type {

int ary[2]; /* Note: MUST point to array element inside

struct. */

int l;

} s, *sptr;

int * lptr1;

long long2;

void main ( void )

{

sptr = &s;

printf ( " &(sptr->ary[1]) is at %p\n", &(sptr->ary[1]) ) ;

lptr1 = & ( sptr -> ary[1] ) ;

printf ( "lptr1 is at %p\n\n", lptr1 ) ;

long2 = lptr1 - &(sptr->ary[1]) ; /* This should be 0 */

printf ( "difference is %ld bytes\n", long2 ) ;

}

Additional reference words: 6.00