Is this a bad pointer bug?
H. J. Lu
hjl@lucon.org
Fri Jul 18 15:14:00 GMT 2003
On Fri, Jul 18, 2003 at 09:43:43AM +0200, Marcel Cox wrote:
> "H. J. Lu" <hjl@lucon.org> wrote in message
> 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?
H.J.
More information about the Gcc
mailing list