Code: Select all
struct mystruct
{
short items[1];
};
Code: Select all
struct mystruct
{
short items[1];
};
Code: Select all
Type mystruct
As Short Ptr items
End Type
Code: Select all
Type mystruct
As Short Ptr items
As Integer num
Declare Constructor(ByVal n As Integer = 1)
Declare Destructor()
End Type
Constructor mystruct(ByVal n As Integer = 1)
items = CAllocate(n, SizeOf(Short))
num = n
End Constructor
Destructor mystruct()
Deallocate(items)
num = 0
End Destructor
Code: Select all
Dim As mystruct m
m.items[0] = 12
Code: Select all
type mystruct
items(0) as short
end type
Code: Select all
type mystruct
items(0) as short
end type
' enough memory for mystruct plus 9 more SHORTs
dim p as mystruct ptr = allocate( sizeof( mystruct ) + sizeof( short ) * 9 )
' This will abort for indices > 0 when the -exx compiler option is used
for i as integer = 0 to 9
p->items(i) = i
next i
' This will abort for indices > 0 when the -exx compiler option is used
for i as integer = 0 to 9
print p->items(i)
next i
deallocate( p )
stylin - I think that you're spot on with the way that the C code that I'm working with is allocating code within the struct itself. I couldn't wrap my head around why the struct would only have one array element - it seemed redundant, but after seeing your example of allocating memory dynamically to expand the size of the struct, i can now see how the items would be accessed.However, some C code uses structs like the above in such a way as to have a variable-length array field, where the elements themselves are located in the same memory allocated for the struct itself