Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 09:01 30 Dec 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 : MMbasic RP2350 V6.01.00EXP with user-defined structures

     Page 2 of 2    
Author Message
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 528
Posted: 08:39pm 29 Dec 2025
Copy link to clipboard 
Print this post

You probably have a lot of feedback from the RP2040 version already but here's some more. The test program runs just fine with no errors and benchmarks I have run show no slowdown.

From here, it looks like the entire package is ready for primetime.
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2710
Posted: 08:50pm 29 Dec 2025
Copy link to clipboard 
Print this post

will new releases have a no structure version? I don't like block coding. I don't need it.
 
bfwolf
Regular Member

Joined: 03/01/2025
Location: Germany
Posts: 98
Posted: 08:52pm 29 Dec 2025
Copy link to clipboard 
Print this post

@Peter: I found a bug...  

Yesterday I was wondering if there's a difference in memory usage between RAM and a file when a struct doesn't have a size divisible by 8 (for uint64_t alignment), and you might be automatically adding padding bytes at the end to ensure alignment even with an array of structs: I was getting an 'address error' when accessing 'people(2).phone', followed by a reset of MMBasic.

I figured it out by first commenting out the initialization right at the DIM statement and then initializing each member individually. I tested which member was causing the error: Only when I commented out the statement 'people(2).phone = 5678' did it run flawlessly until 'END'.

Then I added a string member 'padding' to the end of the struct to increase its length from 25 to 32 bytes.

Then it ran flawlessly!

I then tried whether it also worked with a struct length of 28 (for uint32_t alignment): Yes, it works too!

So you could probably get by with uint32_t alignment when inserting padding bytes, which would save memory in RAM and the file! Unless you want uint64_t alignment in general, so that MMBasic is "future-proof" for 64-bit systems with mandatory alignment?

Of course, Struct(SIZEOF, type$) should then report the correct size including padding bytes. And in my opinion, the identical structure can be used in the file!

If the users want to be memory-efficient, they can arrange the struct members where possible and, if necessary, manually insert a padding member at the end. This way, they can see where there's still free space in the file's records without changing the record format. I do this in C as well and explicitly set them to 0.

Nevertheless, an automatic function in the interpreter should ensure that the alignment is correct and that there are no address errors.

Here my test-program: Crashes, if padding-member removed!

Option Base 1
Option Explicit

Type TPerson
 phone As integer
 surname As string Length 8
 name As string Length 7
 padding As string Length 2 'alignment to uint32_t seems OK, L=6 for uint64_t
End Type

Dim people(2) As TPerson ' = (1234,"Marther","Peter", 5678,"Graham","Geoff")

people(1).phone = 1234
people(1).surname = "Marther"
people(1).name = "Peter"

people(2).phone = 5678
people(2).surname = "Graham"
people(2).name = "Geoff"

Dim i As integer

Print "SIZEOF(TPerson)=";Struct(SIZEOF, "TPerson")

Print "VARADDR(people(1))=0x";Hex$(Peek(VARADDR people(1)))
Print "VARADDR(people(2))=0x";Hex$(Peek(VARADDR people(2)))

End

Open "a:/fpeople_u.bin" For Random As #2
For i=1 To 2
 Struct Save #2, people(i)
Next i
Close #2


An other thing: The interpreter seems to dislike lines that start with a 'comment within 'Type' and 'End Type'. An error message is thrown..
Don't know if this is a bug? We could live with it but perhaps it should then written in the docs..

Regards!  
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 528
Posted: 08:56pm 29 Dec 2025
Copy link to clipboard 
Print this post

  stanleyella said  will new releases have a no structure version? I don't like block coding. I don't need it.


You don't have to use structures if you don't want to. You can still program the way you are used to.
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 528
Posted: 09:05pm 29 Dec 2025
Copy link to clipboard 
Print this post

In MMBasic Is there a way to define a type as an array without having to use dot notation to access the members? Here's an example of a Zeno program that does it:

type conic : array[6] of real
var r,c as conic


now you can access r and c as arrays r(1), r(2)...r(6) and c(1), c(2)...c(6) etc.

I'd like to be able to do that in MMBasic. Te closest I can get right now seems to be

TYPE conic
a(6) as FLOAT
END TYPE

DIM r,c as conic


Then I have to use r.a(1), r.a(2), c.a(1), c.a(2), c.a(3), etc.

This is awkward coding.
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2890
Posted: 09:23pm 29 Dec 2025
Copy link to clipboard 
Print this post

It looks almost the same as the Zeno example if you lay it out like this.
TYPE conic : a(6) as FLOAT : END TYPE

DIM r,c as conic
: END TYPE is the only real difference I can see.
Edited 2025-12-30 07:25 by phil99
 
PeteCotton

Guru

Joined: 13/08/2020
Location: Canada
Posts: 589
Posted: 09:27pm 29 Dec 2025
Copy link to clipboard 
Print this post

  stanleyella said  I can get my head around "structured" basic ie simple flow with subs, no goto, but not structures. functions never used. I can't see any of this is would make any code I wrote better if I used structures.
I use option explicit and declare var types as.
I'm willing to learn though.


I suspect it's one of those situations where you will be trying to solve a problem one day, and suddenly go aha- this is where a structure would be very useful.

It can really help to simplify and tidy up your code. For example, if you wanted to write a function that returned more than one value, then structs make it trivial and easy to read.

For example: If you wanted to quickly convert an angle and distance to cartesian co-ordinates, which gets returned as two values, x & y you could do the following:


' This bit is the structure - effectively it's a new variable type called Point
' that is made up of two separate FLOATS.
Type Point
  x As FLOAT
  y As FLOAT
End Type

' And here we're going to define our function, but instead of returning a single
' value, we're going to return a Point variable which has an X and Y value
Function PolarToCartesian(angle!, length!) As Point
  PolarToCartesian.x = length! * COS(angle!)
  PolarToCartesian.y = length! * SIN(angle!)
End Function



Now when you want to find the x,y value for a point 45 degrees and 1 unit away from your origin you could do:



Dim p1 As Point
p1 = PolarToCartesian(45,1)

'  p1 now contains two values, p1.x and p1.y
PRINT p1.x
PRINT p1.y


I'm excited for them (hopefully making their way into the CMM2)  
 
phil99

Guru

Joined: 11/02/2018
Location: Australia
Posts: 2890
Posted: 09:35pm 29 Dec 2025
Copy link to clipboard 
Print this post

Thanks Pete.
Was also wondering where I might use this and Functions will be it.
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 528
Posted: 09:59pm 29 Dec 2025
Copy link to clipboard 
Print this post

  phil99 said  It looks almost the same as the Zeno example if you lay it out like this.
TYPE conic : a(6) as FLOAT : END TYPE

DIM r,c as conic
: END TYPE is the only real difference I can see.


But when you want to access a single member (say x) of r or c, you have to use r.a(x) or c.a(x)

In Zeno, you can treat r and c as arrays thus: r(x) or c(x)
 
stanleyella

Guru

Joined: 25/06/2022
Location: United Kingdom
Posts: 2710
Posted: 10:10pm 29 Dec 2025
Copy link to clipboard 
Print this post

no!! it was better before. I never got into bbc basic just wrote a game in 6502 and sold it. ran crap on electron ...his problem.
I don't see structures enhance mmbasic for joe user
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4181
Posted: 10:14pm 29 Dec 2025
Copy link to clipboard 
Print this post

The Zeno way isn't really helping understanding or maintenance much, as the code needs to know the r & c (in your example) are arrays.  You may as well just declare them as such and if anything make the code clearer.

John
 
bfwolf
Regular Member

Joined: 03/01/2025
Location: Germany
Posts: 98
Posted: 10:25pm 29 Dec 2025
Copy link to clipboard 
Print this post

  toml_12953 said  In MMBasic Is there a way to define a type as an array without having to use dot notation to access the members? Here's an example of a Zeno program that does it:

type conic : array[6] of real
var r,c as conic

...


It's nearly the same syntax in PASCAL. C offers 'typedef' for this job.

Unfortunately, Microsoft chose the `Type ... End Type` construct for structs back then, and nearly all BASIC dialects offering structs do it the same way, so it makes sense to follow that example to avoid introducing a new BASIC dialect. C uses the `struct` keyword for structs, and Pascal uses the `RECORD` keyword.

I've seen several BASIC dialects, but none them support type aliasing with `typedef` (C) or `TYPE` (PASCAL).

The only idea I have for how to achieve this with `Type ... End Type` would be to allow a single, anonymous member — something like this:

Type conic
 As float(10)
End Type

But I doubt that would be easy to implement in the interpreter! Besides, I don't think any other BASIC dialect does it that way.

Can't MMEDIT macros be defined with #define? Maybe that would work?

Regards.
Edited 2025-12-30 08:26 by bfwolf
 
toml_12953
Guru

Joined: 13/02/2015
Location: United States
Posts: 528
Posted: 10:38pm 29 Dec 2025
Copy link to clipboard 
Print this post

  JohnS said  The Zeno way isn't really helping understanding or maintenance much, as the code needs to know the r & c (in your example) are arrays.  You may as well just declare them as such and if anything make the code clearer.

John


The example I gave was greatly simplified to show just the essence of the problem.
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10756
Posted: 10:46pm 29 Dec 2025
Copy link to clipboard 
Print this post

  Quote  @Peter: I found a bug...

Excellent - good catch. The interpreter needs to make sure each instance of a struct is integer aligned. Will fix.
The interpreter should also allow comment lines inside a type declaration. I'll also look at that tomorrow.
  Quote  I don't see structures enhance mmbasic for joe user

So don't use them - others think it is a great enhancement
 
bfwolf
Regular Member

Joined: 03/01/2025
Location: Germany
Posts: 98
Posted: 10:46pm 29 Dec 2025
Copy link to clipboard 
Print this post

  stanleyella said  no!! it was better before. I never got into bbc basic just wrote a game in 6502 and sold it. ran crap on electron ...his problem.
I don't see structures enhance mmbasic for joe user


No one is forcing you to use structs! Everything else works as before. It seems that, apart from a minimal increase in code in Flash, it has no impact on speed or RAM.

But I predict: once you understand the possibilities of structs, you'll use them and won't want to be without them!

I started with Pascal at university a little over 40 years ago, where structs exist (called RECORDS) – and shortly after, C (where structs are also declared with 'struct'), which is the programming language I've primarily used professionally ever since. I don't know how I would manage large programs without such capabilities!

Structs offer a wonderful way to manage and manipulate different pieces of related information together in a single 'body'!

Imagine the following example:

You want to program a data logger that records weather data.

There's nothing more elegant than being able to write, for example,

Type weatherData
 temperature_degC As integer
 humidity_percent As integer
 airpressure_mbar As integer
 unix_time As integer ' seconds since 1.1.1970
End Type


and then simply

Dim weatherDataRecordings(1000) As weatherData


Greetings.
 
karlelch

Senior Member

Joined: 30/10/2014
Location: Germany
Posts: 297
Posted: 10:59pm 29 Dec 2025
Copy link to clipboard 
Print this post

For my applications, the possibility of defining structured types is really useful. They allow me to logically organize my code better and dramatically facilitate some coding (eg returning multiple values from a function, saving sets of variables in one go to disk etc.). So I really appreciate Peter's suggestion to add them to MMBasic.

If you don't like/need them, why not just ignoring them? I personally am not a fan of turtle graphics and did so far not use any stepper motors, but who knows when these feature will become useful for my projects?

In any case, I am really impressed by all the work Geoff, Peter and many of you invested into MMBasic and by what possibilities it offers today. I am grateful for being able to use such an incredible tool for free for my projects.

My 2 cents.

Best
Thomas
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4181
Posted: 11:00pm 29 Dec 2025
Copy link to clipboard 
Print this post

  toml_12953 said  
  JohnS said  The Zeno way isn't really helping understanding or maintenance much, as the code needs to know the r & c (in your example) are arrays.  You may as well just declare them as such and if anything make the code clearer.

John


The example I gave was greatly simplified to show just the essence of the problem.

A better example would help.  As it is, what you showed isn't the kind of thing structures are for.

John
 
     Page 2 of 2    
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