From: Yusuke ENDOH Date: 2010-07-09T22:43:38+09:00 Subject: [ruby-core:31158] Re: Why is there no standard way of creating a String from a char *? Hi, 2010/7/9 Nikolai Weibull : > I�m sorry for being so slow to understand, but I still don�t see why > Ruby can�t use the char * that I�ve malloced myself. ?I want to be > able to tell Ruby that �here, take this char *, I own it, no one else > knows about it, guaranteed, and I�m now giving it to you for you to > own, I don�t want it anymore�. You may want to use rb_str_buf_new instead of malloc. /* allocate buffer whose size is s */ VALUE str = rb_str_buf_new(s); /* 'library_call' writes to the buffer */ library_call(RSTRING_PTR(str), &s); /* update the length of string */ rb_str_resize(str, s); return str; Be careful not to allow GC to occur within 'library_call'. GC may shrink the buffer. -- Yusuke Endoh