From: Charles Mills Date: 2005-07-26T05:51:01+09:00 Subject: Re: Ruby extensions on 64 bit linux Berger, Daniel wrote: > > -----Original Message----- > > From: Joe Van Dyk [mailto:joevandyk@gmail.com] > > Sent: Monday, July 25, 2005 12:24 PM > > To: ruby-talk ML > > Subject: Ruby extensions on 64 bit linux > > > > > > http://www.ruby-talk.org/cgi-bin/scat.rb/ruby/ruby-talk/149446 > > makes me wonder, when writing C extensions, are there > > anything I should be watching out for when intending the code > > to be run on a 64 bit OS? > > > > Thanks, > > Joe > > * Don't assume a pointer and an int are the same size > * Don't make assumptions about the relative size of variable types > * Be wary of sign extension problems > * Use pointer arithmetic rather than address arithmetic > * Never cast malloc > * Explicitly include header files (-Wall will warn you about implicit > declarations) > * Watch out for data truncation, e.g. passing a size_t to something that > expects an int. > * Pack (align) your data structures as tightly as possible > * Watch out for data loss in constant expressions > * Use %p in your (s)printf's as appropriate > * Use #ifdef _LP64 as needed (or is that Solaris only?) > > Regards, > > Dan That is a nice list. It seems the first bullet on your list causes the majority of problems. From ruby.h: #if SIZEOF_LONG != SIZEOF_VOIDP # error ---->> ruby requires sizeof(void*) == sizeof(long) to be compiled. <<---- #endif typedef unsigned long VALUE; typedef unsigned long ID; ---- On the vast majority of 64bit systems/compilers sizeof(long) > sizeof(int), however on many 32bit systems sizeof(long) == sizeof(int). When the type of a function return value is unknown most/all compilers default to int. If you have the following statement in your code VALUE obj = my_func(arg); (where my_func() returns a VALUE) and do not define or give a function prototype for my_func() prior to the statement the result of my_func() will be cast to an int (by default) and then cast to a VALUE, causing truncation on most 64 bit systems. Turning warnings on catches these kind of errors. Adding to the list: * turn on warnings * use ANSI C function prototypes FYI: the current Ruby sources do not always this :( -Charlie