How to convert this C code?

General FreeBASIC programming questions.
McLovin
Posts: 82
Joined: Oct 21, 2008 1:15
Contact:

How to convert this C code?

Postby McLovin » Jan 22, 2010 15:05

i have been converting some C code and i am not 100% sure how to handle code like the following:

Code: Select all

struct mystruct
{
    short items[1];
};


How is the "items[1]" converted to FB? Is it an array of 1 or 2 short values?
mathwizard44
Posts: 72
Joined: Oct 13, 2006 6:11
Location: Latte Plantation, Guam
Contact:

Postby mathwizard44 » Jan 22, 2010 16:02

First, for your second question, "short items[1]" is an array called items with 1 short element, which you would access as "items[0]".

I looked at my ANSI C book (which who knows how old it is ^_^) and I think the strict FreeBASIC translation of your code is

Code: Select all

Type mystruct
    As Short Ptr items
End Type


As of FreeBASIC 0.20b, you can't directly put a dynamic array inside a user-defined type. So you would have to mind reading, writing, resizing, and allocating the memory attached to the pointer. I'm assuming, of course, that you wanted the items array to be able to grow (as the word "items" is plural).

With all the custom code the type would look something like this:

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



You would write functions for resizing the array and deleting elements and other functions for that matter. The upside is referring to the array in your module-level code is pretty easy to understand. You could write

Code: Select all

Dim As mystruct m
m.items[0] = 12


which would put the value 12 in position 0 of the items pointer/array.

For more information about this, see http://www.freebasic.net/wiki/wikka.php ... aArrayType for using pointers to give array functionality inside UDTs. I have had to read this page myself many a time. ^_^

Hope this helps.
Last edited by mathwizard44 on Jan 22, 2010 16:51, edited 1 time in total.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Postby stylin » Jan 22, 2010 16:27

McLovin,

Code: Select all

type mystruct
    items(0) as short
end type


so `sizeof( mystruct )` is 2. 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, like the following code:

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 )


Hope this helps.
McLovin
Posts: 82
Joined: Oct 21, 2008 1:15
Contact:

Postby McLovin » Jan 22, 2010 18:14

Thansk mathwizard44 and stylin, really appreciate the answers.

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

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.
cha0s
Site Admin
Posts: 5317
Joined: May 27, 2005 6:42
Location: Illinois
Contact:

Postby cha0s » Jan 23, 2010 7:18

Do note that if you compile the code with -exx error checking, it will fail... because the compiler thinks you're accessing the array out-of-bounds (technically you are, you just know better than the machine, in this case)

I know stylin's code mentioned it, just wanted to drill it in...

Return to “General”

Who is online

Users browsing this forum: dbickin and 5 guests