On Fri, Jul 9, 2010 at 06:20, Urabe Shyouhei <shyouhei / ruby-lang.org> 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 amemory
> 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.