Re: A89: Re: when malloc fails...


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

Re: A89: Re: when malloc fails...




> > TIOS won't exit() the program if a memory allocation fails
> > (unless TIGCC has
> > implemented it that way). Instead, you'll get a null pointer or
> > handle returned
> > from the function, so it's up to you to free all the memory
> > you've allocated
>
> That is correct, I just wanted to know If I had to free all allocated memory
> "manually" on the calling level and up.

Like Sebastian said, that'd probably the best solution, yes.

> > In most cases you shouldn't have to use malloc() though.
>
> What should have I used? (My program may require several bytes in one place,
> and several kilobytes on another, or vice versa, and there is no way to
> predict how much memory will be needed in compile time)

If these memory blocks are used throughout the program you must use malloc() or
the TIOS equivalents, but if not, I think C supports dynamically sized
variables. Since these are "allocated" from the stack you can have a global
pointer variable and allocate the memory in main():s variable declarations, or
at a level where they are needed. If makes things more complicated though.
I know, I know - it's assembly thinking, but it works.

> > In a related topic, does TIGCC support BSS blocks yet? Can the
> > TIOS support it?
>
> As far as I understand it, TIGCC does not support BSS blocks, because TIOS
> doesn't.

Too bad. But it probably wouldn't have worked in TIOS's semi-multitasked system
anyway. :)

--
 / Niklas Brunlid
Check out Prosit for the TI-89 / TI-92+ at http://prosit.ticalc.org
Random PQF v5.1 Quote follows:

Consider the situation. There you are, forehead like a set of balconies,
worrying about the long-term effects of all this new 'fire' stuff on the
environment, you're being chased and eaten by most of the planet's large
animals, and suddenly tiny versions of one of the worst of them wanders
into the cave and starts to purr.
        -- Why humans like cats
           (Terry Pratchett, The Unadulterated Cat)





References: