Intialize an array of bytes, How ?

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
ndudman
Posts: 79
Joined: 25 December 2008, 14:00 PM

Intialize an array of bytes, How ?

Post by ndudman »

I thought I could do the following... but I'm missing something ?

I've searched and search... but can't turn up an answer yet, I'm sure its in the docs, but I keep going around and around.

Code: Select all

private const A 	as Byte = &H10
private const B 	as Byte = &H20
private const C 	as Byte = &H30

Sub Main()
	 Dim bytearray(1 to 3) as Byte = (A,B,C)
End Sub
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

You need Program Memory Data as illustrated below

Code: Select all

private const A    as Byte = &H10
private const B    as Byte = &H20
private const C    as Byte = &H30

Private READ_ONLY_BYTE_ARRAY as ByteVectorData ({ A, B, C})

Sub Main()
	Debug.Print READ_ONLY_BYTE_ARRAY(1)
End Sub
The syntax is inherited from BasicX and is a little archaic. I was thinking only the other day when using this construct that I would really like to write is something like

Code: Select all

Private Const READ_ONLY_BYTE_ARRAY() as Byte = ({ A, B, C})
That is, to have a way to declare arrays of constants and then the compiler figure out that it is read only data and can be stored in program memory.
Mike Perks
twesthoff
Posts: 247
Joined: 17 March 2006, 6:45 AM
Location: Fredericksburg, VA

Intialize an array of bytes, How ?

Post by twesthoff »

I totally agree the second way makes much better sense...
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

Note, however, that Neil ostensibly wanted a RAM-based array that is initialized. This can be accomplished by copying the ProgMem data to the RAM-based array.

Code: Select all

private const A    as Byte = &H10
private const B    as Byte = &H20
private const C    as Byte = &H30

Private READ_ONLY_BYTE_ARRAY as ByteVectorData ({ A, B, C})

Sub Main()
   Dim bytearray(1 to 3) as Byte
   Call GetProgMem(READ_ONLY_BYTE_ARRAY.DataAddress, bytearray, SizeOf(bytearray))
End Sub
- Don Kinzer
ndudman
Posts: 79
Joined: 25 December 2008, 14:00 PM

Post by ndudman »

Thanks for the replies... yes I did want a byte array I could modify during the program... and I guess didnt understand if I initilized with =((A,B,C))it then it would be created in program memory and thus read-only...

So I have to set each element individually or have the READ_ONLY version of the array to reset my working r/w copy of byte array.

Thanks
Neil

------------------------------------------------------------------------
p.s The problems I have in Zbasic usually turn out to be my own mistakes
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Post by dkinzer »

ndudman wrote:I guess didnt understand if I initilized with =((A,B,C))it then it would be created in program memory and thus read-only...
You may be confusing two different (but somewhat related) things. RAM-based variables can optionally have an initializer but only if they are scalar (i.e. not an array). Example:

Code: Select all

Dim a as Integer = 5
This is equivalent to:

Code: Select all

Dim as as Integer
a = 5
This is a typing shortcut only, the generated code is (nearly) identical.

In contrast, ZBasic does support initialized arrays of data in Program Memory. The original syntax for specifying the initialization data (invented by NetMedia for the BasicX devices) requires the name of a separate file to be specified, which file contains the data values. ZBasic supports this but it also supports specifying the data inline, e.g.

Code: Select all

Dim pmData as ByteVectorData ({ &H21, &H35,  &Hff })
This is more convenient for most applications because it keeps the data together with the code that uses it. The form shown above is considered read-only because the compiler won't allow you to assign values to the array. A different keyword ByteVectorDataRW can be used to allow the array to be read-write. This, too, is a carryover from BasicX.
ndudman wrote:So I have to set each element individually or have the READ_ONLY version of the array to reset my working r/w copy of byte array.
Yes, those are the choices. If you have more than just a few elements in the RAM array, it will be more efficient (time and code space) to define the Program Memory data array and copy it over the RAM-based array to initialize it.

Code: Select all

Dim pmData as ByteVectorData ({ 1, 2, 3})
Dim myData(1 to 3) as Byte
Call GetProgMem(pmData.DataAddress, myData, SizeOf(myData))
The code above will copy more data than it should if the Program Memory data item has fewer elements than the RAM array. This can be remedied thusly:

Code: Select all

Call GetProgMem(pmData.DataAddress, myData, Min(Sizeof(pmData), SizeOf(myData)))
- Don Kinzer
ndudman
Posts: 79
Joined: 25 December 2008, 14:00 PM

Post by ndudman »

Thanks for the clarification Don, Iḿ understanding better.

Regards
Neil
Post Reply