From: matz@... (Yukihiro Matsumoto) Date: 2001-12-28T16:42:50+09:00 Subject: [ruby-talk:29517] Re: C extensions: ruby and C strings Hi, In message "[ruby-talk:29515] C extensions: ruby and C strings" on 01/12/28, Tomasz Wegrzanowski writes: |1) Should I free string here or is it used as string buffer by Ruby ? | | char *cstr; | VALUE rstr; | | rstr = rb_str_new2(cstr); | free (cstr); // ??? rb_str_new2() copies c string, so that you have to free cstr. |2) Does cstr point to Ruby string buffer or should I free it ? | | VALUE rstr; | char *cstr; | | cstr = rb_str2cstr (rstr, 0); | do_something_with_const_string (cstr); | free (cstr); // ??? In this case, cstr is a pointer to a internal buffer in a String object, so that you don't have to free. |3) Is RSTRING(rstr)->ptr null-terminated ? Yes. But do not rely on it too much. I'd like to guarantee that a pointer retrieved from rb_str2cstr() (or StringValuePtr() in 1.7) is null-terminated. matz.