This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c/11577] New: Gcc doesn't handle pointer subtraction right
- From: "hjl at lucon dot org" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 18 Jul 2003 14:20:52 -0000
- Subject: [Bug c/11577] New: Gcc doesn't handle pointer subtraction right
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11577
Summary: Gcc doesn't handle pointer subtraction right
Product: gcc
Version: 3.3.1
Status: UNCONFIRMED
Severity: critical
Priority: P2
Component: c
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: hjl at lucon dot org
CC: gcc-bugs at gcc dot gnu dot org
Gcc will produce the wrong result of pointer subtraction:
[hjl@gnu pointer]$ cat y.c
#include <stdlib.h>
#include <stdio.h>
struct a
{
unsigned int x[11];
};
int
main()
{
unsigned long i, delta;
struct a *c, *b;
c = (struct a *) malloc (sizeof (struct a) * 3);
b = (struct a *) malloc (sizeof (struct a) * 2);
if (b > c)
{
i = ((char *) b - (char *) c)/sizeof (struct a);
delta = b - c;
}
else
{
i = ((char *) c - (char *) b)/sizeof (struct a);
delta = c - b;
}
printf ("0x%lx, 0x%lx\n", c, b);
printf ("0x%lx, 0x%lx \n", i, delta);
if (i != delta)
abort ();
return 0;
}
[hjl@gnu pointer]$ gcc y.c
[hjl@gnu pointer]$ ./a.out
0x80496b0, 0x8049738
0x3, 0xba2e8ba6
Aborted