mARGHloc

Started by
4 comments, last by SMurf7 22 years, 5 months ago
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?
Advertisement
You could also try
  screen = (unsigned char far*)farmalloc(64000);  

and remember to farfree(screen)....
But then you get unreadable code that looks like you are trying to allocate 64000 farms =).

Z.
______________"Evil is Loud"
As far as I''m aware - the "far" keyword isn''t apart of MSVC++, as it''s apparently obsolete now... There is "FAR" though...
If at first you don't succeed, redefine success.
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.
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
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.

This topic is closed to new replies.

Advertisement