Bug #20614


Integer#size returns incorrect values on 64-bit Windows

Added by surusek (Łukasz Sur) 5 days ago. Updated 4 days ago.

Target version:
ruby -v:
ruby 3.4.0dev (2024-07-08T11:00:01Z master 02c4f0c89d [x64-mswin64_140]


According to the ruby/spec, 0.size should return size of the machine word in bytes, but on x64-mswin64_140 (both release 3.3.3 and git revision 02c4f0c89d) it doesn't. Following example:

a, b = 0.size, [0].pack('J').length
puts a, b

should print two 8s, but on x64-mswin64_140, a is 4.
Issue is most likely caused by use of long instead of SIGNED_VALUE in internal/fixnum.h and fix_size function in numeric.c, because on Windows, long is always a 32-bit number.

Updated by akr (Akira Tanaka) 5 days ago

You can use RbConfig::SIZEOF to query the size of a C type.

% ruby -v -rrbconfig/sizeof -e 'pp RbConfig::SIZEOF'
ruby 3.4.0dev (2024-05-16T19:35:22Z master 854cbbd5a9) [x86_64-linux]
 "long long"=>8,
 "long double"=>16,
 "float _Complex"=>8,
 "double _Complex"=>16,
 "long double _Complex"=>32,

Updated by alanwu (Alan Wu) 5 days ago · Edited

IMO based on the current wording of the documentation it should always return sizeof(VALUE) for fixnums, because VALUE holds the machine representation for fixnums.

By the way, ruby/spec is not a specification for how things ought to behave; it's descriptive not prescriptive. Check out the README of the project:

Updated by Eregon (Benoit Daloze) 4 days ago

Agreed with @alanwu (Alan Wu), the docs seems clear, and one would expect this method returns how many bytes are used to represent the Integer (not counting object header overhead for bignums, fair enough):

  int.size  ->  int

Document-method: Integer#size

Returns the number of bytes in the machine representation of int
(machine dependent).

  1.size               #=> 8
  -1.size              #=> 8
  2147483647.size      #=> 8
  (256**10 - 1).size   #=> 10
  (256**20 - 1).size   #=> 20
  (256**40 - 1).size   #=> 40

So it should be pointer-size bytes for any Fixnum on any platform (since Fixnum are tagged pointers).


Also available in: Atom PDF