As a part of a reflection/serialization library I've been writing, I've found it very nice to be able to make a copy of a static freebasic array's array descriptor and use that while simply holding an "any ptr" to the start of the array. The code works great, but I'm rather concerned with the hack I used to get there. Consider the following:
Code: Select all
#include "crt/mem.bi"
#include "fbc-int/array.bi"
type thing
dim x(9) as string
end type
dim savedArrayDesc as FBC.FBARRAY
memcpy( _
@savedArrayDesc, _
FBC.ArrayConstDescriptorPtr(cast(thing ptr, 0)->x()), _ 'How bad is this? Undefined? Ok but hacky?
sizeof(savedArrayDesc))
'Never use these members when doing this hack, they'll be garbage
savedArrayDesc.index_ptr = 0
savedArrayDesc.base_ptr = 0
'Demonstrate that things worked just fine
print "dims: ";savedArrayDesc.dimensions
print "lbound: ";savedArrayDesc.dimTb(0).lbound
print "ubound: ";savedArrayDesc.dimTb(0).ubound
sleep
Ultimately, FBC.ArrayConstDescriptorPtr() is calling the rtlib function fb_ArrayGetDesc which looks like this:
Code: Select all
FBCALL FBARRAY *fb_ArrayGetDesc( FBARRAY *array )
{
return array;
}
My guess is that: The compiler is treating the subsequent parenthesis in a special way that causes it to generate a temporary array descriptor and that array descriptor is being passed to the function. It's then destroyed when the function returns and the value goes out of scope. The dereference is ignored in the same way it would be in, say, a typeof() expression.
Simply put this code works and I want to know if it actually should be working.