[Gambas-user] Arrays of Structs - how to - and curious debug message

Brian G brian at westwoodsvcs.com
Mon Jun 14 20:29:28 CEST 2021


I have been using structures for a while
Here is an example of how I handle them, I hope this helps

It uses memcpy() to get the real address of the element and to also copy elements

'Struct must be declared before the memcpy extern declare as it uses the structure
Public Struct Hello
  b As Integer
  c As Integer
  d As Long
  e As Float
End Struct

' We will use memcpy for two purposes , get the real address of the data, actually copy the data
Extern memcpy(dest As Hello, src As Hello, len As Integer) As Pointer In "libc:6"
Extern Arraymemcpy(dest As Pointer, src As Pointer, len As Integer) As Pointer In "libc:6" Exec "memcpy"

Public b As Struct Hello
Public c[20] As Struct Hello 

Public Sub Main()
  b.b = 43
  
  Dim a As Variant = b
  
  Dim e As Hello = c[1]  ' these just set pointers
  Dim f As Hello = c[2]
  Dim g As Hello = c[3]
  g.d = 500
  g.e = 3.67
  
  ' calc the struct length  subtrack address of second element from the first 
  Dim iStructLen As Integer = memcpy(c[1], c[1], 0) - memcpy(c[0], c[0], 0)
  Print f.d, f.e
  'Copy one entry to another
  memcpy(f, g, iStructLen)
  Print f.d, f.e
  a.b = 45
  
  Print a.b, b.b
  Display the actual address Of Each element And root
  Print memcpy(a, a, 0), memcpy(b, b, 0), arraymemcpy(VarPtr(c), VarPtr(c), 0), memcpy(e, e, 0), memcpy(f, f, 0), memcpy(g, g, 0)
  Print VarPtr(a), VarPtr(b), VarPtr(c)
  Print "Hello world"

End

"Failure is the key to success; 
 each mistake teaches us something"  .. Morihei Ueshiba
Brian G

----- On Jun 11, 2021, at 6:55 AM, Benoît Minisini g4mba5 at gmail.com wrote:

> Le 11/06/2021 à 02:34, John Anderson a écrit :
>> 
>> Yes, I did the description as per your wiki page.  I tried that first,
>> but it says it's not a real array.  But throwing all caution to wind I
>> Did 'MyBigArray2[200] as Struct MyStruct.'
>> 
>> Now Object.SizeOf(MyBigArray2) has a size of 40 bytes (I know it is just
>> an object).   Then I started to try different ways but I was just being
>> stupid.
>> 
>> C code needs pointer to that array PLUS size of the array it's getting
>> (ByRef MyBigArray2, tsize)... I guess I have to calc final array size in
>> bytes as Object.SizeOf(MyStruct) * MyBigArray2.Count?
>> 
>> How to copy array of struct contents Gambas way? (not pointers,
>> contents).  I'm sorry if its in docs, I don't see it yet.  I know I can
>> copy arrays of Gambas [] types with .Copy method, but I don't think that
>> works with array of struct? (If I try Copy with array of struct , it
>> comes back "Copy Unknown Method error).
>> 
>> Attached, compressed project (with known error) will emit errors upon
>> completion if a data breakpoint is set, but will show NO errors when
>> breakpoint NOT set.  That could be a real problem - is it supposed to
>> work that way?  If it might be a bug, I'll post on bug tracker when its
>> login works again.
>> 
>> -John
>> 
> 
> OK, I have analyzed the problem: the structure assignment is buggy, it
> does not handle all cases. Hopefully the memory stream does, so it
> should be a matter of code refactoring.
> 
> If you don't know what is a memory stream in Gambas, I suggest you look
> it, you can use it as a workaround for the current problem (Look at OPEN
> MEMORY in the documentation).
> 
> As for the breakpoint problem, let's see that later.
> 
> Regards,
> 
> --
> Benoît Minisini
> 
> ----[ http://gambaswiki.org/wiki/doc/netiquette ]----


More information about the User mailing list