Nim Compiler Version 1.2.12 [Linux: amd64] Compiled at 2021-04-21 Copyright (c) 2006-2020 by Andreas Rumpf
active boot switches: -d:release
var v: a
echo a.sizeof
v.sizeof returns size of whole reference (with metadata). I must use v[].sizeof . In my opinion, it is an error, since why we would have access to internal nim types?
sizeof(a)
in you example is in no way related to the object size, but is the size of of the reference itself. That is the size of the managed pointer, in C it would be the size of a pointer. That value is 4 or 8 byte on 32 or 64 bit systems, and it is unrelated to the size of the object which the ref points to.
Well, that is what Yardanico already told you :-)
More interesting is
type
A = ref object of RootObj
i: cint
B = ref object
i: cint
echo sizeof(A()[]) #16
echo sizeof(B()[]) #4
I think B()[] is a plain C struct, while A()[] is an object with inheritance support, so has hidden metatdata. I was not sure of that myself before.
type
A = ref object
v: int
B = ref object
v1: int
v2: int
C = ref object
v1: int
v2: int
v3: int
echo sizeof(A) # 8
echo sizeof(B) # 8
echo sizeof(C) # 8