digitalmars.D.learn - D processing a char* allocated by a C module
- Carlos Smith (21/21) Jun 20 2007 Hi !
- Kirk McDonald (8/37) Jun 20 2007 Yes.
-
Jarrett Billingsley
(3/3)
Jun 20 2007
"Kirk McDonald"
wrote in message -
Carlos Smith
(13/13)
Jun 20 2007
"Jarrett Billingsley"
wrote in message - 0ffh (3/9) Jun 20 2007 I think he was pwned.... :)
- BCS (3/16) Jun 20 2007 in his favor, Mr. Billingsley version saves a function call and doesn't
- 0ffh (3/5) Jun 21 2007 You're right, let's call it a performance-typing tradeoff... :)
- Jarrett Billingsley (4/8) Jun 20 2007 We just posted at almost exactly the same time with almost exactly the s...
- Jarrett Billingsley (7/27) Jun 20 2007 Slice and dup!
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
Jun 20 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
Jun 20 2007
"Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message news:f5c3ko$3vh$1 digitalmars.com... NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOooooooo........
Jun 20 2007
"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 ?
Jun 20 2007
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
Jun 20 2007
0ffh wrote:Carlos Smith wrote:in his favor, Mr. Billingsley version saves a function call and doesn't need to probe for the end-of-string.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
Jun 20 2007
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
Jun 21 2007
"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 :)
Jun 20 2007
"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.
Jun 20 2007