Debugger works not correct with Null pointer strings

Just starting out? Need help? Post your questions and find answers here.
User avatar
IceSoft
Addict
Addict
Posts: 1690
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Debugger works not correct with Null pointer strings

Post 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 ("")
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: Debugger works not correct with Null pointer strings

Post by Trond »

Huh? Null pointer strings are valid strings in PureBasic. There is not supposed to be an error message.
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Debugger works not correct with Null pointer strings

Post by freak »

Trond wrote:Huh? Null pointer strings are valid strings in PureBasic. There is not supposed to be an error message.
correct.
quidquid Latine dictum sit altum videtur
User avatar
IceSoft
Addict
Addict
Posts: 1690
Joined: Thu Jun 24, 2004 8:51 am
Location: Germany

Re: Debugger works not correct with Null pointer strings

Post by IceSoft »

You can find the whole discussion here (Sorry its german):
http://www.purebasic.fr/german/viewtopi ... 20&t=24299
Belive! C++ version of Puzzle of Mystralia
Bug Planet
<Wrapper>4PB, PB<game>, =QONK=, PetriDish, Movie2Image, PictureManager,...
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: Debugger works not correct with Null pointer strings

Post 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.
quidquid Latine dictum sit altum videtur
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Debugger works not correct with Null pointer strings

Post 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)
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Debugger works not correct with Null pointer strings

Post 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
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Debugger works not correct with Null pointer strings

Post 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)       
Last edited by skywalk on Thu May 19, 2011 7:57 pm, edited 1 time in total.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: Debugger works not correct with Null pointer strings

Post 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 :wink:
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Debugger works not correct with Null pointer strings

Post 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.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: Debugger works not correct with Null pointer strings

Post 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
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Debugger works not correct with Null pointer strings

Post 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
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Post Reply