Null char in string

Everything else that doesn't fall into one of the other PB categories.
Peyman
Enthusiast
Enthusiast
Posts: 203
Joined: Mon Dec 24, 2007 4:15 pm
Location: Iran

Null char in string

Post by Peyman »

Hi i know purebasic truncate strings till a null character but there is any way to find out what is behind that null character ? any issue ?

thanks

Code: Select all

mem = AllocateMemory(26)
PokeS(mem, "Hi" + Chr(0) + " Bye")
Debug PeekS(mem) ; Print Hi
Sorry for my bad english.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Null char in string

Post by rsts »

Should be safe as long as you're within the allocate.

cheers

peek would probably be ok as long as you're within your own program memory.
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Null char in string

Post by Kaeru Gaman »

I wonder if PokeS even pokes the chars behind the zero...
the string "Hi" + Chr(0) + " Bye" is a parameter to a command working with zero-terminated strings, so the second part should not be passed to the poke command at all.

completely working with structured pointers and/or PokeA()/PokeC() charwise should solve the problem.
and of course taking care of the length of your allocated mem not to ruin the heap...

PS:
as I thought, it's not poked

Code: Select all

mem = AllocateMemory(26)
PokeS(mem, "Hi" + Chr(0) + " Bye")

For poi = mem To mem + 25
  ch = PeekC( poi )
  Debug Str(ch) + " : " + Chr(ch)
Next
[edit]
forgot the line with the Peek, sorry...
Last edited by Kaeru Gaman on Sun Dec 13, 2009 4:54 am, edited 2 times in total.
oh... and have a nice day.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Null char in string

Post by rsts »

Good catch, Mr Gaman,

I was mistakenly referring to the part about finding out what was behind the null. I completely missed the "poke".

try it this way

Code: Select all

mem = AllocateMemory(26)
PokeS(mem, "Hi" + Chr(0) + " Bye")
PokeS(mem+3, " BYE")
For poi = mem To mem + 25
  ch = PeekC( poi )
  Debug Str(ch) + " : " + Chr(ch)
Next
cheers
Thorium
Addict
Addict
Posts: 1314
Joined: Sat Aug 15, 2009 6:59 pm

Re: Null char in string

Post by Thorium »

PokeS has a length parameter. :wink:
That should ignore the zero, but i didnt tested it.
Peyman
Enthusiast
Enthusiast
Posts: 203
Joined: Mon Dec 24, 2007 4:15 pm
Location: Iran

Re: Null char in string

Post by Peyman »

thanks Gaman and rsts good idea so we just can peek char by char to end of memory because we cant find end of string but i think this problem comes from strings value in purebasic not PokeS and PeekS, i think purebasic truncate strings till a null char then allocate it :

Code: Select all

a.s = "Hi" + Chr(0) + " Bye"
Debug Chr(PeekC(@a))
Debug Chr(PeekC(@a+1))
Debug Chr(PeekC(@a+2))
Debug Chr(PeekC(@a+3))
Debug Chr(PeekC(@a+4))
Debug Chr(PeekC(@a+5))
Debug Chr(PeekC(@a+6))

Code: Select all

a.s = "Hi Bye"
Debug Chr(PeekC(@a))
Debug Chr(PeekC(@a+1))
Debug Chr(PeekC(@a+2))
Debug Chr(PeekC(@a+3))
Debug Chr(PeekC(@a+4))
Debug Chr(PeekC(@a+5))
Debug Chr(PeekC(@a+6))
Sorry for my bad english.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Null char in string

Post by rsts »

Peyman wrote:thanks Gaman and rsts good idea so we just can peek char by char to end of memory because we cant find end of string but i think this problem comes from strings value in purebasic not PokeS and PeekS, i think purebasic truncate strings till a null char then allocate it :
That makes sense, since "Hi" + Chr(0) + " Bye" would be invalid for any string operations. They would have to do something unusual (see my pokeS above) to even allocate it.

cheers
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Null char in string

Post by Kaeru Gaman »

this is self explaning:
since native strings are NULL-terminated, no native string operation could produce a string that carries a NULL somewhere in the middle.

if you manage somehow, you will mess up the process heap where the stringpool is located, producing a memory leak.

so, only mess around with strings containing NULLs on self allocated Memory, do not try to put NULLs into native strings.
oh... and have a nice day.
Peyman
Enthusiast
Enthusiast
Posts: 203
Joined: Mon Dec 24, 2007 4:15 pm
Location: Iran

Re: Null char in string

Post by Peyman »

thanks all so this is better i dont use null char in string or memory buffer and for get it i can get char by char from memory.

Thanks All.
Sorry for my bad english.
Post Reply