# Easiest way to access rb\_sprintf

**URL:** <https://www.ruby-forum.com/t/easiest-way-to-access-rb-sprintf/146150>\
**Category:** Ruby\
**Created:** [August 18, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/easiest-way-to-access-rb-sprintf/146150 "2008-08-18T15:35:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [August 18, 2008, 3:35pm UTC](https://www.ruby-forum.com/t/easiest-way-to-access-rb-sprintf/146150/1 "2008-08-18T15:35:32Z")

</div>

Hi

I want to create a ruby method in a C++ extension that will spit out the  
C++ pointer address of the wrapped object - useful for debugging. I  
have got the following, which works, but I was wondering if there was an  
easier way to access this that I’m missing?

static VALUE  
cpp\_ptr\_addr(VALUE self, VALUE obj)  
{  
size\_t ptr = (size\_t)DATA\_PTR(obj);  
return rb\_funcall( rb\_mKernel, rb\_intern(“sprintf”), 2,  
rb\_str\_new2(“0x%x”), OFFT2NUM(ptr) );  
}

thanks  
alex

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [August 18, 2008, 4:09pm UTC](https://www.ruby-forum.com/t/easiest-way-to-access-rb-sprintf/146150/2 "2008-08-18T16:09:36Z")

</div>

Alex F. wrote:

> Hi
> 
> I want to create a ruby method in a C++ extension that will spit out the  
> C++ pointer address of the wrapped object - useful for debugging. I  
> have got the following, which works, but I was wondering if there was an  
> easier way to access this that I’m missing?
> 
> static VALUE  
> cpp\_ptr\_addr(VALUE self, VALUE obj)  
> {  
> size\_t ptr = (size\_t)DATA\_PTR(obj);  
> return rb\_funcall( rb\_mKernel, rb\_intern(“sprintf”), 2,  
> rb\_str\_new2(“0x%x”), OFFT2NUM(ptr) );  
> }
> 
> thanks  
> alex

Instead of calling Ruby’s sprintf, use C’s standard sprintf with the %p  
conversion specifier, then convert the result to a Ruby string.

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [August 18, 2008, 8:30pm UTC](https://www.ruby-forum.com/t/easiest-way-to-access-rb-sprintf/146150/3 "2008-08-18T20:30:17Z")

</div>

Hi,

At Mon, 18 Aug 2008 22:31:41 +0900,  
Alex F. wrote in [ruby-talk:311670]:

> I want to create a ruby method in a C++ extension that will spit out the  
> C++ pointer address of the wrapped object - useful for debugging. I  
> have got the following, which works, but I was wondering if there was an  
> easier way to access this that I’m missing?

rb\_sprintf is a function added in 1.9, and you can use it like  
printf.

> static VALUE  
> cpp\_ptr\_addr(VALUE self, VALUE obj)  
> {  
> void \*ptr = DATA\_PTR(obj);  
> return rb\_sprintf("%p", ptr);

---

<div class="post-metadata">

**Author:** ![absolute-beginner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a5b964/32.png) [@absolute-beginner](https://www.ruby-forum.com/u/absolute-beginner)\
**Post date:** [August 19, 2008, 3:30am UTC](https://www.ruby-forum.com/t/easiest-way-to-access-rb-sprintf/146150/4 "2008-08-19T03:30:45Z")

</div>

Nobuyoshi N. wrote:

> > static VALUE  
> > cpp\_ptr\_addr(VALUE self, VALUE obj)  
> > {  
> > void \*ptr = DATA\_PTR(obj);  
> > return rb\_sprintf("%p", ptr);  
> > }

excellent, thank you both. i’m ultimately targetting 1.9 so this is  
ideal.

alex
