DescriptionSize = SizeOf(Type)
SizeOf can be used to find the size of any complex Structure, built-in type (word, float, etc.), Interface or even ReferenceLink "variables" "variables" (Structures with same name as variable take precedence). This can be useful in many areas such as calculating memory requirements for operations, using API commands, etc.
As a compiler function, SizeOf(x) is assigned to a constant and does not require assignment to another variable if inside a loop or often called procedure.
As a compile time function SizeOf doesn't work with runtime Array, List or Map. ArraySize(), ListSize() or MapSize() can be used instead.
Note: A Character (.c) variable is unicode and uses 2 bytes. An Ascii variable (.a) is Ascii and uses 1 byte.
char.c='!' Debug SizeOf(char); display 2 ascii.a='!' Debug SizeOf(ascii); display 1
Structure Person Name.s ForName.s Age.w EndStructure Debug "The size of my friend is "+Str(Sizeof(Person))+" bytes" ; will be 10 (4+4+2) John.Person\Name = "John" Debug SizeOf(John) ; will be also 10
DescriptionIndex = OffsetOf(Structure\Field) Index = OffsetOf(Interface\Function())
OffsetOf can be used to find out the address offset of a Structure field or the address offset of an Interface function. When used with an Interface, the function index is the memory offset, so it will be IndexOfTheFunction*SizeOf(Integer).
Structure Person Name.s ForName.s Age.w EndStructure Debug OffsetOf(Person\Age) ; will be 8 as a string is 4 byte in memory ; (16 with the 64-bit compiler, as a string is 8 bytes there) Interface ITest Create() Destroy(Flags) EndInterface Debug OffsetOf(ITest\Destroy()) ; will be 4
DescriptionType = TypeOf(Object)
TypeOf can be used to find out the type of a variable, or a structure field. The type can be one of the following values:#PB_Byte #PB_Word #PB_Long #PB_String #PB_Structure #PB_Float #PB_Character #PB_Double #PB_Quad #PB_List #PB_Array #PB_Integer #PB_Map #PB_Ascii #PB_Unicode #PB_Interface
Structure Person Name.s ForName.s Age.w EndStructure If TypeOf(Person\Age) = #PB_Word Debug "Age is a 'Word'" EndIf Surface.f If TypeOf(Surface) = #PB_Float Debug "Surface is a 'Float'" EndIf
DescriptionResult = Subsystem(<constant string expression>)
Subsystem can be used to find out if a subsystem is in use for the program being compiled. The name of the subsystem is case sensitive.
CompilerIf Subsystem("OpenGL") Debug "Compiling with the OpenGL subsystem" CompilerEndIf
DescriptionResult = Defined(Name, Type)
Defined checks if a particular object of a code source like structure, interface, variables etc. is already defined or not. The 'Name' parameter has to be specified without any extra decoration (ie: without the '#' for a constant, without '()' for an array, a list or a map).
The 'Type' parameter can be one of the following values:#PB_Constant #PB_Variable #PB_Array #PB_List #PB_Map #PB_Structure #PB_Interface #PB_Procedure #PB_Function #PB_OSFunction #PB_Label #PB_Prototype #PB_Module #PB_Enumeration
#PureConstant = 10 CompilerIf Defined(PureConstant, #PB_Constant) Debug "Constant 'PureConstant' is declared" CompilerEndIf Test = 25 CompilerIf Defined(Test, #PB_Variable) Debug "Variable 'Test' is declared" CompilerEndIf
InitializeStructure initialize the specified structured memory area. It initializes structure members of type Array, List and Map, other members are not affected (.s, .l, .i etc). 'Structure' is the name of the structure which should be used to perform the initialization. There is no internal check to ensures the structure match the memory area. Warning: multiple calls to InitializeStructure create a memory leak because the old members are not freed (ClearStructure has to be called before calling InitializeStructure once more). This function is for advanced users and should be used with care. To allocate dynamic structure, use AllocateStructure()().
Structure People Name$ Age.l List Friends.s() EndStructure *Student.People = AllocateMemory(SizeOf(People)) InitializeStructure(*Student, People) ; Now the list is ready to use ; AddElement(*Student\Friends()) *Student\Friends() = "John" AddElement(*Student\Friends()) *Student\Friends() = "Yann" ; Print out the list content ; ForEach *Student\Friends() Debug *Student\Friends() Next
DescriptionCopyStructure(*Source, *Destination, Structure)
CopyStructure copy the memory of a structured memory area to another. This is useful when dealing with dynamic allocations, through pointers. Every fields will be duplicated, even array, list, and map. The destination structure will be automatically cleared before doing the copy, it's not needed to call ClearStructure before CopyStructure. Warning: the destination should be a valid structure memory area, or a cleared memory area. If the memory area is not cleared, it could crash, as random values will be used by the clear routine. There is no internal check to ensures that the structure match the two memory area. This function is for advanced users and should be used with care.
Structure People Name$ LastName$ Map Friends$() Age.l EndStructure Student.People\Name$ = "Paul" Student\LastName$ = "Morito" Student\Friends$("Tom") = "Jones" Student\Friends$("Jim") = "Doe" CopyStructure(@Student, @StudentCopy.People, People) Debug StudentCopy\Name$ Debug StudentCopy\LastName$ Debug StudentCopy\Friends$("Tom") Debug StudentCopy\Friends$("Jim")
ClearStructure clears a structured memory area. This is useful when the structure contains strings, array, list or map which have been allocated internally by PureBasic. 'Structure' is the name of the structure which should be used to perform the clearing. All the fields will be set to zero, even native type like long, integer etc. There is no internal check to ensures the structure match the memory area. This function is for advanced users and should be used with care.
Structure People Name$ LastName$ Age.l EndStructure Student.People\Name$ = "Paul" Student\LastName$ = "Morito" Student\Age = 10 ClearStructure(@Student, People) ; Will print empty strings as the whole structure has been cleared. All other fields have been reset to zero. ; Debug Student\Name$ Debug Student\LastName$ Debug Student\Age
ResetStructure clears a structured memory area and initialize it to be ready to use. This is useful when the structure contains strings, array, list or map which have been allocated internally by PureBasic. 'Structure' is the name of the structure which should be used to perform the clearing. This function is for advanced users and should be used with care.
Structure Person Map Friends.s() EndStructure Henry.Person\Friends("1") = "Paul" ResetStructure(@Henry, Person) ; Will print an empty string as the whole structure has been reseted. The map is still usable but empty. ; Debug Henry\Friends("1")
Bool can be used to evaluate a boolean expression outside of regular conditional operator like If, While, Until etc. If the boolean expression is true, it will return #True, otherwise it will return #False.
Hello$ = "Hello" World$ = "World" Debug Bool(Hello$ = "Hello") ; will print 1 Debug Bool(Hello$ <> "Hello" Or World$ = "World") ; will print 1