Author Topic: Question about _MEM.SIZE of byte array  (Read 3945 times)

0 Members and 1 Guest are viewing this topic.

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Question about _MEM.SIZE of byte array
« on: March 31, 2020, 08:52:55 am »
I am trying to use _MEM.SIZE to provide the size of a byte array.
The example code below uses _MEM.SIZE to provide the size of a byte variable and the size of a byte array.

For the byte variable MEM SIZE gives 1, which is expected.
For the byte array, MEM SIZE also gives a value of 1, this is not expected.

Should it not be equal to 10, the actual size of the byte array?

I am using QB64 version 1.4, 32-bit on a Windows 10 64-bit computer.

Code: QB64: [Select]
  1. DIM barray(1 TO 10) AS _BYTE
  2. '-----
  3. PRINT "BYTE VARIABLE"
  4. m = _MEM(b)
  5.  
  6. PRINT "TYPE        = " + STR$(m.TYPE), "0x" + HEX$(m.TYPE)
  7. PRINT "OFFSET      = " + STR$(m.OFFSET)
  8. PRINT "ELEMENTSIZE = " + STR$(m.ELEMENTSIZE)
  9. PRINT "SIZE        = " + STR$(m.SIZE)
  10.  
  11. PRINT "BYTE ARRAY - 10 ELEMENTS"
  12. m = _MEM(barray(10))
  13.  
  14. PRINT "TYPE        = " + STR$(m.TYPE), "0x" + HEX$(m.TYPE)
  15. PRINT "OFFSET      = " + STR$(m.OFFSET)
  16. PRINT "ELEMENTSIZE = " + STR$(m.ELEMENTSIZE)
  17. PRINT "SIZE        = " + STR$(m.SIZE)

Offline luke

  • Administrator
  • Seasoned Forum Regular
  • Posts: 324
    • View Profile
Re: Question about _MEM.SIZE of byte array
« Reply #1 on: March 31, 2020, 08:59:58 am »
You intended to write m = _MEM(barray()) on line 15 to refer to the entire array, not just a single element of it.

The setting of bit 16 (Array) in the .TYPE field when you write m = _MEM(barray(10)) is probably a bug.
« Last Edit: March 31, 2020, 09:02:37 am by luke »

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: Question about _MEM.SIZE of byte array
« Reply #2 on: March 31, 2020, 09:12:02 am »
Hi luke,
On this QB64 wiki page for MEMELEMENT,
http://www.qb64.org/wiki/MEMELEMENT
this example program is given.
Code: QB64: [Select]
  1.  
  2.  
  3. m1 = _MEM(a(50)) 'function returns information about array up to specific element
  4. PRINT m1.OFFSET, m1.SIZE, m1.TYPE, m1.ELEMENTSIZE
  5.  
  6. m2 = _MEMELEMENT(a(50)) 'function returns information about the specific element
  7. PRINT m2.OFFSET, m2.SIZE, m2.TYPE, m2.ELEMENTSIZE
  8.  
  9.  

m1.SIZE prints out with a value of 51
Why, in this example, was the value of 51 output?

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: Question about _MEM.SIZE of byte array
« Reply #3 on: March 31, 2020, 09:35:32 am »
Here is a test program that initializes a memory block with a barray parameter that runs from empty, 1, 2, 3 and so on to 10.
The MEM.SIZE for each parameter value is printed out.

For the "empty parameter", barray(), the MEM.SIZE is 10, the whole array.
For barray(1), the MEM.SIZE is 10.
For barray(2), the MEM.SIZE is 9.
For barray(3), the MEM.SIZE is 8.
and so on, MEM.SIZE decreasing as the barray parameter increases, ending with, barray(10) giving a MEM.SIZE of 1.

This inverse relation between the barray parameter and the MEM.SIZE is interesting.
How does it come about?

Code: QB64: [Select]
  1.  
  2. DIM barray(1 TO 10) AS _BYTE
  3.  
  4.  
  5. PRINT "BYTE ARRAY - 10 ELEMENTS"
  6.  
  7. PRINT "barray index,  MEM.SIZE"
  8. PRINT "-----------------------"
  9.  
  10. m = _MEM(barray())
  11. PRINT "EmptyIndex =" + STR$(m.SIZE)
  12.  
  13. FOR i& = 1 TO 10
  14.     m = _MEM(barray(i&))
  15.     PRINT i&, m.SIZE
  16. NEXT i&
  17.  

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: Question about _MEM.SIZE of byte array
« Reply #4 on: March 31, 2020, 09:56:22 am »
I have the answer.
One has to read carefully the QB64 wiki documentation.
http://www.qb64.org/wiki/MEM

Quote
SIZE AS _OFFSET         'size of block remaining at offset(changes with position)

Offline EricE

  • Forum Regular
  • Posts: 114
    • View Profile
Re: Question about _MEM.SIZE of byte array
« Reply #5 on: March 31, 2020, 10:48:12 am »
This is why I got MEM.SIZE of 1 for the memory block barray(10).

In the memory block intialization,
m=_MEM(barray(10))
barray(10) is the starting position in the existing memory block barray(), which is 10 bytes in size.
But barray(10) is the last element in the memory bock, and so there is only 1 byte left for the memory block m.