[comp.lang.c] graphics & farmalloc

hennebry@plains.NoDak.edu (Michael J. Hennebry) (05/10/91)

The following turbo C 2.0 code takes up too much memory to
run from the TC environment, so I run it from the command line.
Both it and its predecessor do what I would expect until I
type tc to get back into the TC environment. Then I get a
program too big for memory message and do not get into the
TC environment.

How come? The error occurs with the compact, large, and huge
memory models. The problem does not occur if I farfree(layer)
before closegraph(). It does not occur if I replace j % 80L
with 30 .

#include <stdlib.h>
#include <alloc.h>
#include <stdio.h>
#include <io.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <graphics.h>

FILE *err;

typedef unsigned char byte;

byte *layer;
int pixelsx, pixelsy;
long screensize;

#define Lather(x) layer[test(0L, x, screensize)]

#define Layer(x,y) (Lather(pixelsx* (long)y+x))

struct rectangle { int left, upper, rite, lower; } ;

struct xy { int x, y; } ;

struct ratio { int num, den; } ;

struct simage {
   int xsize, ysize;
   byte colors;
   byte list[100];   /* 100=dummy value */
} ;

long int test(lo, x, hi) long int lo, x, hi; {
if(lo<=x && x< hi) return x;
fprintf(err, "x=%ld\n", x);
exit(0);
}


main() {
int driver, mode, gflag;
int fsparse;
int colors, color, icolor, xp, yp0, XPIXELS, YPIXELS;
int acolor[3]={ RED, YELLOW, BLUE } ;
byte aimage[2000];
int level, isize;
long j;

struct rectangle rect;
struct xy        offset;
struct ratio     scalex, scaley;
   err=fopen("err", "w");
   fprintf(err, "err opened\n"); fflush(err);
   driver=DETECT;
   initgraph(&driver, &mode, "C:\\tc");

   gflag=graphresult();

   if(gflag != grOk) {
      printf("\7graphresult=%d=%s\n", gflag, grapherrormsg(gflag));
      sleep(2);
      exit(-1);
   }
   fprintf(err, "driver=%d  mode=%d   maxx=%d   maxy=%d\n",
		 driver,    mode,     getmaxx(),getmaxy() );
   setbkcolor(BLACK);
   cleardevice();

   pixelsx=getmaxx()+1;  offset.x=200;  scalex.num=1; scalex.den=1;
   pixelsy=getmaxy()+1;  offset.y=100;  scaley.num=1; scaley.den=1;


   rect.left= 0; rect.rite= 1000;
   rect.upper=0; rect.lower=1000;

   level=40;

   fprintf(err, "sizeof(layer)=%d\n", sizeof(layer));

   layer=farmalloc(screensize= (long)pixelsx *
			       (long)pixelsy *
			       (long)sizeof(*layer) );

   if(layer==NULL) {
      closegraph();
      printf("not enough memory\n");
      exit(0);
   }
   for(j=(long)pixelsx*pixelsy-1; j>=0L; j--) {
      Lather(j)= j % 80L;
   }

   fprintf(stderr, "\7");
   scanf("%*[^\n]");
   closegraph();
   farfree(layer);
}
-- 
Mike   hennebry@plains.NoDak.edu
"Dea ex Machina reporting for duty" -- Aleytys