Path: utzoo!attcan!uunet!lll-winken!lll-tis!ames!mailrus!cornell!batcomputer!braner
From: braner@batcomputer.tn.cornell.edu (braner)
Newsgroups: comp.sys.atari.st
Subject: Re: MWC & large arrays -- help!
Summary: Yeah, why _would_ you need _structures_ > 32K?
Message-ID: <5327@batcomputer.tn.cornell.edu>
Date: 28 Jun 88 15:02:16 GMT
References: <734@cacilj.UUCP> <46700008@hcx2> <411@brambo.UUCP> <767@lakesys.UUCP> <2921@tekig5.TEK.COM>
Reply-To: braner@tcgould.tn.cornell.edu (braner)
Organization: Cornell Theory Center, Cornell University, Ithaca NY
Lines: 41

[]

I think the original question was _not_ naive:  why would one need
a _structure_ (rather than an array) that is more than 32K in size?
That's what many 68000 compilers cannot handle:  structure fields
that are offset more than 32K from the beginning of the structure.
That's because a field in a structure is accessed by adding an offset
(a constant calculated at compile time) to the base address.  On the
68000, the obvious, compact, efficient method is to use the indexed
addressing mode, but that limits you to an offset of +-32K.
That is very different from _array_ access, which (usually) involves
a calculation at run time:  the address of a[i] is base_of_a[] +
i * sizeof (element of a[]).  That's slower.  If you want a structure
like:
	struct humongous {
		int	dis;
		int	data[1000000];
		float	dat;
	}
you'll have a problem with most compilers (the offset of 'dat' is too large).
But you can do it like this:
	struct not_so_humongous {
		int	dis;
		int	*data;
		float	dat;
	}
where 'data' is just a pointer and you set it to point to the array that
is allocated separately.  What you win is far greater efficiency when
accessing more typical structures.

Of course, a _very_ smart compiler could switch from one addressing mode
to another depending on the size of a structure.

Note concerning the opposite from structures of arrays:  arrays of structures.
You might certainly want to use a very large array of many small structures.
Laser C has a bug in doing pointer arithmetic on pointers to structures, as
I have posted a while ago.  In short, struct mystruct a[i] works fine, even
for i very large, but struct mystruct *p;  p += i;  fails (uses the wrong
address) if (i * sizeof (mystruct) > 64K) and i is an int.  So use
p = &p[i];  instead.   This bug holds for arrays of _structures_ only.

- Moshe Braner