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
Is it safe, or good practise, to allocate memory with SYS"malloc"?
-
- Posts: 177
- Joined: Mon 02 Apr 2018, 21:51
Re: Is it safe, or good practise, to allocate memory with SYS"malloc"?
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
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
-
- Posts: 177
- Joined: Mon 02 Apr 2018, 21:51
Re: Is it safe, or good practise, to allocate memory with SYS"malloc"?
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
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