question about keeeping reference to toStringz()
Ali Çehreli
acehreli at yahoo.com
Wed May 30 20:43:48 UTC 2018
On 05/30/2018 01:09 PM, Dr.No wrote:
> consider a C function with this prototype:
>> void foo(const char *baa);
>
> Does it means I should do:
>
>> string s = ...;
>> auto cstring = s.toStringz;
>> foo(cstring);
>
> rather just:
>
>> foo(s.toStringz);
>
> ?
It depends. cstring method above is not sufficient if cstring's life is
shorter than the C library's use:
void bar() {
string s = ...;
auto cstring = s.toStringz;
foo(cstring);
} // <- cstring is gone
What if the library saved that pointer while performing foo()?
If cstring is in module-scope or in a container (e.g. an array) that's
in module-scope then it's fine. But then, you would have to remove it
from that container when the C library does not need that pointer anymore.
Ali
More information about the Digitalmars-d-learn
mailing list