Copying Strutures

Discussions about the BBC BASIC language, with particular reference to BB4W and BBCSDL
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Copying Strutures

Post by MattC »

Hi,

In the help, in the section on Structures, there is a paragraph or two on copying the contents of one structure to another (identically dimensioned) one. However, it also states:
Do not copy structures containing string members; since only the string descriptor is copied, not the string itself, you are likely to confuse BASIC and may even crash it.
In one of my constantly changing programs, I inadvertently broke this rule by adding a string variable to a structure without thought of the consequences it would have on a previously written procedure. The proc created a local structure and used a global prototype to declare it and then copy its contents. This all worked fine until I introduced the string variable. This caused interesting(?) side affects. The way I have overcome this, currently, is to dimension the local structure using the prototype, and then copy each structure member. This seems inefficient. Is there a way to copy all but, perhaps, the string values in one (or, or at least, fewer) hits?

Also, (as the help is not entirely clear on this point), when passing a structure to a procedure, does the new string variables of the structure in the procedure also have the same string descriptors? Would changing the string value of the new structure cause issues, either with the original structure or BASIC itself? My experimentations suggest not, but I've tried experimenting with the above problem (the copying issue) in a separate program and found no errors, so I would not trust a simple test program with this. This is the basic coding I used to test the copying issue:

Code: Select all

      DIM Proto{str$, sgl%, sub1{sub1sgl1%, sub1sgl2%}, sub2{sub2sgl1%, sub2sgl2%}}
      Proto.str$ = "TEST"
      Proto.sgl% = 10
      Proto.sub1.sub1sgl1% = 20
      Proto.sub1.sub1sgl2% = 30
      Proto.sub2.sub2sgl1% = 40
      Proto.sub2.sub2sgl2% = 50
      PROCTEST
      PRINT Proto.str$
      END

      DEF PROCTEST
      LOCAL local{}
      DIM local{} = Proto{}
      local{} = Proto{}
      local.str$ = "NEW"
      ENDPROC
Matt
MattC
Posts: 114
Joined: Mon 16 Apr 2018, 06:17

Re: Copying Strutures

Post by MattC »

Sorry, forget the last paragraph before the coding. I think my brain was on strike, here. The help is quite clear about what happens to the structure when passing it to a procedure.