Page 1 of 1
Null char in string
Posted: Sun Dec 13, 2009 3:34 am
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
Re: Null char in string
Posted: Sun Dec 13, 2009 3:59 am
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.
Re: Null char in string
Posted: Sun Dec 13, 2009 4:15 am
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...
Re: Null char in string
Posted: Sun Dec 13, 2009 4:39 am
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
Re: Null char in string
Posted: Sun Dec 13, 2009 12:46 pm
by Thorium
PokeS has a length parameter.
That should ignore the zero, but i didnt tested it.
Re: Null char in string
Posted: Sun Dec 13, 2009 2:55 pm
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))
Re: Null char in string
Posted: Sun Dec 13, 2009 3:20 pm
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
Re: Null char in string
Posted: Sun Dec 13, 2009 5:40 pm
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.
Re: Null char in string
Posted: Wed Dec 16, 2009 5:01 am
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.