OFFSET (function): Difference between revisions

From QB64 Phoenix Edition Wiki
Jump to navigation Jump to search
(Created page with "{{DISPLAYTITLE:_OFFSET (function)}} The _OFFSET function returns the memory offset of/within a given variable. {{PageSyntax}} : {{Parameter|offset%&}} = _OFFSET({{Parameter|variable}}) {{PageDescription}} * The {{Parameter|variable}} parameter can be any type of numerical or string variable name. * API LIBRARY parameter or type names may include '''lp, ptr''' or '''p''' which designates them as a pointer type. * _OFFSET...")
 
No edit summary
Line 12: Line 12:
* _OFFSET function return values should be stored in [[_OFFSET]] type variables. As no other variable type is 'elastic' like [[_OFFSET]], there can be no guarantee that any other variable type can hold the value of an _OFFSET.
* _OFFSET function return values should be stored in [[_OFFSET]] type variables. As no other variable type is 'elastic' like [[_OFFSET]], there can be no guarantee that any other variable type can hold the value of an _OFFSET.
* Returns the memory offset of variables, user-defined-types & elements, arrays & indices and the base offset of [[STRING]]s.
* Returns the memory offset of variables, user-defined-types & elements, arrays & indices and the base offset of [[STRING]]s.
* Offset values are currently only useful when used in conjunction with [[_MEM]] or [[DECLARE LIBRARY]] procedures.  
* Offset values are currently only useful when used in conjunction with [[_MEM]] or [[DECLARE LIBRARY]] procedures.
* OFFSET values are used as a part of the [[_MEM]] variable [[type]] in QB64; {{Parameter|variable}}.OFFSET returns or sets the current position in memory.
* OFFSET values are used as a part of the [[_MEM]] variable [[type]] in QB64; {{Parameter|variable}}.OFFSET returns or sets the current position in memory.
* '''Warning:''' QB64 variable length strings can move about in memory at any time. If you get the _OFFSET of a variable length sting on one line and use it on the next it may not be there anymore.''' To be safe, move variable length strings into fixed length strings first.'''
* '''Warning:''' QB64 variable length strings can move about in memory at any time. If you get the _OFFSET of a variable length sting on one line and use it on the next it may not be there anymore.''' To be safe, move variable length strings into fixed length strings first.'''
Line 19: Line 19:
{{PageExamples}}
{{PageExamples}}
''Example:'' Using memcpy with the _OFFSET function values as parameters.
''Example:'' Using memcpy with the _OFFSET function values as parameters.
{{CodeStart}} '' ''
{{CodeStart}}
{{Cl|DECLARE DYNAMIC LIBRARY|DECLARE CUSTOMTYPE LIBRARY}}
{{Cl|DECLARE DYNAMIC LIBRARY|DECLARE CUSTOMTYPE LIBRARY}}
     {{Cl|SUB}} memcpy ({{Cl|BYVAL}} dest {{Cl|AS}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} source {{Cl|AS}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} bytes {{Cl|AS}} {{Cl|LONG}})
     {{Cl|SUB}} memcpy ({{Cl|BYVAL}} dest {{Cl|AS}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} source {{Cl|AS}} {{Cl|_OFFSET}}, {{Cl|BYVAL}} bytes {{Cl|AS}} {{Cl|LONG}})
Line 28: Line 28:


memcpy {{Cl|_OFFSET (function)|_OFFSET}}(a$) + 5, {{Cl|_OFFSET (function)|_OFFSET}}(b$) + 5, 5
memcpy {{Cl|_OFFSET (function)|_OFFSET}}(a$) + 5, {{Cl|_OFFSET (function)|_OFFSET}}(b$) + 5, 5
{{Cl|PRINT}} a$ '' ''
{{Cl|PRINT}} a$
{{CodeEnd}}
{{CodeEnd}}
{{OutputStart}}12345FGHIJ
{{OutputStart}}12345FGHIJ

Revision as of 02:11, 23 January 2023

The _OFFSET function returns the memory offset of/within a given variable.


Syntax

offset%& = _OFFSET(variable)


Description

  • The variable parameter can be any type of numerical or string variable name.
  • API LIBRARY parameter or type names may include lp, ptr or p which designates them as a pointer type.
  • _OFFSET function return values should be stored in _OFFSET type variables. As no other variable type is 'elastic' like _OFFSET, there can be no guarantee that any other variable type can hold the value of an _OFFSET.
  • Returns the memory offset of variables, user-defined-types & elements, arrays & indices and the base offset of STRINGs.
  • Offset values are currently only useful when used in conjunction with _MEM or DECLARE LIBRARY procedures.
  • OFFSET values are used as a part of the _MEM variable type in QB64; variable.OFFSET returns or sets the current position in memory.
  • Warning: QB64 variable length strings can move about in memory at any time. If you get the _OFFSET of a variable length sting on one line and use it on the next it may not be there anymore. To be safe, move variable length strings into fixed length strings first.


Examples

Example: Using memcpy with the _OFFSET function values as parameters.

DECLARE CUSTOMTYPE LIBRARY
    SUB memcpy (BYVAL dest AS _OFFSET, BYVAL source AS _OFFSET, BYVAL bytes AS LONG)
END DECLARE

a$ = "1234567890"
b$ = "ABCDEFGHIJ"

memcpy _OFFSET(a$) + 5, _OFFSET(b$) + 5, 5
PRINT a$
12345FGHIJ


See also



Navigation:
Main Page with Articles and Tutorials
Keyword Reference - Alphabetical
Keyword Reference - By usage
Report a broken link