Compiler Functions


Syntax

Size = SizeOf(Type)
Description
SizeOf can be used to find out the size of any complex Structure (it does not work on the simple built-in types such as word and float), Interface or even variables. This can be useful in many areas such as calculating memory requirements for operations, using API commands, etc.

Note: In unicode mode one character uses 2 bytes. In Ascii mode one character uses 1 byte. Sizeof(Character) allows to return the size (in bytes), which is taken by one character according to the active mode.

Example:

  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 
Note: if a variable and a structure have the same name, the structure will have the priority over the variable.

Syntax
Index = OffsetOf(Structure\Field)
Index = OffsetOf(Interface\Function())
Description
OffsetOf can be used to find out the index of a Structure field or the index of an Interface function. When used with an Interface, the function index is the memory offset, so it will be IndexOfTheFunction*4.

Example:

  Structure Person
    Name.s
    ForName.s 
    Age.w 
  EndStructure
  
  Debug OffsetOf(Person\Age) ; will be 8 as a string is 4 byte in memory
  
  
  Interface ITest
    Create()
    Destroy(Flags) 
  EndInterface
  
  Debug OffsetOf(ITest\Destroy()) ; will be 4

Syntax
Result = Subsystem(<constant string expression>)
Description
Subsystem can be used to find out if a subsystem is in use for the program being compiled. The name of the subsystem is not case sensitive.

Example:

  CompilerIf Subsystem("OpenGL")
    Debug "Compiling with the OpenGL subsystem"
  CompilerEndIf

Syntax
Result = Defined(Name, Type)
Description
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 linkedlist or a map).

The 'Type' parameter can be one of the following values:
  #PB_Constant
  #PB_Variable
  #PB_Array
  #PB_LinkedList
  #PB_Map
  #PB_Structure
  #PB_Interface
  #PB_Procedure
  #PB_Function
  #PB_OSFunction

Example:

  #PureConstant = 10
  
  CompilerIf Defined(PureConstant, #PB_Constant)
    Debug "Constant 'PureConstant' is already declared"
  CompilerEndIf
  
  Test = 25
  
  CompilerIf Defined(Test, #PB_Variable)
    Debug "Variable 'Test' is already declared"
  CompilerEndIf

Syntax
InitializeStructure(*Pointer, Structure)
Description
InitializeStructure initialize the specified structured memory area. This is useful when the structure contains dynamic objects like array", list or map which have to be allocated internally by PureBasic. 'Structure' is the name of the structure which should be used to perform the initialization. There is no internal check to ensures than the structure match the memory area. This function is for advanced users only and should be used with care.

Example:

  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

Syntax
CopyStructure(*Source, *Destination, Structure)
Description
CopyStructure copy the memory of a structured memory area to another. This is useful when dealing with dynamic allocations, trough pointers. Every field will be duplicated, even array, list, and map. There is no internal check to ensures than the structure match the two memory area. This function is for advanced users only and should be used with care.

Example:

  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")

Syntax
ClearStructure(*Pointer, Structure)
Description
ClearStructure free the memory of a structured memory area. This is useful when the structure contains strings which have been allocated internally by PureBasic. 'Structure' is the name of the structure which should be used to perform the clearing. There is no internal check to ensures than the structure match the memory area. This function is for advanced users only and should be used with care.

Example:

  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 resetted to zero.
  ;
  Debug Student\Name$
  Debug Student\LastName$
  Debug Student\Age