Page 1 of 1

Array with no/open size in structures

Posted: Sat May 22, 2010 9:32 pm
by sverson
It would be great to have a special kind of 'sizeless' arrays to address (imported) arrays of unknown*/variable size.
*unknown on structure definition time

For example MYSQL_ROW:
The C definition is »typedef char **MYSQL_ROW; /* return data as array of strings */«

The PB structure could look like this:

Code: Select all

Structure MYSQL_ROW
  Array field.s()
EndStructure
Structure MYSQL_LENGTHS
  Array length.l()
EndStructure
- OR -

Code: Select all

Structure MYSQL_ROW
  OpenArray field.s
EndStructure
Structure MYSQL_LENGTHS
  OpenArray length.l
EndStructure
In this case PureBasic will not take control of the array. It just makes it possible to access the MYSQL_ROW data.
mySQL still controls, fills and frees the memory - AND - off course knows the size of the array. (FieldCount = mysql_num_fields(*mySQL_Result))

This would be possible:

Code: Select all

*myRow.MYSQL_ROW = mysql_fetch_row(*mySQL_Result)
debug *myRow\field(3)
Direct access to the field string...

Or if there is no zero at the end:

Code: Select all

*myRow.MYSQL_ROW = mysql_fetch_row(*mySQL_Result)
*myLengths.MYSQL_LENGTHS  = mysql_fetch_lengths(*mySQL_Result)
debug PeekS(*myRow\field(3), *myLengths\length(3))
instead of:

Code: Select all

*mySQL_Row = mysql_fetch_row(*mySQL_Result)
*mySQL_Lengths = mysql_fetch_lengths(*mySQL_Result)
 mySQL_FieldLength.l = PeekL(*mySQL_Lengths+4*3)
*mySQL_FieldPtr = PeekL(*mySQL_Row+4*3)
debug PeekS(*mySQL_FieldPtr, mySQL_FieldLength)
This one works:

Code: Select all

Structure MYSQL_ROW
  field.s[255]
EndStructure
..but what happens if there are more than 255 fields?

I hope I was clear enough.
Any chance to implement this? - Would be great!
Thanks - sverson

Re: Array with no/open size in structures

Posted: Sat May 22, 2010 9:42 pm
by Trond
Try this:

Code: Select all

Structure MYSQL_ROW
  *field.String[0]
EndStructure
I would strongly advice against using the type .s as this is strings managed automatically by PB. If your strings are managed by MySQL you could end up with some big trouble. Better make it an array of pointers to the structure String. Since they are pointers, PB won't reallocate or free the strings implicitly. Access the strings by reading variable\field[index]\s. Note: You can't assign anything to this string, as PB will try to free the original string, which should be managed by MySQL.

Re: Array with no/open size in structures

Posted: Sat May 22, 2010 10:41 pm
by sverson
Trond wrote:Try this:

Code: Select all

Structure MYSQL_ROW
  *field.String[0]
EndStructure
@Trond
I'm sure I tried this before - no success!
Now it works - on any string array size - strange :?

May be there was a little bug in my code :oops:

Thanks for making me try it again - it's exactly what I've been looking for.

EDIT (add):
It even works this way as I need to call peeks(*myRow\field[x]). [*myRow.MYSQL_ROW]

Code: Select all

Structure MYSQL_ROW
  *field[0]
EndStructure