home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!uunet.ca!wildcan!sq!msb
- From: msb@sq.sq.com (Mark Brader)
- Subject: Re: Problem with Turbo-C: calculating with pointers
- Message-ID: <1993Jan10.024536.29542@sq.sq.com>
- Organization: SoftQuad Inc., Toronto, Canada
- References: <1993Jan7.112219.578@reks.uia.ac.be> <726524790snz@panache.demon.co.uk>
- Date: Sun, 10 Jan 93 02:45:36 GMT
- Lines: 59
-
- Reminder: it is neither necessary, nor preferred, nor desirable to
- quote an ENTIRE article when posting a followup to it!!
-
- > > char *pointer,*begin,*end;
- > > long size;
- > >
- > > size=71800;
-
- > make this 71800L and it will work. This compiler like many others treats
- > integer cosntants as 16-bit ints. 61800-65536 == 6264.
-
- If so, it is a compiler bug. Both K&R1 (page 180) and the ANSI/ISO
- standard (section 3.1.3.2/6.1.3.2) specify that an integer constant
- whose value doesn't fit in an int, but does fit in a long, has the
- type long. However, if there are really "many" compilers with this
- bug, then using 71800L is a suitable workaround.
-
- > > pointer=(char *)farmalloc(size*sizeof(char));
- > > begin=pointer;
- > > end=pointer+size;
- > > printf("%p %p %ld",begin, end, end - begin);
-
- If the results of converting the two pointers with %p had been given,
- we would have been able to tell whether the above conjecture was right!
- When posting a program in this situation, please include ALL relevant
- output.
-
- > > I would expect end-begin to return 71800. However it seems to return 6264.
-
- Perhaps the implementation expresses the difference between two pointers
- as an int rather than a long. If so, and if objects can actually be large
- enough to require a long to express the difference, this is stupidity
- at least. However, the only workaround I can suggest is, "get a better
- compiler".
-
- Note incidentally that printing the result of a pointer subtraction using
- %ld is implementation-dependent. The result actually could be an int, so
- %ld could, for example, pick up an extra word from the stack. Safer is
- to use %ld and cast the result of the subtraction to long.
-
- > Better still, don't do pointer arithmetic. It is a sure route to trouble.
-
- This is followed immediately by a signature line of "Nil taurus excretum".
- The two assertions seem to be in conflict.
-
-
-
-
-
- I'd like to leave it at that, but for the benefit of the Latin-illiterate,
- the signature quote is a mangled translation of "No bullshit". I am,
- therefore, saying that the suggestion to avoid pointer arithmetic is
- exactly that.
- --
- Mark Brader "Domine, defende nos
- SoftQuad Inc., Toronto Contra hos motores bos!"
- utzoo!sq!msb, msb@sq.com -- A. D. Godley
-
- This article is in the public domain.
-