Page 1 of 1

PB string handling.

Posted: Sun Jan 25, 2009 5:58 am
by marksibly
Hi,

I've got library that uses C-style 'const char *' to return strings.

Up until now, I've been using PeekS() to use such strings in PB. For example, say I've got a C function...

const char *bbDoSomething(){
return "Something";
}

...in PB I'm using...

Debug PeekS( bbDoSomething() )

But what exactly does PeekS() do? Does it 'copy' the string at all?

The library actually reuses a global string buffer to return strings, and this buffer will get overwritten the next time a string returning function is called.

Therefore, if PeekS() is assigned to a var, but not copied, then the var will eventually get snotted. Correct?

Is there any way to copy a string returned 'const char * style' from a lib like this?

How about: PeekS( bbDoSomething() )+""

Will this, as I'm hoping, create a copy of the returned string (with "" appended)?

Is there a more elegant way to do this? How does PB's string GC work in general?

Bye!
Mark

Posted: Sun Jan 25, 2009 11:45 am
by Trond
Will this, as I'm hoping, create a copy of the returned string (with "" appended)?
"" is an empty string. Appending an empty string will do absolutely nothing.

PeekS() already copies the string.

Posted: Sun Jan 25, 2009 12:55 pm
by idle
PeekS should be fine.

For a null terminated *char

Text.s = PeekS(*char)

if it's not null terminated pass in the length

Text.s = PeekS(*char,length)

Posted: Sun Jan 25, 2009 1:05 pm
by Kaeru Gaman
when you're assigning the result of PeekS to a Variable like idle showed,
sure the content will be copied into the destination variable.

when you use Debug, there is only the temporary buffer of the debugger to hold it, so sure it will be lost.

PeekS does return the content into the Variable, but not alter the pointer of the Variable.

Posted: Sun Jan 25, 2009 4:00 pm
by netmaestro
if PeekS() is assigned to a var, but not copied, then the var will eventually get snotted. Correct?
No, Result$ = PeekS( bbDosomething() ) makes a new copy of the string in Result$.