home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 14 Text / 14-Text.zip / C6_BUGS.ZIP / C6_20056.BUG < prev    next >
Text File  |  1990-09-11  |  2KB  |  68 lines

  1. Q62912 Bad Code Generated for Difference Between Huge Pointers
  2. Microsoft C Compiler (C)
  3. 6.00   | 6.00
  4. MS-DOS | OS/2
  5.  
  6. Summary:
  7.  
  8. The following code demonstrates a case in which the C Compiler version
  9. 6.00 generates incorrect results when calculating the difference
  10. between two huge pointers.
  11.  
  12. Note that the pointer must address an array element inside of a
  13. structure for this problem to occur. Changing the types of the array
  14. elements, etc., does not solve the problem.
  15.  
  16. Sample Code
  17. -----------
  18.  
  19. #include <stdio.h>
  20.  
  21. struct s_type {
  22.             int ary[2]; /* Note: MUST point to array element inside
  23.                                                      struct */
  24.             int l;
  25.        } s, *sptr;
  26.  
  27. int * lptr1;
  28. long long2;
  29.  
  30. void main ( void )
  31. {
  32.     sptr = &s;
  33.     printf ( " &(sptr->ary[1]) is at %p\n", &(sptr->ary[1]) ) ;
  34.  
  35.     lptr1 = & ( sptr -> ary[1] ) ;
  36.     printf ( "lptr1 is at %p\n\n", lptr1 ) ;
  37.  
  38.     long2 =  lptr1 - &(sptr->ary[1]) ;  /* This should be 0 */
  39.     printf ( "difference is %ld bytes\n", long2 ) ;
  40.  
  41. }
  42.  
  43. More Information:
  44.  
  45. Compile the above program with the following:
  46.  
  47.    cl /AH /W4 /Od test.c
  48.  
  49. When you run the program, the first two addresses should be the same
  50. because they are pointing to the same location. The third printf()
  51. should return 0 (zero) bytes. However, under a huge model, you will
  52. get an incorrect return value.
  53.  
  54. One workaround is to use the quick compiler (/qc option), as follows:
  55.  
  56.    cl /qc /AH /W4 /Od test.c
  57.  
  58. This will generate the correct code.
  59.  
  60. Microsoft has confirmed this to be a problem in C version 6.00. We are
  61. researching this problem and will post new information here as it
  62. becomes available.
  63.  
  64. Keywords:  buglist6.00
  65.  
  66. COPYRIGHT Microsoft Corporation, 1990.
  67. Updated  90/06/14 06:15
  68.