Search binary bytes in the Big arrays

General FreeBASIC programming questions.
grindstone
Posts: 726
Joined: May 05, 2015 5:35
Location: Germany

Re: Search binary bytes in the Big arrays

Postby grindstone » Jul 11, 2015 11:45

caseih wrote:Yes but unless you reset the descriptor back to zero pointer and zero length, when the string goes out of scope, the fB runtime will free the memory associated with your array, which almost certainly isn't what you want. Especially when FB will also try to free the array when it goes out of scope. This would end up in a double free() situation, which will cause a crash. That's the reason for having the object-oriented wrapper to make sure that cleanup is correct.

Yes, you're right. To get out of that number unscathed you need some additional lines:

Code: Select all

Dim Shared As UByte ub(&h6fffffff)

ub(&h6fffffff) = 65
ub(&h6ffffffE) = 65
ub(&h6ffffffD) = 65
ub(&h6ffffffC) = 65
ub(&h6ffffffB) = 65
ub(&h6ffffffA) = 65

Dim As String sz = "AAAAAA"

''make FB to think of the array as a string
Dim As String dummyString = "" 'create string descriptor
Dim As Any Ptr oldptr = @dummystring 'memorize old stringdescriptor pointer
*Cast(UInteger Ptr,@dummyString) = Cast(UInteger,@ub(0)) 'set the pointer
*(Cast(UInteger Ptr,@dummyString)+1) = &h6fffffff+1 'set the length value

Var t = Timer
? "Position="; InStr(dummyString,sz)
? "Time =";Timer -t

*Cast(UInteger Ptr,@dummyString) = Cast(UInteger,oldptr) 'restore pointer
*(Cast(UInteger Ptr,@dummyString)+1) = 0 'reset length

Sleep
fxm
Posts: 9700
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Search binary bytes in the Big arrays

Postby fxm » Jul 11, 2015 12:08

When a var-len string is empty (or becomes empty), its descriptor is always totally set to 0 (12 bytes set to 0).
So, memorize the pointer is useless.

Code: Select all

Dim Shared As UByte ub(&h6fffffff)

ub(&h6fffffff) = 65
ub(&h6ffffffE) = 65
ub(&h6ffffffD) = 65
ub(&h6ffffffC) = 65
ub(&h6ffffffB) = 65
ub(&h6ffffffA) = 65

Dim As String sz = "AAAAAA"

''make FB to think of the array as a string
Dim As String dummyString = "" 'create string descriptor
Cast(Any Ptr Ptr, @dummyString)[0] = @ub(Lbound(ub)) 'set the pointer
Cast(Uinteger Ptr, @dummyString)[1] = Ubound(ub)-Lbound(ub)+1 'set the length value

Var t = Timer
? "Position="; InStr(dummyString,sz)
? "Time =";Timer -t

Clear dummyString, 0, 8

Sleep
grindstone
Posts: 726
Joined: May 05, 2015 5:35
Location: Germany

Re: Search binary bytes in the Big arrays

Postby grindstone » Jul 12, 2015 11:31

If you use a dynamic array, the programme gets a strange behaviour: With the 'Clear' - statement it works, also with the restored pointer. But when the dummyString is set empty by >dummyString = ""<, the programme crashes the next time you access the array.

Code: Select all

ReDim Shared As UByte ub(&h6fffffff) '<<<<<<<<< CHANGED

ub(&h6fffffff) = 65
ub(&h6ffffffE) = 65
ub(&h6ffffffD) = 65
ub(&h6ffffffC) = 65
ub(&h6ffffffB) = 65
ub(&h6ffffffA) = 65

Dim As String sz = "AAAAAA"

''make FB to think of the array as a string
Dim As String dummyString = "" 'create string descriptor
Cast(Any Ptr Ptr, @dummyString)[0] = @ub(Lbound(ub)) 'set the pointer
Cast(Uinteger Ptr, @dummyString)[1] = Ubound(ub)-Lbound(ub)+1 'set the length value

Var t = Timer
? "Position="; InStr(dummyString,sz)
? "Time =";Timer -t

'Clear dummyString, 0, 8 '<<<<<<<<< WORKS
dummyString = "" '<<<<<<<<< CRASHES   (with a static array this works)

? ub(&h6ffffffA) '<<<<<<<<< ADDED   

Sleep
fxm
Posts: 9700
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Search binary bytes in the Big arrays

Postby fxm » Jul 12, 2015 12:46

This is it worth trying to explain everything before your above comment!

Return to “General”

Who is online

Users browsing this forum: No registered users and 6 guests