Author Topic: How to work around stupid constraints with offset type?  (Read 3272 times)

0 Members and 1 Guest are viewing this topic.

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
How to work around stupid constraints with offset type?
« 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&

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: How to work around stupid constraints with offset type?
« Reply #1 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.

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: How to work around stupid constraints with offset type?
« Reply #2 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.  ;)
« Last Edit: August 11, 2020, 07:03:50 pm by SMcNeill »
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!