Re: [cc65] calloc() not failing

From: Ullrich von Bassewitz <uz1musoftware.de>
Date: 2012-11-12 15:04:26
Hi!

On Mon, Nov 12, 2012 at 10:19:02AM +0100, Marc 'BlackJack' Rintsch wrote:
> First time I run this, the program returns to the BASIC interpreter
> without printing the "Error" I expect.  On the next run the system hangs
> after half of the video memory is filled with @ signs, so it looks like
> `calloc()` fills memory it clearly shouldn't.  This pattern of the two
> runs is reproducible regardless of the compiler options for
> optimizations.  A glance at the `calloc()` source reveals there is no
> check for overflow of the multiplication of the arguments.  IMHO it
> should‽

I'm not at home currently, so I cannot check, but at least I can say that the
multiplication is not the problem. If you try to allocate more than 65535
bytes of memory, you have a problem in your program. Having calloc check that
won't help you.

> Background of the code snippet: Of course both values are not hard coded
> in the real program.  The count is user input and the size belongs to a
> struct.  I let the user input a count and want to tell him the count is
> too high if `calloc()` fails.  As there is no function in the C standard
> to ask beforehand how much free memory there is, I thought that is the
> way to go.

You can use _heapmemavail() and _heapmaxavail(). Both are non standard of
course.

Regards


        Uz


-- 
Ullrich von Bassewitz                                  uz@musoftware.de
----------------------------------------------------------------------
To unsubscribe from the list send mail to majordomo@musoftware.de with
the string "unsubscribe cc65" in the body(!) of the mail.
Received on Mon Nov 12 15:04:35 2012

This archive was generated by hypermail 2.1.8 : 2012-11-12 15:04:39 CET