Page 1 of 1

Bug with pointer to String

Posted: Wed Aug 08, 2007 6:46 pm
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).

Posted: Wed Aug 08, 2007 6:52 pm
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..

Re: Bug with pointer to String

Posted: Wed Aug 08, 2007 8:17 pm
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)

Posted: Thu Aug 09, 2007 4:27 am
by byo
No problem here.

What should happen with that snippet of yours?

Posted: Fri Aug 10, 2007 7:21 pm
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...

Posted: Fri Aug 10, 2007 10:39 pm
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

Posted: Tue Aug 14, 2007 10:37 am
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.

Posted: Fri Aug 17, 2007 5:07 pm
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.

Posted: Thu Aug 23, 2007 7:34 pm
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.