Author Topic: Using _MEM to get/put arrays into UDT strings  (Read 3907 times)

0 Members and 1 Guest are viewing this topic.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Using _MEM to get/put arrays into UDT strings
« on: December 02, 2019, 09:29:41 am »
Every so often, someone will ask, "Why can't we use arrays with User Defined Types?"  The answer to that, is of course, "No one coded BASIC to work like that, and the functionality hasn't been expanded for it yet."

Which, leaves us with trying to find a workaround for easy transfer of data via GET/PUT statements...

Here's the easiest way I've found to do this type of thing, and I thought I'd shared for those who might be interested:

Code: QB64: [Select]
  1. DIM b(0 TO 7, 0 TO 7) AS _UNSIGNED _BYTE
  2.  
  3. TYPE Whatever
  4.     x AS INTEGER
  5.     y AS INTEGER
  6.     s AS STRING * 64 'to hold b()
  7.  
  8. DIM foo AS Whatever
  9.  
  10. DIM m AS _MEM, m1 AS _MEM
  11. m = _MEM(b()) 'a memblock to point at our array
  12. m1 = _MEM(foo.s) 'and one to point at our user type string to hold the array
  13.  
  14.  
  15. FOR x = 0 TO 7
  16.     FOR y = 0 TO 7
  17.         b(x, y) = count
  18.         PRINT b(x, y),
  19.         count = count + 1
  20.     NEXT
  21. SLEEP 'So we can see what b() contains.
  22.  
  23.  
  24. _MEMCOPY m, m.OFFSET, m.SIZE TO m1, m1.OFFSET 'Copy our array to our User Type Variable
  25.  
  26.  
  27. CLEAR b()
  28. FOR x = 0 TO 7
  29.     FOR y = 0 TO 7
  30.         PRINT b(x, y),
  31.     NEXT
  32. SLEEP 'So we can see that b() is now blank.
  33.  
  34. m = _MEM(b()) 'point our mem blocks back, since CLEAR erased them...
  35. m1 = _MEM(foo.s)
  36.  
  37. _MEMCOPY m1, m1.OFFSET, m1.SIZE TO m, m.OFFSET 'Copy our User Type Variable back to our array
  38. FOR x = 0 TO 7
  39.     FOR y = 0 TO 7
  40.         b(x, y) = count
  41.         PRINT b(x, y),
  42.         count = count + 1
  43.     NEXT
  44.  
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: Using _MEM to get/put arrays into UDT strings
« Reply #1 on: December 02, 2019, 09:41:05 am »
Code: QB64: [Select]
  1. CLEAR b()
??? Since when this is possible ???

Wiki doesn't mention this kind of usage.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: Using _MEM to get/put arrays into UDT strings
« Reply #2 on: December 02, 2019, 10:04:34 am »
Code: QB64: [Select]
  1. CLEAR b()
??? Since when this is possible ???

Wiki doesn't mention this kind of usage.

Actually that's a classic "Steve Oopsie".  ERASE b() would be more appropriate to just erase the arrays, but CLEAR, in this case, works well enough here  (though the b() is ignored).

The ERASE statement is used to clear all data from an array
The CLEAR statement clears all variable and array element values in a program.


 
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!