QB64.org Forum

Active Forums => QB64 Discussion => Topic started by: Petr on August 11, 2020, 04:25:37 pm

Title: How to work around stupid constraints with offset type?
Post by: Petr on August 11, 2020, 04:25:37 pm
I need to find the first non-zero character in the field (from end). The function on the input is received by the _MEM pointer. Because I need the last non-zero character in the field, I read from m.size to zero. In the moment, when readed value is not zero, I get an offset. Then I need to use the result as a value of type long. However, I will encounter an stupid restriction with the offset difference. How to work around this stupid restriction?

I need this: offset A - offset B = search&
Title: Re: How to work around stupid constraints with offset type?
Post by: Petr on August 11, 2020, 05:04:10 pm
I figured it out ... I define the variable as _INTEGER64 (because it collects 8 bytes in memory just like _OFFSET), I point to it via MEM and use MEMPUT to write the value to it.
Title: Re: How to work around stupid constraints with offset type?
Post by: SMcNeill on August 11, 2020, 07:02:21 pm
Code: [Select]
DIM x AS INTEGER
DIM m AS _MEM
m = _MEM(x)
PRINT m.OFFSET
PRINT ConvertOffset(m.OFFSET)


FUNCTION ConvertOffset&& (value AS _OFFSET)
$CHECKING:OFF
DIM m AS _MEM 'Define a memblock
m = _MEM(value) 'Point it to use value
$IF 64BIT THEN
    'On 64 bit OSes, an OFFSET is 8 bytes in size.  We can put it directly into an Integer64
    _MEMGET m, m.OFFSET, ConvertOffset&& 'Get the contents of the memblock and put the values there directly into ConvertOffset&&
$ELSE
    'However, on 32 bit OSes, an OFFSET is only 4 bytes.  We need to put it into a LONG variable first
    _MEMGET m, m.OFFSET, temp& 'Like this
    ConvertOffset&& = temp& 'And then assign that long value to ConvertOffset&&
$END IF
_MEMFREE m 'Free the memblock
$CHECKING:ON
END FUNCTION

This little routine should be preserved in samples, and everyone’s toolbox, if they work with offsets much at all.  ;)