Page 1 of 1
Debugger works not correct with Null pointer strings
Posted: Wed May 18, 2011 1:25 pm
by IceSoft
Code: Select all
Define mystring.s
Debug @""
Debug @mystring
Debug mystring ; <<<<<<<< An error message about a null pointer variable is expected but this is handles as an empty string variable ("")
Re: Debugger works not correct with Null pointer strings
Posted: Wed May 18, 2011 3:45 pm
by Trond
Huh? Null pointer strings are valid strings in PureBasic. There is not supposed to be an error message.
Re: Debugger works not correct with Null pointer strings
Posted: Wed May 18, 2011 7:33 pm
by freak
Trond wrote:Huh? Null pointer strings are valid strings in PureBasic. There is not supposed to be an error message.
correct.
Re: Debugger works not correct with Null pointer strings
Posted: Wed May 18, 2011 8:05 pm
by IceSoft
You can find the whole discussion here (Sorry its german):
http://www.purebasic.fr/german/viewtopi ... 20&t=24299
Re: Debugger works not correct with Null pointer strings
Posted: Thu May 19, 2011 5:42 pm
by freak
This is the intended behavior. Strings are only initialized when something is assigned to them. A nullpointer is always interpreted as the empty string internally. This is how it was designed from the start.
Re: Debugger works not correct with Null pointer strings
Posted: Thu May 19, 2011 6:00 pm
by skywalk
freak wrote:This is the intended behavior. Strings are only initialized when something is assigned to them. A nullpointer is always interpreted as the empty string internally. This is how it was designed from the start.
I noticed this behavior when I try to fill an uninitialized string variable passed to a procedure ByRef.
I remember getting an IMA. It was annoying for sure.
Meaning, I had to define the string and then assign it to a "" before I could modify it in the procedure.
Is this correct?
Code: Select all
Procedure EditStr(*p)
PokeS(*p,"Hello")
ProcedureReturn Len(PeekS(*p))
EndProcedure
Define.s r2;="" ;<--- remove comment and it works
Debug EditStr(@r2)
Re: Debugger works not correct with Null pointer strings
Posted: Thu May 19, 2011 6:15 pm
by ts-soft
skywalk wrote:Is this correct?
Code: Select all
Procedure EditStr(*p)
PokeS(*p,"Hello")
ProcedureReturn Len(PeekS(*p))
EndProcedure
Define.s r2;="" ;<--- remove comment and it works
Debug EditStr(@r2)
It works sometimes with removed comment but it is not correct. You poke in a not reserved memory. Enable purifier
to see that it not works!
Greetings - Thomas
Re: Debugger works not correct with Null pointer strings
Posted: Thu May 19, 2011 6:31 pm
by skywalk
ts-soft wrote:It works sometimes with removed comment but it is not correct. You poke in a not reserved memory. Enable purifier to see that it not works!
True, I left out the other stuff in the procedure for simplicity.
When you say sometimes, I am curious.
What amount of memory is reserved when r2 = ""?
Is there a minimum?
Code: Select all
Procedure EditStr(*p)
*p = AllocateMemory(RLen)
; do stuff...
PokeS(*p,"Hello")
FreeMemory(*p)
EndProcedure
Define.s r2;="" ;<--- remove comment and it works
Debug EditStr(@r2)
Re: Debugger works not correct with Null pointer strings
Posted: Thu May 19, 2011 7:24 pm
by ts-soft
skywalk wrote:What amount of memory is reserved when r2 = ""?
There is only place for the nullbyte, so you can only poke a empty string

Re: Debugger works not correct with Null pointer strings
Posted: Thu May 19, 2011 7:55 pm
by skywalk
Ok, that's what I thought, so I rearranged the Procedure to return the string directly instead of modifying a ByRef parameter. VB6 handled ByRef strings behind the scenes for me.
Re: Debugger works not correct with Null pointer strings
Posted: Thu May 19, 2011 9:08 pm
by PMV
PB can only handle the string for you, if you are using normal PB-Strings.
PB ignores memory-functions like PokeS().
Code: Select all
Procedure EditStr(*p)
Protected *s.STRING = AllocateMemory(SizeOf(STRING))
PokeI(*s, *p)
*s\s = "Hello"
FreeMemory(*s)
ProcedureReturn Len(PeekS(*p))
EndProcedure
Define.s r2=""
Debug EditStr(@r2)
Debug r2
Re: Debugger works not correct with Null pointer strings
Posted: Fri May 20, 2011 10:53 am
by helpy
There is nothing comparable in PureBasic like "ByRef VariableName As String" in Visual Basic (or maybe other programming languages)!
Yes! You can pass a pointer to a PureBasic string to a procedure!
Yes! You can read the string in such a procedure!
Yes! You can change the string in such a procedure,
if the string is not getting longer as the original one!
No! You can not handle the string like normal string variables in such a procedure.
No! You cannot use PureBasic string functions in such a Procedure.
The only way you can pass strings ByRef is, if they are inside Structures. See example:
Code: Select all
Procedure AppendString( *First.String, *Second.String )
*First\s + *Second\s
EndProcedure
Procedure EmptyString( *aString.String )
*aString\s = ""
EndProcedure
First.String
Second.String
First\s = "Hello"
Debug First\s
Second\s = ", this was appended to the orignial string without any problems :-)"
AppendString( First, Second )
Debug First\s
Second\s = #CRLF$ + #CRLF$ + "Und noch mehr" + RSet("",1024,"-")
AppendString( First, Second )
Debug First\s
EmptyString( First )
Debug #DQUOTE$ + First\s +#DQUOTE$
Hope this helps!
cu, guido