Structures, am I doing this right?
ke4pjw
Posts: 1,283
Since I am trapped inside for the foreseeable future due to weather, I started looking at using structures.
I save configs in memory with my light controller project as binary chunks of memory. I wanted to make sure I can do the same with structures without complications.
I verified that when an array of structures are instantiated, all of the needed memory gets allocated as a single block, even if no data is assigned. I put together a little test harness to help me understand how the structure is represented in memory. It appears to work and it makes sense.
Working doesn't necessarily mean correct, so I am asking advice on if I am I using structures correctly here? Basically this is a multidimensional array.
{Spin2_v52}
CON ' Constants
_CLKFREQ = 160_000_000
STRUCT nametype(BYTE namefield[32], BYTE unusedextrabyte)
VAR ' Variables
nametype name[24]
byte test
OBJ ' Objects
PUB main()
STRCOPY(@name[0].namefield,@"namefield 1",12)
STRCOPY(@name[1].namefield,@"namefield 2",12)
STRCOPY(@name[2].namefield,@"namefield 3",12)
STRCOPY(@name[3].namefield,@"namefield 4",12)
debug(zstr(@name[0].namefield), uhex(@name[0].namefield))
debug(zstr(@name[1].namefield), uhex(@name[1].namefield))
debug(zstr(@name[2].namefield), uhex(@name[2].namefield))
debug(zstr(@name[3].namefield), uhex(@name[3].namefield))
debug("'nametype' structure is a 32 byte array named 'namefield' + 1 byte named 'unusedextrabyte'.")
debug("Total bytes for this sctructure: 33")
debug("24 structures of nametype, named 'name' were instantiated")
debug("Expected bytes: 792 (33*24)")
debug("Measured total size of instantiated 'name' structures: ",udec_(@test-@name), " bytes")
Cog0 INIT $0000_0000 $0000_0000 load Cog0 INIT $0000_0F64 $0000_1BFC jump Cog0 @name[0].namefield = "namefield 1", @name[0].namefield = $18E0 Cog0 @name[1].namefield = "namefield 2", @name[1].namefield = $1901 Cog0 @name[2].namefield = "namefield 3", @name[2].namefield = $1922 Cog0 @name[3].namefield = "namefield 4", @name[3].namefield = $1943 Cog0 'nametype' structure is a 32 byte array named 'namefield' + 1 byte named 'unusedextrabyte'. Cog0 Total bytes for this sctructure: 33 Cog0 24 structures of nametype, named 'name' were instantiated Cog0 Expected bytes: 792 (33*24) Cog0 Measured total size of instantiated 'name' structures: 792 bytes

Comments
I have been enjoying STRUCTS since their arrival. roughly, yours looks about like I'd use them. but, I am no strong Spin coder.
Guess would be nice if could do sizeof(name[0].namefield) for the STRCOPY bytes. Is there some Spin2 equivalent of that?
Suppose could make the #bytes in namefield a constant and then use that.
Sometimes nice to have things automatically adjusted in case you make changes later to #bytes in things....
I just wanted to make sure the syntax was correct and get an understanding of how it places things in memory.
Man, I SO wish I had this like 4 years ago.
perhaps this is what you're looking for? Been playing with these a bit. seems like a "Pascal String" to me

That looks like what we're doing in NAVCOM, more or less, I think you're on the right track.
The one thing to keep in mind when implementing this kind of data structure is unit testing is important to make sure your memory is doing what you want it to do. So while asking us is great, verifying it with unit tests is better. If you want some guidance for writing test code I'm happy to help when I'm less sick and less busy, just keep harassing me in this thread until I get around to it.