# 1.9 Ruby C Extension?

**URL:** <https://rubytalk.org/t/1-9-ruby-c-extension/48182>\
**Category:** ruby-talk\
**Created:** [7 August 2008 17:44 UTC](https://rubytalk.org/t/1-9-ruby-c-extension/48182 "2008-08-07T17:44:03Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Katebi](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@Alex\_Katebi](https://rubytalk.org/u/Alex_Katebi)\
**Post date:** [7 August 2008 17:44 UTC](https://rubytalk.org/t/1-9-ruby-c-extension/48182/1 "2008-08-07T17:44:03Z")

</div>

Hi All,

&nbsp;&nbsp;&nbsp;When writing ruby extensions in C, how can I save C pointers into ruby  
variables Bignum or Fixnum?  
The only Macros available are INT2FIX, INT2NUM & FIX2INT and NUM2INT. I can  
not save a pinter value that is 32 bits long.

example:

static VALUE round\_trip(VALUE self, VALUE vptr)  
{  
&nbsp;&nbsp;&nbsp;&nbsp;void\* ptr = NUM2INT(vptr);  
&nbsp;&nbsp;&nbsp;&nbsp;return INT2NUM(ptr);  
}

---

<div class="post-metadata">

**Author:** ![Tim\_Hunter1](https://avatars.discourse-cdn.com/v4/letter/t/3ec8ea/32.png) [@Tim\_Hunter1](https://rubytalk.org/u/Tim_Hunter1)\
**Post date:** [7 August 2008 18:04 UTC](https://rubytalk.org/t/1-9-ruby-c-extension/48182/2 "2008-08-07T18:04:13Z")

</div>

Alex Katebi wrote:

> Hi All,
> 
> &nbsp;&nbsp;&nbsp;When writing ruby extensions in C, how can I save C pointers into  
> ruby  
> variables Bignum or Fixnum?  
> The only Macros available are INT2FIX, INT2NUM & FIX2INT and NUM2INT. I  
> can  
> not save a pinter value that is 32 bits long.
> 
> example:
> 
> static VALUE round\_trip(VALUE self, VALUE vptr)  
> {  
> &nbsp;&nbsp;&nbsp;&nbsp;void\* ptr = NUM2INT(vptr);  
> &nbsp;&nbsp;&nbsp;&nbsp;return INT2NUM(ptr);  
> }

I don't think I'd do that. My inclination would be to create a structure  
to hold the pointer and wrap the structure into a Ruby object.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Jason\_R](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Jason\_R](https://rubytalk.org/u/Jason_R)\
**Post date:** [7 August 2008 18:10 UTC](https://rubytalk.org/t/1-9-ruby-c-extension/48182/3 "2008-08-07T18:10:25Z")

</div>

Um, yeah, that, that's just wrong. What exactly are you trying to  
accomplish with this extension?

In case you haven't read it, please go read:  
[http://whytheluckystiff.net/ruby/pickaxe/](http://whytheluckystiff.net/ruby/pickaxe/), the chapter called  
Extending Ruby

Jason

> **···**
>
> On Thu, Aug 7, 2008 at 1:44 PM, Alex Katebi \<alex.katebi@gmail.com\> wrote:
> 
> > Hi All,
> > 
> > &nbsp;&nbsp;When writing ruby extensions in C, how can I save C pointers into ruby  
> > variables Bignum or Fixnum?  
> > The only Macros available are INT2FIX, INT2NUM & FIX2INT and NUM2INT. I can  
> > not save a pinter value that is 32 bits long.
> > 
> > example:
> > 
> > static VALUE round\_trip(VALUE self, VALUE vptr)  
> > {  
> > &nbsp;&nbsp;&nbsp;void\* ptr = NUM2INT(vptr);  
> > &nbsp;&nbsp;&nbsp;return INT2NUM(ptr);  
> > }
