PB string handling.

Just starting out? Need help? Post your questions and find answers here.
marksibly
New User
New User
Posts: 4
Joined: Wed Jan 21, 2009 10:40 pm

PB string handling.

Post 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
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
User avatar
idle
Always Here
Always Here
Posts: 6108
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Post 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)
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Post 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.
oh... and have a nice day.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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$.
BERESHEIT
Post Reply