Page 1 of 1

Is it safe, or good practise, to allocate memory with SYS"malloc"?

Posted: Tue 22 Jan 2019, 19:33
by p_m21987
Hello,

I'm thinking of converting a program that I previously wrote in C, into BASIC for BBCSDL.
Without going into much detail, this program uses malloc() and free() quite a lot.

As far as I know, BBC BASIC doesn't have a built-in mechanism for freeing memory that's been allocated, for example with DIM. (If I'm wrong, please let me know.) So I was thinking I would simply use SYS to call malloc() and free() exactly as I did in my C program.

Is this safe to do? I mean, can I expect this to work without problems?
Is it good practise?
What do you all think?

Thanks everyone
- PM

Re: Is it safe, or good practise, to allocate memory with SYS"malloc"?

Posted: Wed 23 Jan 2019, 08:52
by DDRM
Hi PM,

Well, in terms of temporary memory allocation in BBC Basic it depends a bit on context - you can use DIM LOCAL to reserve temporary space, but only within a procedure/function (as far as I know).

Otherwise I've certainly seen others who know more than me using MALLOC in their programs, so it should be fine.

Anyone who knows better, please chip in!

:-)

D

Re: Is it safe, or good practise, to allocate memory with SYS"malloc"?

Posted: Thu 24 Jan 2019, 17:29
by p_m21987
Hello DDRM,

I've been aware of DIM LOCAL, which is handy, but it's not quite the same as the experience of using malloc() and free(). As you say, and as far as I know, you can only use it to allocate memory within a function or procedure, and you can only free it by returning from the function or procedure.

I wrote a short test program and using malloc and free via SYS seems to work without problems, so I will probably use that method, if I continue working on adapting this C program into basic for bbcsdl.

PM