![]() |
Forum Index : Microcontroller and PC projects : CMM2: Function Returning a String
Author | Message | ||||
vegipete![]() Guru ![]() Joined: 29/01/2013 Location: CanadaPosts: 1132 |
The following code works just fine: dim string c c = GetLetter print c end function GetLetter() as string length 1 GetLetter = "123" end function But should it? Is that the desired behaviour? Can the size of the string returned by the function be specified? Changing the first line above to dim string length 1 generates an expected error.Visit Vegipete's *Mite Library for cool programs. |
||||
KeepIS![]() Guru ![]() Joined: 13/10/2014 Location: AustraliaPosts: 1882 |
I've never used basic like that with a function returning a string of x length, and I would not expect it to function correctly. I'm sure the length parameter was not intended to be used in this way as there is likely a default mem allocation for a string. I think length parameter was mainly for declaring arrays etc. But as usual I could be totally wrong in my assumptions ![]() Edited 2020-06-24 08:56 by KeepIS NANO Inverter: Full download - Only Hex Ver 8.1Ks |
||||
Geoffg![]() Guru ![]() Joined: 06/06/2011 Location: AustraliaPosts: 3292 |
First, when you call a function you need the brackets even if there is no argument: c = GetLetter() Second, the length qualifier is only used to tell MMBasic how much memory to allocate for the string. This is only important in arrays. To quote from page 58 of the manual: You should use the LEFT$() function to get the first character of the string: dim string c c = GetLetter() print c end function GetLetter() as string GetLetter = LEFT$("123", 1) end function Geoff Geoff Graham - http://geoffg.net |
||||
vegipete![]() Guru ![]() Joined: 29/01/2013 Location: CanadaPosts: 1132 |
Thank you for your reply. First issue, no brackets on function call, was a typo on my part. Apologies. The second part came from some testing after an error. Unfortunately, I didn't get the details. A running program halted with an error message along the lines of "insufficient string memory to assign a string" or some such. The indicated error line was a function definition returning a string. At first I thought perhaps the function had been called so many times as to fill up 'string memory' but some testing showed millions of calls to a function returning a string caused no problems. Since my function only needed to return a single character, I next experimented with forcing the function string length to 1, which led to the above example. Note that dim string c length 1 (Oh phooie, another typo in my original post) does work, and creates a string named 'c' that can hold only a single character, regardless of memory consumed.Visit Vegipete's *Mite Library for cool programs. |
||||
GregZone Senior Member ![]() Joined: 22/05/2020 Location: New ZealandPosts: 114 |
Hi Geoff. I'm a little unclear on your last sentence explaining LENGTH. Is this because each assignment to a regular string variable deallocates/allocates new storage, thereby always consuming just the current length of the string (+1 byte)? Or, are you saying that outside of an Array, a string allocation always consumes 256 bytes (length 255+1)? ie. In effect, the LENGTH keyword, when used outside of an Array, is simply ignored? Thanks Greg |
||||
TassyJim![]() Guru ![]() Joined: 07/08/2011 Location: AustraliaPosts: 6283 |
Hi Geoff. I'm a little unclear on your last sentence explaining LENGTH. Is this because each assignment to a regular string variable deallocates/allocates new storage, thereby always consuming just the current length of the string (+1 byte)? Or, are you saying that outside of an Array, a string allocation always consumes 256 bytes (length 255+1)? ie. In effect, the LENGTH keyword, when used outside of an Array, is simply ignored? Thanks Greg Any string will be allocated 256 bytes. If you limit to a short length, it will consume 256 bytes It will also raise an error of you try and store more in a string than it's declared length So DIM c$ LENGTH 1 will use 255+1 bytes but trying c$ = "KK" will give an error. Jim VK7JH MMedit |
||||
Chopperp![]() Guru ![]() Joined: 03/01/2018 Location: AustraliaPosts: 1097 |
@Jim Not sure what you mean. From the CMM2 Manual Strings will default to allocating 255 bytes (ie, characters) of memory for each element and this can quickly use up memory when defining arrays of strings. In that case the LENGTH keyword can be used to specify the amount of memory to be allocated to each element and therefore the maximum length of the string that can be stored. This allocation ('n') can be from 1 to 255 characters. For example: DIM STRING s(5, 10) will declare a string array with 66 elements consuming 16,896 bytes of memory while: DIM STRING s(5, 10) LENGTH 20 Will only consume 1,386 bytes of memory. Using LENGTH should reduce the amount of memory used for the String. Brian ChopperP |
||||
matherp Guru ![]() Joined: 11/12/2012 Location: United KingdomPosts: 10310 |
The minimum unit of memory is 256 bytes. This applies to strings, string arrays and also integer and float arrays. Setting a length on a simple string will control how much data you can save in it but will not change the memory allocated which will always be 256 bytes. The only reason I can see for setting a length on a simple string is if you need to control it in order to copy to or from a string array with a fixed length. |
||||
Chopperp![]() Guru ![]() Joined: 03/01/2018 Location: AustraliaPosts: 1097 |
Should the manual be updated to reflect this? ChopperP |
||||
![]() |
![]() |
The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |