Error FB1. 8 -64 when counting bytes in sizeof (NewType)?

New to FreeBASIC? Post your questions here.
Post Reply
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Error FB1. 8 -64 when counting bytes in sizeof (NewType)?

Post by PavelUT »

There is a program FB1. 08-64, OS Windows 7-64
CLS
Type NewType
Dim As Integer VarInt
Dim As Single VarSin
Dim As Double Var3
End Type

Dim SupVar As NewType

SupVar.VarInt=150 '
SupVar.VarSin=1200.756 '
SupVar.Var3=1500.2

Print "SupVar.VarInt="; SupVar.VarInt
Print "SupVar.VarSin="; SupVar.VarSin
Print "SupVar.Var3="; SupVar.Var3 '
Print

Print "sizeof(SupVar.VarInt)=";sizeof(SupVar.VarInt)
print "sizeof(SupVar.VarSin)=";Sizeof(SupVar.VarSin)
print "sizeof(SupVar.Var3)=";sizeof(SupVar.Var3)
Print "sizeof(NewType)="; sizeof(NewType) '
sleep
End
The result of the program:
sizeof(SupVar.VarInt)= 8
sizeof(SupVar.VarSin)= 4
sizeof(SupVar.Var3)= 8
sizeof(NewType)= 24

Based on the result, I have a question:
8+4+8=20.

And the total size of the structure is specified in
sizeof(NewType)= 24 bytes.
What is the correct result?
Where is the error?
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Error FB1. 8 -64 when counting bytes in sizeof (NewType)?

Post by fxm »

This is due to automatic alignment in the Type structure to optimize access time.
See the Structure packing/field alignment documentation page.

You can use the 'Field' specifier to force the field alignment decreasing.
See the FIELD documentation page.
badidea
Posts: 2591
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: Error FB1. 8 -64 when counting bytes in sizeof (NewType)?

Post by badidea »

Also, if the size is important, don't use integer (use longint if you want a 8-byte integer).
output of your example code (without the field alignment specified) with 32-bit fbc:

Code: Select all

sizeof(SupVar.VarInt)= 4
sizeof(SupVar.VarSin)= 4
sizeof(SupVar.Var3)= 8
sizeof(NewType)= 16
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Re: Error FB1. 8 -64 when counting bytes in sizeof (NewType)?

Post by PavelUT »

Thanks. And I used to think that in the 64-bit version, in addition to Integer, single also doubles. And double. Agree it would be logical
PavelUT
Posts: 78
Joined: Jun 14, 2021 14:42

Re: Error FB1. 8 -64 when counting bytes in sizeof (NewType)?

Post by PavelUT »

Dim As String myString4= "This is the string"
Print Len (myString4) ' returns 18, the size of the string
Print SizeOf(myString4) ' returns 12 ????? What 12 bytes does SizeOf return?
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Error FB1. 8 -64 when counting bytes in sizeof (NewType)?

Post by MrSwiss »

PavelUT wrote:Print SizeOf(myString4) ' returns 12 ????? What 12 bytes does SizeOf return?
SizeOf() returns the size of the 'string-header' since, a FB-String is in fact a UDT (user defined type).

Btw: it also tells us, that you've used the 32-bit version of FBC (in 64-bit, the size doubles).
Post Reply