Page 1 of 1

Allow [] as size of arrays in structures

Posted: Sun Nov 13, 2016 1:17 pm
by infratec
Hi,

again I have a problem to implement a C dll in PB.
Problem is that there are many 'open' arrays in structures.

Example:

Code: Select all

struct libusb_interface_descriptor {
:
:
:
   /** Array of endpoint descriptors. This length of this array is determined
	 * by the bNumEndpoints field. */
	const struct libusb_endpoint_descriptor *endpoint;
:
If I try

Code: Select all

*endpoint.libusb_endpoint_descriptor[0]
I get no size for this entry.
If I remove the [0] I get the right structure size, but I can not access the arrays in an easy way.
If I use [1] I get also the correct size, but I can not access [2] for example.

Code: Select all

Structure Test
  test.u
EndStructure

Structure MainTest
  *test.Test[0]
EndStructure

Debug SizeOf(MainTest)
Returns 0 as size.

A [] would be the best option (for me :mrgreen: )

At the moment I have no idea how to solve such a problem without a lot of work around this.

Example:

Code: Select all

*endpoint = *conf_desc\usb_interface[i]\altsetting[j]\endpoint[k]
To do this without the arrays is a big overhead and not easy to read in the code.

Bernd

Re: Allow [] as size of arrays in structures

Posted: Sun Nov 13, 2016 5:09 pm
by skywalk
Why can't you use a dynamic array () and ReDim later?

Re: Allow [] as size of arrays in structures

Posted: Sun Nov 13, 2016 5:42 pm
by infratec
again I have a problem to implement a C dll in PB.
Implement in this case means: I wrote/writing a wrapper for a dll.

I have nothing to do with the arrays.
They are filled and created by the dll.
I only need access to them. :cry:

Bernd

Re: Allow [] as size of arrays in structures

Posted: Sun Nov 13, 2016 8:04 pm
by skywalk
Yes, I understood that. What prevents you accessing the dll arrays using a PB structure containing dynamically allocated arrays? Presumably, you know or can query the size of the dll array? Then redim your PB structure appropriately.

Re: Allow [] as size of arrays in structures

Posted: Sun Nov 13, 2016 10:18 pm
by infratec
Hi,

I tried something and it seams to work:

Code: Select all

Structure libusb_interface Align #PB_Structure_AlignC
	; Array of Interface descriptors. The length of this Array is determined
	; by the num_altsetting field.
	Array altsetting.libusb_interface_descriptor(1)

	; The number of alternate settings that belong To this Interface
	num_altsetting.l
EndStructure
I'm not 100% sure if (1) is now the dimension of the array like in a procesdure parameter.
But the size does not increase if I use (2).

It is not described in the help of Structures.

Bernd

Re: Allow [] as size of arrays in structures

Posted: Sun Nov 13, 2016 10:46 pm
by skywalk
You can set the array to 0 to 1 or whatever, but once you query num_altsetting, you should redim the array later to match.
My cheat sheet for arrays.

Code: Select all

;         To conform with C/C++ Structure format and allow direct API structure porting: 
;         Static arrays defined in structures are different than Dynamic arrays defined by Dim/ReDim.
;           a[2] allocates an array from 0 to 1.
;         But,
;           Dim a(2) allocates an array from 0 To 2.
;
;         To get size of a static array use SizeOf():
;         Structure myStruc
;           aX.b[128]
;           aY.b[5]
;           s.s{32}   ; fixed length strings are ok, but not variable length.
;         EndStructure
;         Define me.myStruc
;         Debug Sizeof(me\aX)
;         For nested Structures, use combinations of Sizeof() and Len()
;
;         Dynamic Multidimensional arrays in structures:
;         Structure myStruc
;           nPts.i
;           x$
;           Array f.d(0,0)
;         EndStructure
;         Global Dim me.myStruc(0)
;         Dim me(0)\f(10,10)   ;<-- Dim 1st, then can ReDim last index later
;
;         Swap cmd does NOT work on entire array. Only individual elements.
;         Use CopyArray() instead.
;
;         Multidimensional arrays in memory:
;         Row-major order:  PureBasic/C/C++/Python  Col steps contiguously in memory.
;         Col-major order:  VB6/Matlab/Fortran      Row steps contiguously in memory.

Re: Allow [] as size of arrays in structures

Posted: Mon Nov 14, 2016 6:04 am
by Demivec
infratec wrote:Code:
*endpoint.libusb_endpoint_descriptor[0]

I get no size for this entry.
Please forgive me if I misunderstand.

In the first case, *endpoint points to a dynamically size array whose element count equals the bNumEndpoints field. Secondly each structured element (libusb_endpoint_descriptor) of that array can have its own length (contained in each of their respective bLength fields.

To access any element you wouldn't be able to use a simple indexed method even with a convenient '[]' C notation.

Wouldn't the size have to be calculated by walking the variable length array and add up the size of it's variable sized elements? This would also be needed for simply accessing any of the data elements in that array.

Re: Allow [] as size of arrays in structures

Posted: Mon Nov 14, 2016 1:02 pm
by infratec
Hi,

The value 1 in

Code: Select all

Array altsetting.libusb_interface_descriptor(1)
represents the dimension of the array and not the member count.

This works without any errors and warnings.

Code: Select all

Structure Test
  Test.a
EndStructure


Structure Main
  members.i
  Array Test.Test(1)
EndStructure


Define *Buffer, *MainTest.Main, i.i
Dim HelpTest.Test(3)

HelpTest(0)\Test = 0
HelpTest(1)\Test = 1
HelpTest(2)\Test = 2
HelpTest(3)\Test = 3

*Buffer = AllocateMemory(20)
If *Buffer
  *MainTest = *Buffer
  *MainTest\members = 4
  
  For i = 0 To *MainTest\members - 1
    PokeI(*Buffer + OffsetOf(Main\Test) + i * SizeOf(Integer), @HelpTest(i))
  Next i
  
  For i = 0 To *MainTest\members - 1
    Debug *MainTest\Test(i)\Test
  Next i
  
  FreeMemory(*Buffer)
EndIf
@Demivec
The count of the arraymembers is given in a field of the structure.
So you can walk through.

Bernd

Re: Allow [] as size of arrays in structures

Posted: Mon Nov 14, 2016 2:30 pm
by Mistrel
Having support for simple C-style arrays would be great where we don't need fat array objects.

We cannot, for example, allocate memory for an array which contains an "Array" object.

You can do this though but it's more verbose. I don't see why we can't just use ".i" instead of ".Integer":

Code: Select all

Structure SomeStruct
  Field.Integer[10]
EndStructure

Define Var.SomeStruct

For i=0 To 9
  Debug Var\Field[i]\i
Next i

Re: Allow [] as size of arrays in structures

Posted: Wed Nov 16, 2016 9:56 pm
by HeX0R
infratec wrote:

Code: Select all

Structure Test
  test.u
EndStructure

Structure MainTest
  *test.Test[0]
EndStructure

Debug SizeOf(MainTest)
Returns 0 as size.

A [] would be the best option (for me :mrgreen: )

At the moment I have no idea how to solve such a problem without a lot of work around this.
[...]
If it is just the wrong sizeof() which hurts you, why not use structureunion with a dummy, like so:

Code: Select all

Structure Test
  test.u
EndStructure

Structure MainTest
	StructureUnion
		*test.Test[0]
		*dummy
	EndStructureUnion  
EndStructure

Debug SizeOf(MainTest)

Re: Allow [] as size of arrays in structures

Posted: Thu Nov 17, 2016 8:07 am
by infratec
Good idea.

But since the 'trick' with Array is working ...
If it stops to work in a future PB release, I hope I can remeber your idea.

Bernd