# \[ruby-core:23407\] ruby-ffi problem

**URL:** <https://rubytalk.org/t/ruby-core-23407-ruby-ffi-problem/53319>\
**Category:** ruby-talk\
**Created:** [9 May 2009 09:48 UTC](https://rubytalk.org/t/ruby-core-23407-ruby-ffi-problem/53319 "2009-05-09T09:48:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aston](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@Aston](https://rubytalk.org/u/Aston)\
**Post date:** [9 May 2009 09:48 UTC](https://rubytalk.org/t/ruby-core-23407-ruby-ffi-problem/53319/1 "2009-05-09T09:48:33Z")

</div>

Hello,

I am trying to learn ruby-ffi now (after sadly leaving dl library), I have some trouble here too

I will present a simple example where I want to pass a struct between ruby and C  
I am facing problems while setting fields of struct if that field is of type string(char\*, not char[])  
I understand we have to allocate the char\* field of struct first than assign some literal srting

consider the C file

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* C FILE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
#include \<stdlib.h\>  
#include \<stdio.h\>  
#include \<string.h\>

typedef struct  
{  
&nbsp;&nbsp;&nbsp;&nbsp;char \*name;  
&nbsp;&nbsp;&nbsp;&nbsp;double val;  
} INFO, \*INFO\_PTR;

INFO\_PTR create( char\* name, double val)  
{  
&nbsp;&nbsp;&nbsp;&nbsp;INFO\_PTR p = (INFO\_PTR)malloc( sizeof(INFO));  
&nbsp;&nbsp;&nbsp;&nbsp;p-\>val = val;  
&nbsp;&nbsp;&nbsp;&nbsp;p-\>name = (char\*)malloc( strlen(name) + 1);  
&nbsp;&nbsp;&nbsp;&nbsp;strcpy( p-\>name, name);  
&nbsp;&nbsp;&nbsp;&nbsp;return p;  
}

int show( INFO\_PTR pInfo)  
{  
&nbsp;&nbsp;&nbsp;&nbsp;return printf( "%s - %f\n", pInfo-\>name, pInfo-\>val);  
}

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* END C FILE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* RUBY FILE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
require 'ffi'

module LibTest  
&nbsp;&nbsp;class Info \< FFI::Struct  
&nbsp;&nbsp;&nbsp;&nbsp;layout :name, :string,  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:val, :double  
&nbsp;&nbsp;end

&nbsp;&nbsp;extend FFI::Library  
&nbsp;&nbsp;ffi\_lib "./libtest.so"  
&nbsp;&nbsp;attach\_function :create, [:string, :double], :pointer  
&nbsp;&nbsp;attach\_function :show, [:pointer], :int  
end

include FFI

ptr = LibTest.create( "test string", 11.27)  
obj = LibTest::Info.new(ptr)  
LibTest.show(ptr)  
obj[:val] = 27.11 # this works  
# obj[:name] = "new string" # this gives error, `[]=': Cannot set :string fields (ArgumentError)  
LibTest.show(ptr)  
\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* END RUBY FILE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*

how can I set string values from ruby ?  
If I refuse to declare name field as type char[] from char\* then I have to allocate before I can assign  
then my approach below core dumps ☹

1 str = "test string"  
2 p = MemoryPointer.new( str.size)  
3 p.write\_string(str)  
4 puts p.read\_string # "test string"  
5 obj[:name].write\_pointer(p) # core dumps here!

line # 5 core dumps everytime one tries to write anything there, since pointer is invalid I guess  
how do I allocate memory there ? once allocated can i treat that allocated memory as ruby string ?

Goal I want to achieve is I shall allocate in ruby, assign in ruby and pass in to C code only for modification or read only purpose  
how do I go from here ? can you comment on this ?

Aston

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cricket on your mind? Visit the ultimate cricket website. Enter [http://beta.cricket.yahoo.com](http://beta.cricket.yahoo.com)

---

<div class="post-metadata">

**Author:** ![Charles\_Oliver\_Nutte](https://avatars.discourse-cdn.com/v4/letter/c/71e660/32.png) [@Charles\_Oliver\_Nutte](https://rubytalk.org/u/Charles_Oliver_Nutte)\
**Post date:** [9 May 2009 18:18 UTC](https://rubytalk.org/t/ruby-core-23407-ruby-ffi-problem/53319/2 "2009-05-09T18:18:48Z")

</div>

You probably want to take this to the ruby-ffi project here:

[http://kenai.com/projects/ruby-ffi](http://kenai.com/projects/ruby-ffi)

Aston wrote:

> **···**
>
> > Hello,
> > 
> > I am trying to learn ruby-ffi now (after sadly leaving dl library), I have some trouble here too
> > 
> > I will present a simple example where I want to pass a struct between ruby and C  
> > I am facing problems while setting fields of struct if that field is of type string(char\*, not char)  
> > I understand we have to allocate the char\* field of struct first than assign some literal srting
> > 
> > consider the C file
> > 
> > \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* C FILE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > #include \<stdlib.h\>  
> > #include \<stdio.h\>  
> > #include \<string.h\>
> > 
> > typedef struct  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;char \*name;  
> > &nbsp;&nbsp;&nbsp;&nbsp;double val;  
> > } INFO, \*INFO\_PTR;
> > 
> > INFO\_PTR create( char\* name, double val)  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;INFO\_PTR p = (INFO\_PTR)malloc( sizeof(INFO));  
> > &nbsp;&nbsp;&nbsp;&nbsp;p-\>val = val;  
> > &nbsp;&nbsp;&nbsp;&nbsp;p-\>name = (char\*)malloc( strlen(name) + 1);  
> > &nbsp;&nbsp;&nbsp;&nbsp;strcpy( p-\>name, name);  
> > &nbsp;&nbsp;&nbsp;&nbsp;return p;  
> > }
> > 
> > int show( INFO\_PTR pInfo)  
> > {  
> > &nbsp;&nbsp;&nbsp;&nbsp;return printf( "%s - %f\n", pInfo-\>name, pInfo-\>val);  
> > }
> > 
> > \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* END C FILE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> > 
> > \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* RUBY FILE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
> > require 'ffi'
> > 
> > module LibTest  
> > &nbsp;&nbsp;class Info \< FFI::Struct  
> > &nbsp;&nbsp;&nbsp;&nbsp;layout :name, :string,  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:val, :double  
> > &nbsp;&nbsp;end
> > 
> > &nbsp;&nbsp;extend FFI::Library  
> > &nbsp;&nbsp;ffi\_lib "./libtest.so"  
> > &nbsp;&nbsp;attach\_function :create, [:string, :double], :pointer  
> > &nbsp;&nbsp;attach\_function :show, [:pointer], :int  
> > end
> > 
> > include FFI
> > 
> > ptr = LibTest.create( "test string", 11.27)  
> > obj = LibTest::Info.new(ptr)  
> > LibTest.show(ptr)  
> > obj[:val] = 27.11 # this works  
> > # obj[:name] = "new string" # this gives error, `=': Cannot set :string fields (ArgumentError)  
> > LibTest.show(ptr)  
> > \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\* END RUBY FILE\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*
> > 
> > how can I set string values from ruby ?  
> > If I refuse to declare name field as type char from char\* then I have to allocate before I can assign  
> > then my approach below core dumps ☹
> > 
> > 1 str = "test string"  
> > 2 p = MemoryPointer.new( str.size)  
> > 3 p.write\_string(str)  
> > 4 puts p.read\_string # "test string"  
> > 5 obj[:name].write\_pointer(p) # core dumps here!
> > 
> > line # 5 core dumps everytime one tries to write anything there, since pointer is invalid I guess  
> > how do I allocate memory there ? once allocated can i treat that allocated memory as ruby string ?
> > 
> > Goal I want to achieve is I shall allocate in ruby, assign in ruby and pass in to C code only for modification or read only purpose  
> > how do I go from here ? can you comment on this ?
> > 
> > Aston
> > 
> > ------------------------------------------------------------------------  
> > Now surf faster and smarter ! Check out the new Firefox 3 - Yahoo! Edition \* Click here! \<[http://in.rd.yahoo.com/tagline\_firefox\_1/\*http://downloads.yahoo.com/in/firefox/?fr=om\_email\_firefox&gt](http://in.rd.yahoo.com/tagline_firefox_1/*http://downloads.yahoo.com/in/firefox/?fr=om_email_firefox&gt);
