From: Masaya TARUI Date: 2010-07-09T19:48:03+09:00 Subject: [ruby-core:31152] Re: Why is there no standard way of creating a String from a char *? It is not guaranteed that Ruby uses existing malloc. I think that you can use DL:CPtr for your purpose. 2010/7/9 Nikolai Weibull : > On Fri, Jul 9, 2010 at 06:20, Urabe Shyouhei wrote: >> (2010/07/09 7:04), Nikolai Weibull wrote: > >>> Why is there no standard way of creating a String from a char *? All >>> the rb_str_new* functions take a const char * and copy it. I would >>> like a function that would create a String from a char *, that is, >>> without copying it. > >> Because there is GC. You can't say if a string content should be freed or not >> when you allow such creation. If you need a non-copying region of a memory >> that should not be called a String. > > Perhaps I was unclear in my problem description. If I have a freshly > allocated char * that I get from a library call, why shouldn’t I be > able to use it as the basis of a Ruby String? I mean, currently I > have to do > > int length; > char *result = library_call(…, &length, …); > VALUE rbresult = rb_str_new(result, length); > free(result); > return rbresult; > > I own result, so after creating rbresult I currently have to > immediately free it. I realize that this doesn’t really matter at all > for small strings, but say that library_call() returns the contents of > an arbitrarily large file. Copying result inside rb_str_new seems to > me to be a total waste of energy. > > -- 樽家昌也(Masaya TARUI) No Tool,No Life.