This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Is this a bad pointer bug?
On Fri, Jul 18, 2003 at 07:12:13AM -0700, H. J. Lu wrote:
> On Fri, Jul 18, 2003 at 09:43:43AM +0200, Marcel Cox wrote:
> > "H. J. Lu" <hjl@lucon.org> wrote in message
> > 20030718041719.GA21789@lucon.org">news:20030718041719.GA21789@lucon.org...
> > > # cat x.c
> > > #include <stdlib.h>
> > >
> > > struct a
> > > {
> > > unsigned int x[11];
> > > };
> > >
> > > unsigned long i;
> > > struct a *c=( struct a*)0x3f736080UL, *b= (struct a*)0x1000048UL;
> > >
> > > int
> > > main()
> > > {
> > > i = (0x3f736080UL-0x1000048UL)/sizeof (struct a);
> > > if (i != (c - b))
> > > abort ();
> > > return 0;
> > > }
> > > # gcc x.c
> > > # ./a.out
> > > zsh: 21264 abort ./a.out
> > >
> > > Intel compiler works fine. Is this a really bad pointer bug?
> > >
> >
> > Your pointers c and b are not even a multiple of 44 apart. So their
> > difference is absolutely meaningless.
> >
>
> There is no guarantee that pointers c and b will be a multiple of 44
> apart. I got
>
> [hjl@gnu pointer]$ cat y.c
> #include <stdlib.h>
> #include <stdio.h>
>
> struct a
> {
> unsigned int x[11];
> };
>
> unsigned long i;
> struct a *c, *b;
> char *p;
>
> int
> main()
> {
> b = (struct a *) malloc (sizeof (struct a) * 2);
> p = (char *) malloc (sizeof (struct a) / 2 + sizeof (struct a));
> c = (struct a *) malloc (sizeof (struct a) * 3);
> i = ((char *) c - (char *) b)/sizeof (struct a);
> printf ("%lx, %lx, %lx\n", c, p, b);
> printf ("%lx, %lx\n", i, c - b);
> if (i != (c - b))
> abort ();
> return 0;
> }
> [hjl@gnu pointer]$ ./a.out
> 8049768, 8049720, 80496c0
> 3, 8ba2e8be
> Aborted
> [hjl@gnu pointer]$ icc y.c
> [hjl@gnu pointer]$ ./a.out
> 804b900, 804b8b8, 804b858
> 3, 3
>
> What does C standard say on this?
That there's no bug.
[#9] When two pointers are subtracted, both shall point to
elements of the same array object, or one past the last
element of the array object; the result is the difference of
the subscripts of the two array elements.
--
Daniel Jacobowitz
MontaVista Software Debian GNU/Linux Developer