sizeof

BlitzMax Forums/BlitzMax Programming/sizeof

Nigel Brown(Posted 2006) [#1]
why does this return 4?

type that
Field n1:byte[32]
end type

print sizeof(that)

should be 32, and this return 16?

type that
Field n1:int
Field n2:long
end type

should be 12!


Nigel Brown(Posted 2006) [#2]
mmm... seems to be some rounding going on here. a Short is 2 bytes and an Int is 4 however if you put them in the same type you get a length of 8. This makes sizeof() useless eg:

my:that = new that
memclear( byte ptr(my), sizeOf(that) )


Azathoth(Posted 2006) [#3]
I thought arrays are pointers to array even in types.
Isn't the second 16 because of padding?


H&K(Posted 2006) [#4]
Yep, the ptr is size 4, and in the second, put two shorts and an int and its size 8. (cos of alignment or somtehing)

Just cos you dont know how to use it yet, doesnt make it usless, but it probably should explaiun it better in the docs, cos, as you said ,its not obvious why your getting what you are getting, until you know why

sizeof (that.n1)


Nigel Brown(Posted 2006) [#5]
Docs? What Docs? I am trying to create a complex type that I can pass to an API call. It expects this to be in a known format or it is not able to fill the fields correctly.

The only way for me to do this in MAX is:

1. Create a block of memory.
2. Fill that block with the content of the type, 126 bytes.
3. Call API function with a pointer to the defined memory.
4. Copy data from the block of memory back to the type.
5 Free the block of memory.

In C:

1. Call the API function with a pointer to the structure.