Excerpts from the mail message of Daniel Shane:
) 
) void init(str)
)       char *   &str
) 
) void fill(str, str2)
)       char *    str
)       char *    str2
) 
) void free(str)
)       char *    str
) 
) void init(char *str) {
)       *str = (char *)calloc(20, sizeof(char));
) }
) 
) void fill(char *str, char *str2) {
)       strcpy(str, str2);
) }
) 
) void free(char *str) {
)       free(str);
) }

Nope, Perl won't treat as a string any memory that it didn't
allocate itself.  It will try to reallocate and/or free it for
any number of reasons.

I'm not sure why you want to do something like this, but
you can either store the pointer to the allocated memory
into something that is relatively opaque to the Perl script
(such as an integer or the pointer packed into a string),
or you can use Perl's *PV* routines to allocate the memory
and let Perl free it when it wants to.

I'd go into more details but I'll wait until you explain
why you want this and which way you'd rather go.

) Now in perl I could simply do:
) 
) my $test_string;           # $test_string is undefined...
) 
) init(\$test_string);       # now $test_string should be defined but empty...
) print "$test_string \n";   # Should print nothing...

No, \$test_string makes a Perl reference which isn't something
you want to pass to XS code that is expecting a "char *".   Did
you know that you can modify $test_string even when you write

    init($test_string)

?  Perl passes arguments "by reference" (not to be confused
with references that Perl creates with \, so I'd say it is
more accurately expressed as Perl passing arguments by making
@_ contian _aliases_ to the actual parameters).

Tye

Reply via email to