Re^2: Using small memory model functions on huge arrays (was See below...)
Mike Percy
grimlok at hubcap.clemson.edu
Tue May 29 03:38:45 AEST 1990
e89hse at rigel.efd.lth.se writes:
>>>char huge *files;
>>>int i;
>>>
>>>files=(char huge *)farmalloc(2000L*82L);
>>>
>>>for (i=0; i<2000; i++)
>>> strcpy(&files[i],"Some kind of character string");
>>>
>>>for (i=0; i<2000; i++)
>>> puts(&files[i]);
>>>
> Either you had a bad day programing this or you're really lost:
Looks like you're the one who was really lost.
>1) An int has the range -32768 <= n <= 32767, and 82*2000 is obviously not
> <= 32767 if you're using a 16-bit processor. (Ok we can argue a whole
> lot about that but I guess it's true for this the ase here.)
1) farmalloc takes an unsigned long argument. 2000L*82L = 164000L, with
a conversion to unsigned (which in this case does, effectively,
nothing).
>2) The idea with small memory model is that you use less than 64k of data,
> otherwise you're probably better off using big model rather than try
> to fix it, unless you're desperate for performance.
2) The idea behind the small memory model is that you use less than 64k
of static (i.e. pre-allocated) data. You can use much more than that in
dynamically allocated data space, although each hunk must be less than
64K, unless you use far*alloc, which is what he did.
>3) If you really want to minimise the memory usage why do you have a fixed
> array? (Many lines are probably less than 81 characters long.) An
> array with ptr to ptr or something would probably be more compact.
Pointer to pointers would do the trick, from what I read into the
article, he wants something like this:
char far * far *files; /* want far ptr to far ptrs */
/* avoid using huge ptrs if at all possible */
files = (char far * far *) farcalloc(2000,sizeof(char far *));
for(i = 0; i < 2000; i++)
files[i] = (char far *) farmalloc(82); /* each of them points to 82 */
/* chars */
>
> Mixing memory models isn't easy. If you know what you're doing you'll have a
>hard time doing it, otherwise you'll just waste a lot of time.
Someone else said that you'd have to write your own strcpy, etc.
routines. This is not necessarily the case, but is probably best.
My experience has proven that if you can do it all in one memory model,
do it. If not, see if you can change things so that you can do it. If
that fails, make sure you know what the hell you're doing, otherwise
you'll end up rebooting a lot.
More information about the Comp.lang.c
mailing list