Turbo C++ 1.0 Huge pointer addition error?

Warner Losh imp at Solbourne.COM
Mon Jan 14 12:21:18 AEST 1991


In article <9101122049.AA28040 at lilac.berkeley.edu> C512052 at UMCVMB.BITNET ("David K. Drum") writes:
>typedef struct { . . . } foo;
[+]>foo huge *hp1,hp2;
>hp1 = (foo huge *) calloc(sizeof(foo),16);  /* 16 foo structures */
>/* Do something with foo structure # 1 */
[*]>hp2 = hp1 + sizeof(foo);
>. . .

[+] should be foo huge *hp1, *hp2
[*] should be hp2 = hp1 + 1

The rules of pointer arithmetic in 'C' state that when you are adding
a number to the poitner, you are not really adding that number, but
instead you are asking the compiler for a pointer to the nth object
past the pointer.  Turbo 'C' is doing exactly what you told it to do
in squaring the sizeof (foo).  It is giving you the sizeof(foo)th
object past the current object of hp1.  It is not a bug, so I don't
think you can get a fix for it from Borland :-)

Read K&R's section on pointer math, or re-read the section on pointer
math in your Turbo C++ manuals before proceding.

Warner Losh		The Imp		imp at Solbourne.COM


-- 
Warner Losh		imp at Solbourne.COM
We sing about Beauty and we sing about Truth at $10,000 a show.



More information about the Comp.lang.c mailing list