A89: memory problems in ti-gcc.


[Prev][Next][Index][Thread]

A89: memory problems in ti-gcc.




Hi, now it is my turn too ask a question.
I have problems with allocating memory with ti-gcc.
When I run this the first time, it works just as expected, but the second (or
sometimes the third) time I run it, it hangs.
Typical memory allocation problem. But I just can't understand what the problem
is.
I have tried to not use malloc and free and used HeapAlloc, and HeapFree (and
HeapDeref ofcourse) too, but it makes no difference.
I have even tried to remove the grayscale part, since I thought it could
interfere, but no difference.
The first version I did, read the playfield from a static var, and that worked
perfectly, so It is unlikely it is anything else then memory handling..
So... please tell... what part of my brain is it that doesn't work? :)

//Olle

#define SAVE_SCREEN  //Have tried to remove this too, no difference.
#define USE_GRAY_PATCH
//..needed includes here.. removed to save lines.
int _ti89;

void DrawField();
static unsigned int hex[4][12]={{0},{0},
{0x07E0,0x0810,0x1008,0x2004,0x4002,0x8001,0x8001,0x4002,0x2004,0x1008,0x0810,0x7E0},{0x07E0,0x0FF0,0x1FF8,0x3FFC,0x7FFE,0xFFFF,0xFFFF,0x7FFE,0x3FFC,0x1FF8,0x0FF0,0x7E0}};

static char* playfield;
static void* light_gray;
static void* dark_gray; 

int _main()
{
  if(!GrayMode(GRAY_ON)) return;
  if((playfield = malloc(14*8))==NULL) return;
  light_gray = GetPlane(0);
  dark_gray = GetPlane(1);

  memset(playfield,1,14*8); // This is the only write I do to the mem-area.
  DrawField();		    // Drawfield just reads playfield and puts sprites
			    // on the screen accordingly.
  ngetchx();
  
  GrayMode(GRAY_OFF);
  free(playfield);

  return 0;    	
}

void DrawField()
{
  int i,j,xpos,ypos,fieldpos;
  for(i=0;i<14;i++)
    for(j=0;i%2?j<8:j<7;j++) {
      xpos = i*11;                  
      ypos = i%2?j*12:j*12+6;       
      fieldpos = *(playfield+i+j*8)*2;
      Sprite16(xpos,ypos,12,hex[fieldpos],light_gray,SPRT_OR);
      Sprite16(xpos,ypos,12,hex[fieldpos+1],dark_gray,SPRT_OR);
    }
}