Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
STR2CSTR? Malloc or not?
jared.r.richardson@gmail.com
7/19/2006 7:05:00 PM
Hi all,
I've seen code examples for STR2CSTR that have mallocs and (mostly)
those that don't. Which way is right?
Is
char * first_string = STR2CSTR( passed_in_string);
good enough or should I be running:
char * first_string = malloc (sizeof(passed_in_string) + 1);
strcpy(first_string, passed_in_string);
Thanks,
Jared
http://jaredric...
1 Answer
Tim Hunter
7/19/2006 8:58:00 PM
0
jared.r.richardson@gmail.com wrote:
> Hi all,
>
> I've seen code examples for STR2CSTR that have mallocs and (mostly)
> those that don't. Which way is right?
>
> Is
>
> char * first_string = STR2CSTR( passed_in_string);
>
> good enough or should I be running:
>
> char * first_string = malloc (sizeof(passed_in_string) + 1);
> strcpy(first_string, passed_in_string);
>
> Thanks,
>
> Jared
>
http://jaredric...
>
In 1.8.4, STR2CSTR is marked "obsolete" in ruby.h. The preferred API is
String_Value_Ptr, which just calls rb_string_value_ptr in string.c. If
you look at that function, all it does is return a pointer to Ruby's
copy of the string in the string object.
So I would say it depends on what you want to do with the string. If you
just want to look at it, then you can just use the pointer you get back
from String_Value_Ptr. If you want to modify it without modifying Ruby's
copy of it, then you should make a copy. If you want to modify the
String object itself, then I'd try to use a Ruby API function to do it
instead of modifying Ruby's copy directly.
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
STR2CSTR? Malloc or not?
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password