The Crystal Programming Language Forum

Sizeof pointer type via macro

Newbie here who came across crystal just few days back. Actually I am trying to use the ioctl shard to run a user app to control a LKM driver. While I am able to make it work fine, I face an issue where I had to pass the pointer size (worked it around by passing Int64 with the ioctl C binding instead of pointer type). In abstract following is the issue below, this macro has to return the size of pointer type but I get unterminated call error. This may be a trivial issue (pardon me). How can I get the size of pointer type via macro? This is Crystal version 1.0.0, Debian Linux x86_64

macro m(t)
  sizeof({{ t }})
end

print sizeof(Int32*)	# prints 8
print m(Int32)		# prints 4
print m(Int32*)		# ERROR unterminated call

Looks to be a macro parer bug. I’d file a bug for it.

However, I think it’s worth pointing out that the sizeof call isn’t executing at compile time. Your code is equivalent to:

print sizeof(Int32*)
print sizeof(Int32)
print sizeof(Int32*)

since the macro code is expanded at compile time, i.e. the actual printing all happens at runtime. Don’t really need to use a macro here, just use sizeof directly.

EDIT: As a workaround you can do m(Pointer(Int32)) as Int32* is just syntax sugar for that.

There are compile time flags bits32 and bits64. Use 4 for the first one, 8 for the second one.

Thanks, print m(Pointer(Int32)) outputs 8 correctly as expected in the example code I shared before.