Skip to main content
gamedev.net
🔒 Locked

allocating memory error(compiler:turbo c)

Started by econoy Dec 22, 2004 at 7:19 AM 1 replies 650+ views
Original Post
econoy
econoy
I wrote a hydrology develop kit for my friends.They can only use Turbo C, it is old,though. :),I find a big problem in my programme. My expression is poor ,so it's better to let my code say it.Look at this code:

#include<stdio.h>
#include<stdlib.h>
int main()
{
  unsigned char* image;
  FILE* file;
  image=NULL;
  image=(unsigned char*)malloc(800*600*sizeof(unsigned char));
  memset(image,1,800*600);
  if(image==NULL)
   {
    printf("error allocating memory");
    return 1;
    }
  file=fopen("text.dat","wb");
  if(file==NULL)
  {
   printf("cannt open output file");
   return 2;
   }
  fwrite(image,800*600,1,file);
  free(image);
  fclose(file);
  return 0;
}
I compile in turbo c2.0,small compile mode,but that the file could not been created.Whats the reasons?and solution? --*---*---merry christmas---*---*---
IndyJones
IndyJones
What error message do you get from your program?
Have you already tried one of the larger memory models (if i remember right there is small, medium and huge or so)?
SiCrane
SiCrane
That's too much memory for Turbo C to allocate in one go in small mode. 800*600 is a lot bigger than 64K. You might get it to work in huge mode, but I doubt it. A better approach is to work with the object in chunks. Since all you are doing is writing the same value to the file, you can allocate a smaller buffer and just write that same buffer to the file multiple times.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.