Operator Mod not possible for pointers

General FreeBASIC programming questions.
Post Reply
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Operator Mod not possible for pointers

Post by SARG »

I was trying to check alignment of pointers and got twice this error : error 20: Type mismatch in 'print pttt mod 8'
I guess it could be allowed.

Code: Select all

dim ttt as INTEGER
dim pttt as integer ptr = @ttt

print pttt mod 8    'same with print @ttt mod 8

#print typeof(@ttt)
fxm
Moderator
Posts: 12082
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Operator Mod not possible for pointers

Post by fxm »

Addition and subtraction are allowed on pointer values. But multiplication and division are forbidden, so applying the MOD operator is too.

A workaround is to cast the pointer value to an UINTEGER:
print cast(uinteger, pttt) mod 8 '' same with 'print cast(uinteger, @ttt) mod 8'
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Operator Mod not possible for pointers

Post by SARG »

I workarounded the problem by using a temporary INTEGER variable.
However I don't see any serious reason not allowing mod operator for pointers.
angros47
Posts: 2321
Joined: Jun 21, 2005 19:04

Re: Operator Mod not possible for pointers

Post by angros47 »

The reason is likely that pointers are supposed to be already aligned, since a pointer can refer to any size. If you need to align them "by hand" you aren't using them in the way they are supposed to be used
SARG
Posts: 1756
Joined: May 27, 2005 7:15
Location: FRANCE

Re: Operator Mod not possible for pointers

Post by SARG »

You can check direcly by using @variable mod n if the variable is aligned on 4, 8, 16.
Not the pointer itself.
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: Operator Mod not possible for pointers

Post by adeyblue »

Going with the FB lineage, the reason is probably 'because you can't do it in C'. And in that land, you can't treat pointers as integers because the content of a pointer might not be linear memory addresses. Far pointers in dos and C++ member function pointers as examples of that
Post Reply