D processing a char* allocated by a C module

Kirk McDonald kirklin.mcdonald at gmail.com
Wed Jun 20 13:51:00 PDT 2007


Carlos Smith wrote:
> Hi !
> 
> I have a C module, compiled with DMC.
> Coming from this C module:
> 
> extern (C)
> {
>   int ccleng;     // strlen(cctext)
>   int pos;
>   char *cctext;   // point into an input buffer
>   int yylex();
> }
> 
> cctext is a pointer to a buffer allocated by yylex().
> the buffer is small and can be freed or overwritten
> by yylex();
> 
> In the D module, i do:
> 
> char[] dtext = toString(cclex);
> 
> and dtext get corrupted (not always).
> 
> I guess toString do not make a new copy of cctext,
> just point to it.
> 
> How can i duplicate cctext, in D ? (strdup does not exist ?)

char[] dtext = toString(cclex).dup;

> Will the D copy be garbage collected ?
> 

Yes.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org


More information about the Digitalmars-d-learn mailing list