Shop OBEX P1 Docs P2 Docs Learn Events
Structures, am I doing this right? — Parallax Forums

Structures, am I doing this right?

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
Sign In or Register to comment.