sandra@utah-cs.UUCP (Sandra J Loosemore) (07/04/87)
I've just run across a rather annoying bug in Alcyon C v4.14 (the developer's kit C): it has a hard time with adding a word-sized integer constant to a pointer variable. My code originally looked something like STRUCTURE *this, *next; next = this + sizeof(STRUCTURE); /* sizeof(STRUCTURE) = 8192 */ and it was generating code like: add.l #$80000000,R0 No difference whether I used "sizeof" or hardwired in the constant directly, same bad results. I eventually got around the problem by doing next = this; next++; In another place, I was doing something similar: STRUCTURE *this, *next; int n; next = this + n*sizeof(STRUCTURE); and instead of generating a multiply it generated a CLR.L instruction! -Sandra Loosemore sandra@cs.utah.edu, sandra@utah-cs.uucpu
jsgray@watmath.UUCP (07/04/87)
In article <4713@utah-cs.UUCP> sandra@utah-cs.UUCP (Sandra J Loosemore) writes: >I've just run across a rather annoying bug in Alcyon C v4.14 (the developer's >kit C): it has a hard time with adding a word-sized integer constant to >a pointer variable. My code originally looked something like > > STRUCTURE *this, *next; > next = this + sizeof(STRUCTURE); /* sizeof(STRUCTURE) = 8192 */ > >and it was generating code like: > > add.l #$80000000,R0 If you wanted 'next' to point to the next structure past 'this', you should have used 'next = this + 1'. 'next = this + 8192' advances next to the structure 8191 structures past 'this'. [K&R C Ref 7.4:] "The result of the + operator is the sum of the operands. A pointer to an object in an array and a value of any integral type may be added. The latter is in all cases converted to an address offset by multiplying it by the length of the object to which the pointer points." Jan Gray
sandra@utah-cs.UUCP (Sandra J Loosemore) (07/06/87)
As a number of people have pointed out to me, there is a bug in my understanding of how pointer arithmetic is supposed to work -- the only example I could find in K&R which showed adding an integer to a pointer used a char pointer, so I had assumed it was always treated as a byte offset. However, THERE IS STILL A BUG IN ALCYON C!!!! Or does the C standard say that 2**13 * 2**13 = 2**27, too??? And there is still that second problem I mentioned, where it was generating a CLR instead of a multiply.... -Sandra Loosemore sandra@cs.utah.edu, sandra@utah-cs.uucp
ger@qtecmuc.UUCP (07/06/87)
Indeed, you don't need sizeof(STRUCTURE) to increment the pointer to the next or any other structure. Just saying: next+=n; sets the pointer to the n'th next structure. This is because every pointer+integer operation always adds integer*sizeof(pointer object) to the pointer, so you don't have to care for the size of the object. This goes for pointer++ as well as for foo=pointer+5. next=this+sizeof(STRUCTURE) will set the pointer sizeof(STRUCTURE) structures further, probably to neverland. I doubt, if this was your intention. Gerhard Pehland UUCP: ...!seismo!unido!qtecmuc!ger
jdg@elmgate.UUCP (07/08/87)
In article <4716@utah-cs.UUCP> sandra@utah-cs.UUCP (Sandra J Loosemore) writes: >...... . . . . >a byte offset. However, THERE IS STILL A BUG IN ALCYON C!!!! Or does >...... . . . . >-Sandra Loosemore >sandra@cs.utah.edu, sandra@utah-cs.uucp Take heart! Your just beginning to find the bugs! 8^) As I understand it there are a few more, though I don't have Alcyon C. Did Atari give you a list of known bugs when you got your kit? BTW. Everyone has been bit by the pointer addition in C at least once..... On a different topic, thanks to all that replied about MWC. Based on those messages and some personal research, I ordered it through a local dealer. I specified 2.0 or >. Is there a ">"? Has anyone compiled a list of no-no's in MWC? -- Jeff Gortatowsky {seismo,allegra}!rochester!kodak!elmgate!jdg Eastman Kodak Company These comments are mine alone and not Eastman Kodak's. How's that for a simple and complete disclaimer?