www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - D processing a char* allocated by a C module

reply "Carlos Smith" <carlos-smith sympatico.ca> writes:
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
next sibling parent reply Kirk McDonald <kirklin.mcdonald gmail.com> writes:
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
parent reply "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Kirk McDonald" <kirklin.mcdonald gmail.com> wrote in message 
news:f5c3ko$3vh$1 digitalmars.com...

NOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOooooooo........ 
Jun 20 2007
parent reply "Carlos Smith" <carlos-smith sympatico.ca> writes:
"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
next sibling parent reply 0ffh <spam frankhirsch.net> writes:
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
parent reply BCS <BCS pathlink.com> writes:
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.
Jun 20 2007
parent 0ffh <spam frankhirsch.net> writes:
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
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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
prev sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"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