Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 23:35 12 Nov 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : Question about MMBasic String Allocation

Author Message
seco61
Senior Member

Joined: 15/06/2011
Location: Australia
Posts: 205
Posted: 09:51am 15 May 2019
Copy link to clipboard 
Print this post

Hi All.

Here is a question that hopefully those that know the MMBasic source code well can answer.

The manual states that to preserve memory, you can use the length parameter when allocating string arrays:

dim string tstStr(5) length 10

My question is: does this apply to non-array strings?

dim string tst2Str length 10

The syntax is accepted, but I would like to know if the allocated memory obeys this syntax.

Regards

Gerard (vk3cg/vk3grs)
 
seco61
Senior Member

Joined: 15/06/2011
Location: Australia
Posts: 205
Posted: 10:10am 15 May 2019
Copy link to clipboard 
Print this post

Hmmm...

I just did a few tests followed by the memory command after each allocation and it would appear that the "length" syntax is ignored for non-array strings.

Any chance this could be changed to obey the syntax - it could potentially save a large amount of RAM - which for some programs running on the smaller chips could be very useful.

The reason for bringing this up was after converting some programs (that used to use basic drawing commands and some complicated logic and touch interrupts to provide number editing) to the GUI based controls, I am seeing about an additional 25K of RAM being used (I have about 90 controls - 5K in additional variables and 20K General) and this is making some of my programs very tight (as in failing ) on memory...

Regards

Gerard (vk3cg/vk3grs)
 
lizby
Guru

Joined: 17/05/2016
Location: United States
Posts: 3470
Posted: 11:41am 15 May 2019
Copy link to clipboard 
Print this post

  seco61 said  this is making some of my programs very tight (as in failing ) on memory...

It has been suggested in the past that the immediate solution is to put some of those individual strings into arrays (with limited length). That's not necessarily a clean fix, but if you're at an impass ....

PicoMite, Armmite F4, SensorKits, MMBasic Hardware, Games, etc. on fruitoftheshed
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10572
Posted: 12:10pm 15 May 2019
Copy link to clipboard 
Print this post

  Quote  The syntax is accepted, but I would like to know if the allocated memory obeys this syntax.


Memory is allocated in 256 byte pages, so strings, string arrays, integer arrays, and float arrays will always take some multiple of 256. In the case of simple strings each one will take 256 bytes even though the length parameter has been specified
 
Geoffg

Guru

Joined: 06/06/2011
Location: Australia
Posts: 3308
Posted: 12:16pm 15 May 2019
Copy link to clipboard 
Print this post

The LENGTH attribute is accepted on non arrayed strings but, as you have discovered, it does not make any difference. This is because the minimum memory allocation by the memory management system is 256 bytes. Ie, a page of RAM is 256 bytes.

Theoretically it would be possible to reduce the page size used by MMBasic but memory management is a trade off between page size and memory wasted keeping track of all the pages. For MMBasic 256 bytes seems about right.

As lizby said, for a workaround you could define an array with a limited length for each element. However this will not help you with the GUI controls which tend to chew through the RAM. BTW, that must be some program that you have - I have never come even close to using up all the RAM.

Geoff
Geoff Graham - http://geoffg.net
 
seco61
Senior Member

Joined: 15/06/2011
Location: Australia
Posts: 205
Posted: 08:53pm 15 May 2019
Copy link to clipboard 
Print this post

Hi All.

Thank you for the replies and the explanation of how the memory is allocated (particularly the fact that all arrays are allocated in multiples of 256 bytes).

At least with this knowledge I can improve the memory usage and if necessary utilise some arrays for the smaller strings - though it does not help the readability .

In this case it would be nice for some mechanism to use a substitute name for particular array elements - eg DEFINE varname=st(0) (similar to using CONST but evaluated as each statement uses it, not when first created, like a "C" #define macro).

Though of course it could be used for any substitution, not just array elements...

And Geoff, the program has many strings, and arrays of floats, arrays of integers and arrays of strings... Hence learning about the array memory allocation is very useful for future programming practices (and retrofitting the existing programs).Edited by seco61 2019-05-17

Regards

Gerard (vk3cg/vk3grs)
 
robert.rozee
Guru

Joined: 31/12/2012
Location: New Zealand
Posts: 2464
Posted: 02:11pm 16 May 2019
Copy link to clipboard 
Print this post

  Geoffg said   The LENGTH attribute is accepted on non arrayed strings but, as you have discovered, it does not make any difference. This is because the minimum memory allocation by the memory management system is 256 bytes. Ie, a page of RAM is 256 bytes.


hi geoff,
could this possibly be made very clear in the manual - that while the length can be set for a non-arrayed string, the memory allocation is still for the full 255 characters?

a question - how practical would it be to have the block size setable at the start of a program, before any variables have been declared, by adding the following:
OPTION ALLOC n
where n is a power of 2 between 8 and 256 (ie, 8, 16, 32, 64, 128, 256), to give maximum individual string lengths of 7, 15, 31, 63, 127 and 255 characters?


cheers,
rob :-)
 
Bizzie
Senior Member

Joined: 06/07/2014
Location: Australia
Posts: 192
Posted: 04:47am 10 Jun 2019
Copy link to clipboard 
Print this post

Hi All,

Just been caught with using this method of declaring a string!

dim tst(200) as string length 12

The length is ignored BUT tst() is declared as string.

MUST USE dim tst(200) length 12 as string or other methods eg

dim string tst(200) length 12 or dim as string tst(200) length 12
Rob White
 
MauroXavier
Guru

Joined: 06/03/2016
Location: Brazil
Posts: 303
Posted: 10:52am 10 Jun 2019
Copy link to clipboard 
Print this post

I believe it's:
dim tst$(200) length 12

In Basic you need to put the $ simbol after a variable to declare it as a string.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10572
Posted: 11:01am 10 Jun 2019
Copy link to clipboard 
Print this post

  Quote  In Basic you need to put the $ simbol after a variable to declare it as a string.


Not true in MMBasic. You can declare a string in a number of ways (3) - see the DIM command in the manual. The only thing you can't do is declare it with a mismatched type identifier e.g.

dim x% as string
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025