[comp.lang.c] Turbo - C

sglen@cs.strath.ac.uk (Scott M Glen CS89) (03/21/91)

Can anyone tell my why the following Turbo-c code causes a crash. (I am using
Turbo-c V2, on an NTS PC).

Basically it's a trivial piece of code, to save a small rectangular area of
the screen to memory, write something in the area (in this case a black
rectangle) and then restore the screen to its original status. 



void
help_option1 (void)

{
int i;
void *buffer;
unsigned size;

size=imagesize(0,0,20,20);  /* define rectangle to be saved */
if (size!=-1)               /* if area is too large imagesize returns -1 */
   {
   buffer=malloc (size);    /* allocate memory for area of screen */
   getimage (0,0,20,20);
   
   setfillstyle (SOLID_FILL,BLACK);
   bar (0,0,20,20);         /* black out the rectangle */
   i=getch();               /* wait for a key */

   putimage (0,0,buffer,COPY_PUT);   /* restore rectangle 
   free (buffer);
   }
else
   {
   /* image too large */
   }
}

When it runs, the black box apewars as expected, but then the screen freezes
I don't even know whether the problem lies with saving the area, or restoring 
it.

Any help would be very much appreciated.....


******************************************************************************
*				                                             *	
*  Scott Glen                                                                *
*  3rd Year Computer Science                                                 *
*  The University Of Strathclyde                                             * 
*  Glasgow                                                                   *
*  Scotland                                                                  *
*                                                                            *	
*  cadx830@uk.ac.strathclyde.computer-centre-sun			     *
*          sglen@uk.ac.strathclyde.cs				             *	
*                                                   		             * 
****************************************************************************** 

darcy@druid.uucp (D'Arcy J.M. Cain) (03/25/91)

[comp.lang.c is the wrong newsgroup for this.  See followup-To]
In article <6204@baird.cs.strath.ac.uk> Scott M Glen CS89 writes:
>   getimage (0,0,20,20);

I assume you have a "#include <graphics.h>" line.  You should also turn on
every warning you can.  Your compiler would have told you that the above
function call was missing an argument, the buffer to hold the image data.

>   i=getch();               /* wait for a key */
Just a nit, you don't need the assignment to i since you obviously don't
plan to use the value returned by getch().

>   putimage (0,0,buffer,COPY_PUT);   /* restore rectangle 
I'm not sure why it hangs here instead of when you read the image but it
certainly won't restore the screen.  My guess is that you are writing to
memory that isn't required until this function returns, perhaps the
stack itself.


-- 
D'Arcy J.M. Cain (darcy@druid)     |
D'Arcy Cain Consulting             |   There's no government
Toronto, Ontario, Canada           |   like no government!
+1 416 424 2871                    |