pointer->integer returns unexpected value on Windows

Create issue
Issue #22 resolved
Takashi Kato repo owner created an issue

Prepare C function;

void set_int(int *i) { *i = 0xF5; }

call above from Scheme like this;

(let ((p (integer->pointer -1))
       (set-int (c-function void set_int (void*))))
  (set-int (address p))
  (pointer->integer p))

This should return #xF5 but return something very huge.

Comments (4)

  1. Takashi Kato reporter

    This is probably an expected behaviour on 64 bit Windows. The actual cause is the different size of void* and long.

    So actual pointer value is 0xFFFFFFFFFFFFFFFF and set_int only sets 32 bit. then after setting the pointer value would be 0xFFFFFFFF000000F5. pointer->integer converts this value to a negative integer.

    So if the value is expected to be an 32 bit size then it's better to get pointer value directly (slot ref return uintptr_t value) and mask it.

    I'll add 4 procedures to handle this case

    • pointer->uinteger
    • uinteger->pointer (this is only for consistency)
    • pointer->long
    • pointer->ulong

    pointer->long and pointer->ulong would do the same as pointer->integer and pointer->uinteger on the environment which has the same size of void* and long.

  2. Takashi Kato reporter
    • changed status to open

    This doesn't work if void* = long = 8 environment (64 bit linux). it's better to let user specify how much pointer value want

    so pointer->integer and pointer->uinteger should have optional argument which specify the bits. (by default all)

  3. Log in to comment