[comp.lang.c] Turbo C++ 1.0 Huge pointer addition error?

C512052@UMCVMB.BITNET ("David K. Drum") (01/13/91)

I am having a problem with huge pointer addition on Turbo C++ 1.0, and suspect
that there may be an error in the compiler itself.  When I attempt to add some
integral value to a huge pointer, the result is the pointer plus the integral
value SQUARED!  For example (in rough code):

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);
. . .

In my section of code, calculating the 20-bit addresses of hp1 and hp2 and then
subtracting hp1 from hp2 equals sizeof(foo) squared!  My above code isn't
kosher, I know, so don't tear it up, but it is essentially what I am doing.
I have switched from huge to far, and it works fine, but I need to use huge
pointers.  I have broken down for right now, and I am breaking the pointer
into SEG and OFS, doing the arithmetic myself, and patching them back together
(it works, of course) but I shouldn't have to do that.  Has anyone else found
a similar case, has Borland release a bug fix, and where can I get it?

While I'm thinking about it, I can't use F4 to run to a point in my program
if it isn't already running.  I have to use F7 or F8, then F4 works.  Otherwise
I run out of memory and the computer crashes.

HELP!

David K. Drum  C512052@UMCVMB.BITNET
                      @UMCVMB.MISSOURI.EDU

hpa@casbah.acns.nwu.edu (Peter Anvin) (01/13/91)

In article <9101122049.AA28040@lilac.berkeley.edu> C512052@UMCVMB.BITNET ("David K. Drum") writes:
>I am having a problem with huge pointer addition on Turbo C++ 1.0, and suspect
>that there may be an error in the compiler itself.
>Has anyone else found
>a similar case, has Borland release a bug fix, and where can I get it?

Yes.  You can order an upgrade to Turbo C++ 1.01 directly from Borland,
they ship it FedEx at no charge.  Contact Borland in one of the ways
mentioned in your manual (I think it is Getting Started) and give them your
serial #; they will send you the new version.

NB: This is not a patch.  You get 8 new distribution disks.

If the =/About command in TC or the header message of TCC says it is "Turbo
C++ version 1.00", you should order the upgrade.

I hope this solves yours and any other's similar problems.


-- 
H. Peter Anvin +++ A Strange Stranger +++ N9ITP/SM4TKN +++
INTERNET:  hpa@casbah.acns.nwu.edu   FIDONET:  1:115/989.4
BITNET:    HPA@NUACC                 RBBSNET:  8:970/101.4

gwyn@smoke.brl.mil (Doug Gwyn) (01/13/91)

In article <9101122049.AA28040@lilac.berkeley.edu> C512052@UMCVMB.BITNET ("David K. Drum") writes:
- typedef struct { . . . } foo;
- foo huge *hp1,hp2;

That should be
  foo huge *hp1,*hp2;

- hp2 = hp1 + sizeof(foo);
- In my section of code, calculating the 20-bit addresses of hp1 and hp2 and then
- subtracting hp1 from hp2 equals sizeof(foo) squared!

Sounds to me like the compiler is doing precisely what you told it to.
Perhaps you should read up on pointer arithmetic before proceeding.

imp@Solbourne.COM (Warner Losh) (01/14/91)

In article <9101122049.AA28040@lilac.berkeley.edu> C512052@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@Solbourne.COM


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

grimlok@hubcap.clemson.edu (Mike Percy) (01/15/91)

gwyn@smoke.brl.mil (Doug Gwyn) writes:

>In article <9101122049.AA28040@lilac.berkeley.edu> C512052@UMCVMB.BITNET ("David K. Drum") writes:
>- typedef struct { . . . } foo;
>- foo huge *hp1,hp2;

>That should be
>  foo huge *hp1,*hp2;
 
Actually it sould be
  foo huge *hp1, huge *hp2;

unless you really want a near (or far, depending on memory model)
pointer for hp2.
 
The pointer types are
*     (default type for memory model)
near *
far *
huge *
 
Near, far, and huge are called pointer modifiers, and only affect one
pointer. 


"I don't know about your brain, but mine is really...bossy."
Mike Percy                    grimlok@hubcap.clemson.edu
ISD, Clemson University       mspercy@clemson.BITNET
(803)656-3780                 mspercy@clemson.clemson.edu