Bug with pointer to String

Just starting out? Need help? Post your questions and find answers here.
ktamp
User
User
Posts: 16
Joined: Sun Jul 01, 2007 4:35 pm

Bug with pointer to String

Post by ktamp »

Code: Select all

s.s = "abcd"
*p.String = @s
PrintN(*p\s)
causes "Invalid memory access". The following works correctly:

Code: Select all

Define s.String
s\s = "abcd"
*p.String = @s
PrintN(*p\s)
Tested with PureBasic 4.10b2 (Windows) and 4.01 (Linux).
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

The first expression isn't valid, you have a pointer to a structure which contains a string. It's not directly a pointer to a string..
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Bug with pointer to String

Post by helpy »

ktamp wrote:

Code: Select all

s.s = "abcd"
*p.String = @s
PrintN(*p\s)
Try it this way:

Code: Select all

Structure MyString
	StructureUnion
		s.s
		p.l
	EndStructureUnion
EndStructure

Define s.s = "abcd"
Define p.MyString\p = @s

Debug(p\s)
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
byo
Enthusiast
Enthusiast
Posts: 635
Joined: Mon Apr 02, 2007 1:43 am
Location: Brazil

Post by byo »

No problem here.

What should happen with that snippet of yours?
ktamp
User
User
Posts: 16
Joined: Sun Jul 01, 2007 4:35 pm

Post by ktamp »

I pass a pointer-to-string to a procedure and I want to use standard string procedures on a part of that string without having to duplicate it.

Why isn't the expression valid? It should work since e.g. the following does:

Code: Select all

n = 100
*p.Long = @n
PrintN(*p\l)
The tricky thing about strings is that they are in fact pointers themselves...
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

I try to explain it:

Code: Select all

n = 100
*p.Long = @n
PrintN(*p\l)
*p.Long is a Pointer to a Structure with a Long-Value. That means, that the pointer *p points to a memory location, where the data of the Structure Long is located. In this case the pointer *p points directly to the location of the Long Variable! ... and so you access the Long Value with *p\l

But if you have a Structure with a String in it ...

Code: Select all

Structure String
    s.s
EndStructure
... you must know, how strings in Structures are handled! If you try the following:

Code: Select all

aString.String
aString\s = "A String"
Debug SizeOf(aString)
==> You see that the size of the variable aString is 4 Byte ... The structure data contains the pointer to the string! You can verify this with the following test code:

Code: Select all

s.String
s\s = "A String"

Debug @s
Debug @s\s
Debug PeekS(@s\s)
@s ... returns the address of the memory location where the structure data is located! Because the element s.s is the only element in the data, @s points to the element s.s ... BUT there is the pointer to the string

@s\s returns the address where the string is located in the memory! With a different structure you can make this more clear:

Code: Select all

s.String
s\s = "A String"

Debug "@s ..... " + Str( @s )
Debug "@s\s ... " + Str( @s\s )
Debug "String at @s\s .... " + PeekS(@s\s)
Debug ""

Structure MyString
	StructureUnion
		s.s
		p.l
	EndStructureUnion
EndStructure

*p.MyString = @s

Debug "*p = " + Str(*p)
Debug "*p\p = " + Str(*p\p)
BUT with your code

Code: Select all

s.s = "abcd"
*p.String = @s
PrintN(*p\s)
... the pointer *p points directly to the string and NOT to a memory location which represents the structure STRING!

In your code *p\s will fetch the first four bytes of the string, which will be interpreted as the pointer to the string which is $64636261 (Long value of string "abcd") ... and now PureBasic tries to get string from memory location $64636261 ...

When I try to explain things like this I always recognize that I am not an english native speaker.

Hope I explained it so you can understand and it was a little help for you!

cu, helpy
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
ktamp
User
User
Posts: 16
Joined: Sun Jul 01, 2007 4:35 pm

Post by ktamp »

Thank you. It took me almost a day after reading your first post to realize that "@string" returns the pointer value of "string" itself rather than a pointer to "string". I suppose PB should have a standard structure like your "MyString" to compensate for this fact.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

ktamp wrote:Thank you. It took me almost a day after reading your first post to realize that "@string" returns the pointer value of "string" itself rather than a pointer to "string". I suppose PB should have a standard structure like your "MyString" to compensate for this fact.
That standard structure exists and is called String.
ktamp
User
User
Posts: 16
Joined: Sun Jul 01, 2007 4:35 pm

Post by ktamp »

Trond wrote:That standard structure exists and is called String.
Not quite, as "MyStructure" contains a StructureUnion. On the other hand it finally downed on me how I could do the same thing using String and Long:

Code: Select all

s.s = "abcd"
p.Long\l = @s
*s.String = @p
Changing p\l changes the position of *s\s.
Post Reply