| Thread overview | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
|
June 20, 2007 D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
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 ?)
Will the D copy be garbage collected ?
Thanks
| ||||
June 20, 2007 Re: D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos Smith | 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 | |||
June 20, 2007 Re: D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos Smith | "Carlos Smith" <carlos-smith@sympatico.ca> wrote in message news:f5bvrr$2ui6$1@digitalmars.com... > 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 ?) > Will the D copy be garbage collected ? Slice and dup! char[] dtext = cctext[0 .. ccleng].dup; You can slice pointers to turn them into D arrays, and then you can .dup them to copy the array into a new array. This duplicated array will not be overwritten by yylex() and will be garbage collected. | |||
June 20, 2007 Re: D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Kirk McDonald | "Kirk McDonald" <kirklin.mcdonald@gmail.com> wrote in message news:f5c3ko$3vh$1@digitalmars.com... NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOooooooo........ | |||
June 20, 2007 Re: D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jarrett Billingsley | "Jarrett Billingsley" <kb3ctd2@yahoo.com> wrote in message news:f5c3nr$47l$1@digitalmars.com... : "Kirk McDonald" <kirklin.mcdonald@gmail.com> wrote in message : news:f5c3ko$3vh$1@digitalmars.com... : : NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOooooooo........ : Thanks to both of you ! But, i am sorry to say that i dont understand that NO... What does it means ? Are you saying that Kirk is wrong ? Or is it a form of humour ? | |||
June 20, 2007 Re: D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos Smith | Carlos Smith wrote:
> But, i am sorry to say that i dont understand that NO...
>
> What does it means ?
>
> Are you saying that Kirk is wrong ?
> Or is it a form of humour ?
I think he was pwned.... :)
Regards, Frank
| |||
June 21, 2007 Re: D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
Posted in reply to 0ffh | 0ffh wrote:
> Carlos Smith wrote:
>
>> But, i am sorry to say that i dont understand that NO...
>>
>> What does it means ?
>>
>> Are you saying that Kirk is wrong ?
>> Or is it a form of humour ?
>
>
> I think he was pwned.... :)
>
> Regards, Frank
in his favor, Mr. Billingsley version saves a function call and doesn't need to probe for the end-of-string.
| |||
June 21, 2007 Re: D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Carlos Smith | "Carlos Smith" <carlos-smith@sympatico.ca> wrote in message news:f5c4bj$5bp$1@digitalmars.com... > But, i am sorry to say that i dont understand that NO... > > What does it means ? > > Are you saying that Kirk is wrong ? > Or is it a form of humour ? We just posted at almost exactly the same time with almost exactly the same answer, that's all :) | |||
June 21, 2007 Re: D processing a char* allocated by a C module | ||||
|---|---|---|---|---|
| ||||
Posted in reply to BCS | BCS wrote:
> in his favor, Mr. Billingsley version saves a function call and doesn't need to probe for the end-of-string.
You're right, let's call it a performance-typing tradeoff... :)
Regards, Frank
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply