[comp.lang.c] Huge arrays in a Small memory model...

simon.ewins@f664.n250.z1.fidonet.org (simon ewins) (05/29/90)

After receiving a lot of replies to my earlier question (re: using huge arrays in a segmented architecture Small memory model) that ranged from insulting to snobbish I did manage to glean some useful ideas that were buried in one or two.  After playing with the ideas for a while the following code has been verified with Turbo C on an IBM AT. (Minor changes and it also tested ok with M'Soft C as well).
 
I am sure there may be a more elegant way of doing this but the point is that it does the job and some of you may find the ideas (if not the code) useful.
 
If you find a more elegant approach or have any other ideas I would love to hear from you ....
 
#include <stdio.h>
#include <dos.h>
#include <alloc.h>
 
typedef struct {
   char a;
   char b[80];
   int c;
} FILES;
 
FILES huge *files;
 
#define ELEMENT(x) (files+x)   /* this is the guy I'd like to improve! */
 
main()
{
   int i,rc;
   char ra,rb[128],*get_far_data();
 
   files=(FILES huge *)farmalloc(2000L*83L);
   for(i=0;i<2000;i++) {
      ELEMENT(i)->a='!';
      put_far_data(ELEMENT(i)->b,"Some kind of string");
      ELEMENT(i)->c=i;
   }
 
/* now pick a group to display (records from 700 to 1000 in this case) */
 
   for(i=700;i<1000;i++) {
      ra=ELEMENT(i)->a;
      strcpy(rb,get_far_data(ELEMENT(i)->b);
      rc=ELEMENT(i)->c;
      printf("\n%4d: %c %s %d",i,ra,rb,rc);
   }
   farfree((FILES far *)files);
}
 
put_far_data(char huge *destination,char *source)
{
   while(*source) {*destination++=*source; ++source;}
   *destination=NULL;
}
 
char *get_far_data(char huge *source)
{
   static char return_buffer[128];
   int i=0;
 
   while(*source) return_buffer[i++]=*source++;
   return(return_buffer);
}
 
One can also define get_far_data() as a pointer to type FILES and return a
pointer to the structure in question located in the NEAR data segment...
 
eg:
 
FILES *get_far_data(char huge *source)
{
   char return_buffer[128];
   int i=0;
 
   for(i=0;i<sizeof(*files);i++) return_buffer[i]=*source++;
   return((FILES *)return_buffer);
}
 
Hope you find this interesting...Simon.
 
.

--- D'Bridge 1.30/002506
 * Origin: A_X_A_X_A  [ FactBase/qDos <> 416-483-2821 ] (1:250/664)