[comp.sys.ibm.pc] Structure spans a segment, huge pointer to structures.

rds95@leah.Albany.Edu (Robert Seals) (03/22/89)

Yo -

I have got a little question/problem. Suppose I declare a huge pointer
to a structure that is larger than 16 bytes, and then far[mc]alloc a
chunk of memory that is larger than 64k. No problem.
Now suppose the struct looks like this... {char a; double b, c}.
It would appear that the structure that borders the 64k line is going to
have troubles if one of the doubles also borders the line - the address
of the beginning of the double is in 1 segment, and at least part of it is
in another. The following illustates...

#include <stdio.h>
#include <alloc.h>

typedef struct { char a; double b, c;} rec;	/* 17 bytes with tc 1.5 */

void main()
{
	rec huge *hp;
	int i;

	if ((hp = (rec huge *) farcalloc(sizeof(rec), 4000)) == NULL) {
		printf("No alloc\n");
		exit(1);
	}

	/*
	 * The address of the beginning of the chunk
	 */
	printf("%Fp\n", hp);

	/*
	 * I picked '3850' here because if we use byte alignment, it
	 * should die on #3855 or so. If you use word alignment,
	 * it will be somewhat less...
	 * 
	 * The loop should print
	 * i, address of hp[i], 2.5, 3.5
	 */
	for (i=3850; i<4000; i++) {
		printf("%d %Fp ", i, hp+i);
		hp[i].b = 2.5;
		printf("%lf ", hp[i].b);
		hp[i].c = 3.5;
		printf("%lf\n", hp[i].c);
	}

	farfree((void far *) hp);
}

On my system, Turbo C 1.5, byte alignment, I get a floating point error
and either system hang or exit to command after it prints hp[3854].b.
So, I know WHY it does this; the question is whether it is CORRECT 
behavior.

Actually, the real question is why we don't have 32 bit pointers, which
then leads to why Intel felt backward compatibility with 8085 was so
inportant in 80286 and 386, and why aren't there any decent [read: cheap]
32 bit compilers, and then why are we still using DOS anyway? and why
isn't GNU finished with a 386 kernel yet???

rob

phco@ecsvax.UUCP (John Miller) (03/22/89)

Try

    typedef struct { char far a; double far b,c;} rec;

instead of

    typedef struct { char a; double b, c;} rec;	

In the second case, the variables are accessed via short (abbreviated)
addresses.

(There are two types of programmers:  those who hate Intel's segmented
 architecture, and those who lie about it.        :-)     )

-- 
                        John Miller  (ecsvax!phco)
                        Dept. of Pharmacology, Univ. of N.C.-Chapel Hill
			CB#7365   1026A FLOB
                        Chapel Hill, NC 27599       (919) 966-6966