Allow [] as size of arrays in structures

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Allow [] as size of arrays in structures

Post 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
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Allow [] as size of arrays in structures

Post by skywalk »

Why can't you use a dynamic array () and ReDim later?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Allow [] as size of arrays in structures

Post 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
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Allow [] as size of arrays in structures

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Allow [] as size of arrays in structures

Post 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
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Allow [] as size of arrays in structures

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Allow [] as size of arrays in structures

Post 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.
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Allow [] as size of arrays in structures

Post 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
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Re: Allow [] as size of arrays in structures

Post 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
User avatar
HeX0R
Addict
Addict
Posts: 1189
Joined: Mon Sep 20, 2004 7:12 am
Location: Hell

Re: Allow [] as size of arrays in structures

Post 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)
infratec
Always Here
Always Here
Posts: 7588
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Allow [] as size of arrays in structures

Post 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
Post Reply