2

I use StringValueCStr to convert ruby String to char*. Do i need to free memory when this new C string is no longer needed?

If this new C string is freed automatically, does this mean i should copy it if i plan on saving it in my C structures for later use?

user124
  • 1,632
  • 1
  • 11
  • 11

1 Answers1

4

StringValueCStr returns the same pointer that StringValuePtr does -- which is the internal string buffer of the object.

That buffer could change or be invalidated by any subsequent mutation of the String instance (or, of course, a garbage collection), so yes you should copy it if you need to keep it around.

matthewd
  • 4,332
  • 15
  • 21