|
Forum Index : Microcontroller and PC projects : Question about MMBasic String Allocation
| Author | Message | ||||
| seco61 Senior Member Joined: 15/06/2011 Location: AustraliaPosts: 205 |
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: AustraliaPosts: 205 |
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 StatesPosts: 3470 |
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 KingdomPosts: 10572 |
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: AustraliaPosts: 3308 |
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: AustraliaPosts: 205 |
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). Regards Gerard (vk3cg/vk3grs) |
||||
| robert.rozee Guru Joined: 31/12/2012 Location: New ZealandPosts: 2464 |
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: AustraliaPosts: 192 |
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: BrazilPosts: 303 |
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 KingdomPosts: 10572 |
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 |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |