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

Here you can talk about anything related to BBC BASIC, not covered in another category
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

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

Post 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
DDRM

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

Post 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
p_m21987
Posts: 177
Joined: Mon 02 Apr 2018, 21:51

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

Post 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