|
Post by wbtcpip on Nov 15, 2022 19:26:12 GMT 1
i have to pass to a c library an "Array of Pointers of Strings"
for exemple in c:
char *arr[] = { "Geek", "Geeks", "Geekfor" };
how i can create it in GB32?
the classic Dim x(3) as string create a classic array of strings not an array of Pointers
|
|
|
Post by (X) on Nov 16, 2022 14:27:32 GMT 1
What happens if you pass an array of string pointers?
Dim a$(3) a$(0) = "Geek0" a$(1) = "Geek1" a$(2) = "Geek2" Dim x(3) As Long x(V:a$(0)) x(V:a$(1)) x(V:a$(2))
and pass *x(), the pointer to the x() array...
|
|
|
Post by wbtcpip on Nov 16, 2022 14:42:11 GMT 1
it works like this.
dim comando(10) As String, comandov(10) As Int
comando(0) = "loadfile"
comandov(0) = V:comando(0)
comando(1) = "av://dshow:video=Full HD 1080P PC Camera" comandov(1) = V:comando(1)
and passing v:comandov(0) to the c library
i was looking for a less coding solution like in c
|
|
|
Post by Roger Cabo on Nov 16, 2022 16:04:44 GMT 1
Not really sure what you like to do.
This is a unsafe behavior to use Pointers to Strings in GB32 and can cause to fatal crash or data corruption. I cannot recommended this behavior. Because through a GB Memory Collection update, it could be possible that old pointers locate silently to undefined memory.
There is only one use case for this. If you have a static memory allocation of all strings in a row. Then you should take care that these memory block is write protected, unless you update all pointers based on /0 each time you access them.
|
|
|
Post by dragonjim on Nov 24, 2022 15:30:35 GMT 1
To make it a bit less clumsy, you could always use functions like in the example below.
Dim s$(), pt%() PtrArrayCreate(s$(), "Entry 1", "Entry 2", "Entry 3", "Entry 4") PtrArrayAdd(s$(), "Entry 5", "Entry 6", "Entry 7", "") PtrArrayPointers(s$(), pt%()) Local n% For n% = 0 To UBound(s$()) Debug pt%(n%),s$(n%) Next n% Debug.Show
Procedure PtrArrayCreate(ByRef a$(), ParamArray p()) Local n% ReDim a$(0 To UBound(p())) For n% = 0 To UBound(p()) a$(n%) = p(n%) Next n% EndProcedure Procedure PtrArrayAdd(ByRef a$(), ParamArray p()) Local ct%, lb% = UBound(a$()) + 1, n%, ub% = UBound(a$()) + UBound(p()) + 1 ReDim a$(0 To ub%) For n% = lb% To ub% a$(n%) = p(ct%) : Inc ct% Next n% EndProcedure Procedure PtrArrayPointers(ByRef a$(), ByRef p%()) Local n% ReDim p%(LBound(a$()) To UBound(a$())) For n% = LBound(a$()) To UBound(a$()) p%(n%) = V:a$(n%) Next n% EndProcedure
I have purposely kept the pointer creation separate as, sometimes, changing the content of an array element can change the pointer, so a pointer array created upon the creation of the string array may no longer be valid.
|
|