* So Y is my pointer so bogus
@ 1999-02-03 22:36 Uncle George
1999-02-03 22:59 ` Jason Duerstock
` (2 more replies)
0 siblings, 3 replies; 10+ messages in thread
From: Uncle George @ 1999-02-03 22:36 UTC (permalink / raw)
To: axp-list; +Cc: Dan Schmitt
This little example caused a (un-expected ) pointer failure in one of
the font routines in my java 1.2 port on linux/alpha. It was expected to
add a -1760 ( -1760 * sizeof(int)) but does not.
Ugg. what is a 64bitter to do.
gat
main()
{
int x1,x2,x3;
int y1,y2,y3;
unsigned int P;
unsigned int S;
void *a;
a = (void *)0;
x1=20,x2=0,x3=0;
y1=5, y2=0,y3=0;
P = 1;
S = 348;
a = (int *)a +(x2-x1-x3)*P + (y2-y1-y3)*S;
printf("a = %p\n",a);
}
^ permalink raw reply [flat|nested] 10+ messages in thread* Re: So Y is my pointer so bogus 1999-02-03 22:36 So Y is my pointer so bogus Uncle George @ 1999-02-03 22:59 ` Jason Duerstock 1999-02-03 23:32 ` B. James Phillippe ` (2 more replies) 1999-02-04 0:38 ` Michal Jaegermann 1999-02-04 11:25 ` Phil Carmody 2 siblings, 3 replies; 10+ messages in thread From: Jason Duerstock @ 1999-02-03 22:59 UTC (permalink / raw) To: axp-list; +Cc: Dan Schmitt Unless my brain has turned to jello lately, a = (void *) 0; is the same thing as saying a = NULL; and a = (int *) a; dereferences null, thus causing your program to go kerfloom. Perhaps you'd like to explain more clearly what you're trying to do? Jason On Wed, 3 Feb 1999, Uncle George wrote: > This little example caused a (un-expected ) pointer failure in one of > the font routines in my java 1.2 port on linux/alpha. It was expected to > add a -1760 ( -1760 * sizeof(int)) but does not. > Ugg. what is a 64bitter to do. > gat > main() > { > > int x1,x2,x3; > int y1,y2,y3; > unsigned int P; > unsigned int S; > void *a; > > a = (void *)0; > x1=20,x2=0,x3=0; > y1=5, y2=0,y3=0; > P = 1; > S = 348; > > a = (int *)a +(x2-x1-x3)*P + (y2-y1-y3)*S; > printf("a = %p\n",a); > } ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: So Y is my pointer so bogus 1999-02-03 22:59 ` Jason Duerstock @ 1999-02-03 23:32 ` B. James Phillippe 1999-02-03 23:45 ` Uncle George 1999-02-03 23:36 ` Uncle George 1999-02-03 23:40 ` Re[2]: " Dan 2 siblings, 1 reply; 10+ messages in thread From: B. James Phillippe @ 1999-02-03 23:32 UTC (permalink / raw) To: axp-list On Wed, 3 Feb 1999, Jason Duerstock wrote: > a = (int *) a; > > dereferences null, thus causing your program to go kerfloom. a = (int *) a; just means to tell the compiler to start _treating_ a as a pointer-to-int for that expression in case you wish to do ptr arith on it or deref it. But he doesn't ever dereference it. So: a = (int *)a + 1; will increment a's address by sizeof(int) * 1, which is 4 for Intel, Alpha, etc. > On Wed, 3 Feb 1999, Uncle George wrote: > > > This little example caused a (un-expected ) pointer failure in one of > > the font routines in my java 1.2 port on linux/alpha. It was expected to > > add a -1760 ( -1760 * sizeof(int)) but does not. > > Ugg. what is a 64bitter to do. I *think* I know what you're having problems with; I tried your sample code and some other math and when I do a %p on just plain (int*) 0 - 1760; I got a value of 0xffffffffffffe480 (which is expected), but your sample code instead gave 0x7ffffe480. Seems the 64bit range didn't get assigned in the version of your code. Is this what you were asking about? -bp -- B. James Phillippe . bryan@terran.org Linux Engineer/Admin . http://www.terran.org/~bryan Member since 1.1.59 . finger:bryan@earth.terran.org ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: So Y is my pointer so bogus 1999-02-03 23:32 ` B. James Phillippe @ 1999-02-03 23:45 ` Uncle George 1999-02-04 0:51 ` Michal Jaegermann 0 siblings, 1 reply; 10+ messages in thread From: Uncle George @ 1999-02-03 23:45 UTC (permalink / raw) To: axp-list yup, in essence yes. It seems that the "unsigned int" screws up the arith. As i remem changing the ints ( without the unsigned) to long didn't help either!. Its just an interesting result, which leaves me wondering about all the rest of the java 1.2 code that may have these little 64bit gotcha's B. James Phillippe wrote: > I *think* I know what you're having problems with; I tried your sample code > and some other math and when I do a %p on just plain (int*) 0 - 1760; I got > a value of 0xffffffffffffe480 (which is expected), but your sample code > instead gave 0x7ffffe480. Seems the 64bit range didn't get assigned in the > version of your code. Is this what you were asking about? > > - ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: So Y is my pointer so bogus 1999-02-03 23:45 ` Uncle George @ 1999-02-04 0:51 ` Michal Jaegermann 1999-02-04 0:55 ` Michal Jaegermann 0 siblings, 1 reply; 10+ messages in thread From: Michal Jaegermann @ 1999-02-04 0:51 UTC (permalink / raw) To: axp-list Uncle George wrote: > > yup, in essence yes. It seems that the "unsigned int" screws up the arith. I do no not follow. In that sense that you got what C says that you will get? I did not think that this was called "screws up". > As i > remem changing the ints ( without the unsigned) to long didn't help either!. I do not follow that either. I definitely got 0xffffffffffffe480 from printf("%p\n", (int *)a + ((x2-x1-x3)*P + (y2-y1-y3)*S)); Are you getting something else? If you will "save" parenthesis around this offset computation on the right results will be different as promotion rules will be applied in a different order. That what C Standard says, IIRC. Michal ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: So Y is my pointer so bogus 1999-02-04 0:51 ` Michal Jaegermann @ 1999-02-04 0:55 ` Michal Jaegermann 0 siblings, 0 replies; 10+ messages in thread From: Michal Jaegermann @ 1999-02-04 0:55 UTC (permalink / raw) To: axp-list Ugh! Sorry to followp my own message. :-( I wrote: > I do not follow that either. I definitely got 0xffffffffffffe480 > from > printf("%p\n", (int *)a + ((x2-x1-x3)*P + (y2-y1-y3)*S)); I meant "after making P and S **signed** quantities" - of course. Michal ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: So Y is my pointer so bogus 1999-02-03 22:59 ` Jason Duerstock 1999-02-03 23:32 ` B. James Phillippe @ 1999-02-03 23:36 ` Uncle George 1999-02-03 23:40 ` Re[2]: " Dan 2 siblings, 0 replies; 10+ messages in thread From: Uncle George @ 1999-02-03 23:36 UTC (permalink / raw) To: axp-list actually, its a neat trick use to fetch struct member byte offsets. Try it on alpha, and see what it does! Jason Duerstock wrote: > Unless my brain has turned to jello lately, > > a = (void *) 0; > ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re[2]: So Y is my pointer so bogus 1999-02-03 22:59 ` Jason Duerstock 1999-02-03 23:32 ` B. James Phillippe 1999-02-03 23:36 ` Uncle George @ 1999-02-03 23:40 ` Dan 2 siblings, 0 replies; 10+ messages in thread From: Dan @ 1999-02-03 23:40 UTC (permalink / raw) To: axp-list [-- Attachment #1: Body --] [-- Type: text/plain, Size: 1432 bytes --] According to the gods of SMTP, Jason Duerstock wrote: > Unless my brain has turned to jello lately, Hmm... sounds like me, lately. :) > a = (int *) a; > dereferences null, thus causing your program to go kerfloom. Actually, (int *)a isn't dereferencing a, it's typecasting it. > > int x1,x2,x3; > > int y1,y2,y3; > > unsigned int P; > > unsigned int S; > > void *a; > > > > a = (void *)0; > > x1=20,x2=0,x3=0; > > y1=5, y2=0,y3=0; > > P = 1; > > S = 348; > > > > a = (int *)a +(x2-x1-x3)*P + (y2-y1-y3)*S; Hmm. This should be equivalent to: a = 0 + -20 * 1 + -5 * 348. That should be a = -1760, assuming the type promotion that's going on there is working as expected. sizeof( int* ) should be 8, whereas sizeof( unsigned int ) should be 4. I can't remember the promotion rules for C (and I'm not sure I ever knew them for pointers :) but are the unsigned ints getting promoted to int*, or is the int* getting demoted to 32 bits? Or, even worse, is nothing happening at all, and bits are being lost during the operation? Perhaps you could try: a = (int *)a +(int *)((x2-x1-x3)*P) + (int*)((y2-y1-y3)*S); ..to force the unsigned ints to get promoted. Alternatively try declaring P and S as unsigned long ints, or as int* ? Dan Morrill Computer Scientist, Physicist If I'm talking out of my arse, please excuse me. :) ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: So Y is my pointer so bogus 1999-02-03 22:36 So Y is my pointer so bogus Uncle George 1999-02-03 22:59 ` Jason Duerstock @ 1999-02-04 0:38 ` Michal Jaegermann 1999-02-04 11:25 ` Phil Carmody 2 siblings, 0 replies; 10+ messages in thread From: Michal Jaegermann @ 1999-02-04 0:38 UTC (permalink / raw) To: axp-list Uncle George wrote: > > This little example caused a (un-expected ) pointer failure in one of > the font routines in my java 1.2 port on linux/alpha. Do you mean that you are getting different results when you will do a = (int *)a +(x2-x1-x3)*P + (y2-y1-y3)*S; and a = (int *)a +((x2-x1-x3)*P + (y2-y1-y3)*S); Ordering and promotion rules got you. :-) > It was expected to > add a -1760 ( -1760 * sizeof(int)) but does not. Eh??? Both P and S are unsigned int. Whatever a result it definitely will be not -1760. > Ugg. what is a 64bitter to do. Exactly what you asked for. :-) Print that way and things will be clear a = (int *)a + (x2-x1-x3)*P; printf("%p %x\n", a, (x2-x1-x3)*P); a = (int *)a + (y2-y1-y3)*S; printf("%p %x\n", a, (y2-y1-y3)*S); If you want signed quantities then do not use unsigned. Stupid tricks with types and their representations will eventually bite your back. BTW: " a = NULL ; printf ("%p\n", (int *)a - 1760); " will print 0xffffffffffffe480. Is this what you really wanted in the first place? Michal > main() > { > > int x1,x2,x3; > int y1,y2,y3; > unsigned int P; > unsigned int S; > void *a; > > a = (void *)0; > x1=20,x2=0,x3=0; > y1=5, y2=0,y3=0; > P = 1; > S = 348; > > a = (int *)a +(x2-x1-x3)*P + (y2-y1-y3)*S; > printf("a = %p\n",a); > } ^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: So Y is my pointer so bogus 1999-02-03 22:36 So Y is my pointer so bogus Uncle George 1999-02-03 22:59 ` Jason Duerstock 1999-02-04 0:38 ` Michal Jaegermann @ 1999-02-04 11:25 ` Phil Carmody 2 siblings, 0 replies; 10+ messages in thread From: Phil Carmody @ 1999-02-04 11:25 UTC (permalink / raw) To: axp-list Uncle George wrote: > > This little example caused a (un-expected ) pointer failure in one of > the font routines in my java 1.2 port on linux/alpha. It was expected to > add a -1760 ( -1760 * sizeof(int)) but does not. > a = (int *)a +(x2-x1-x3)*P + (y2-y1-y3)*S; in expressions of the form int * unsigned The int gets promoted to the unsigned type. The value therefore is _not_ -1760, it 0xFFFF~~~~ When promoted to a 64bit type (e.g. (type*)) as it's unsigned it will become 0x00000000FFFF~~~~ and then scaled by sizeof(type) It's doing exactly what was programmed, the programmer was in error. If you want the difference between pointers, why not use ptrdiff_t? That _is_ what it was invented for. FatPhil -- Phil Carmody Not speaking for or on behalf of Nokia Telecommunications. "At the showing of blue movies at Scotland Yard I take the precaution of removing my glasses, which reduces the whole messy business to an impressionist blur." John Mortimer, QC, 1978 ^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~1999-02-04 11:33 UTC | newest] Thread overview: 10+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 1999-02-03 22:36 So Y is my pointer so bogus Uncle George 1999-02-03 22:59 ` Jason Duerstock 1999-02-03 23:32 ` B. James Phillippe 1999-02-03 23:45 ` Uncle George 1999-02-04 0:51 ` Michal Jaegermann 1999-02-04 0:55 ` Michal Jaegermann 1999-02-03 23:36 ` Uncle George 1999-02-03 23:40 ` Re[2]: " Dan 1999-02-04 0:38 ` Michal Jaegermann 1999-02-04 11:25 ` Phil Carmody
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox