Archived

This topic is now archived and is closed to further replies.

SMurf7

mARGHloc

Recommended Posts

Hi, I''ve made this very program that opens a file, allocates a 64,000 byte buffer and reads data in from it, before closing it, opening another file, and then writing the data to it. However, it kept crashing, so I added some debug printfs. The code looks like:-
  
#include <stdio.h>

int main(void)
{
   FILE *fp;
   unsigned char *screen;

   fp = fopen("image", "rb");
   printf("Opened image\n");
   screen = (unsigned char *)malloc(64000);
   if (screen == NULL)
   {
      printf("Error: malloc\n");
      fclose(fp);
      return(-1);
   }
   printf("Allocated 64,000 bytes\n");
   fread(screen, 64000, 1, fp);
   printf("Data read into buffer\n");
   fclose(fp);
   printf("Closed image\n");
   fp = fopen("data.raw", "wb");
   printf("Opened data.raw");
   fwrite(screen, 64000, 1, fp);
   printf("Data written from buffer\n");
   fclose(fp);
   printf("Closed data.raw");
   return(0);
}
  
The output from this is:- Opened image Error: malloc Is 64,000 bytes too much memory?!? It won''t always be that much, so a static array allocated at the beginning is not that answer. This was compiled under Borland Turbo C++ 3.0. If I try to compile it using a large memory model and then run it, it appears to allocate the memory ("Allocated 64,000 bytes") then I get a big nasty "Windows Protection Error, you must restart your computer". Pfft. Any ideas?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
You could also try
  
screen = (unsigned char far*)farmalloc(64000);

and remember to farfree(screen)....

Share this post


Link to post
Share on other sites
quote:

This was compiled under Borland Turbo C++ 3.0



Yes, 64k is too much - change the memory model your program to Huge, and then look up the far pointers.


OR switch to a 32 bit system, where the memory model is flat. Memory management in the 16bit days and prior was a p.i.t.a.

Share this post


Link to post
Share on other sites
Oh man, all these problems I remember oh so fondly!

unsigned char far *VideoMemory=MK_FP(0xa000, 0x0000);

"far" is a keyword I don''t miss.

Share this post


Link to post
Share on other sites