Re: [cc65] Config files and heap space

From: Christian Groessler <chris1groessler.org>
Date: 2009-05-28 22:18:56
Hi Dan,

On Wed, 20 May 2009, Dan Winslow wrote:

> void heap_avail(void)
> {
>     int x;
>     char *t;
>     x=10;
>     while(1)
>     {
>       t=malloc(x);
>       if ( !t ) break;
>       free(t);
>       x+=10;
>     }
>     if ( x > 10 ) 
>       x-=10;
>     else
>       x=0;
>     cprintf("heap avail: %i bytes\r\n",x);
> }

[snip]

> If the the config file I am using has the start address set at 2E00 ( 
> STARTADDRESS: default = $2E00; ), I get around 30k reported by the above 
> function. If the config file I am using has the start address set at 
> 2000 ( which should be ok with the DOS I am using ), I get 0 k 
> reported. The program seems to run fine either way..altough I am not 
> using malloc in my code (yet).


You are using "int" for "x", and if you set the start address to 0x2000, 
it overflows and becomes negative. Then the final check (x > 10) sets it 
to 0.

I've tried a slightly modified version of your program, and I get 34200 
available bytes.


#include <atari.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>

void heap_avail(void)
{
  unsigned int x;
  char *t;
  x=10;
  while(1) {
    t=malloc(x);
    if (! t) break;
    free(t);
    x+=10;
  }
  if (x > 10)
    x-=10;
  else
    x=0;
  cprintf("heap avail: %u bytes\r\n",x);
}

int main(void)
{
  heap_avail();
  if (_dos_type != 1) cgetc();
  return 0;
}


I had similar strange experiences in the past and nowadays I always 
declare variables "unsigned" by default, unless I know I will really have 
to deal with negative values.

regards,
chris

----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Thu May 28 22:16:58 2009

This archive was generated by hypermail 2.1.8 : 2009-05-28 22:17:00 CEST