Structured pointer problem

Just starting out? Need help? Post your questions and find answers here.
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Structured pointer problem

Post by Trond »

What is wrong with this code?

Code: Select all

AString.s = "Apple pie"
*ZString.String = @AString
Debug *ZString\s
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Lots of pointers there.

Is Debug PeekS(*ZString) not the way?


How/why would you use something like that, rather than straightforward strings? In other words, what trick or tip do you have up your sleeve? :)
@}--`--,-- A rose by any other name ..
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Structured pointer problem

Post by blueznl »

Trond wrote:What is wrong with this code?

Code: Select all

AString.s = "Apple pie"
*ZString.String = @AString
Debug *ZString\s
dunno, well, isn't *zstring pointing to the array descriptor instead of the string itself?

haven't tried it but ueh, perhaps debug peeks(*zstring\s)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

But peeks(*zstring\s) would be (in theory, anyhow) trying to use a string as a number.

Either Trond's
.. Debug *Zstring\s (which fails, with invalid memory address)
or
.. Debug PeekS(*Zstring)

But what has me curious is what is the advantage of using this at all? :)
@}--`--,-- A rose by any other name ..
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

If you're going to use pointers, you should stick with pointer logic. That means knowing the datatype you are after and identifying its offset in the structure:

Code: Select all


AString.s = "Apple pie" 
*ZString = @AString 
Debug PeekS(*zstring+OffsetOf(string\s))

But what has me curious is what is the advantage of using this at all?
You find a significant speed advantage with a heavy workload as it's all direct memory access. (it's more impressive with more complicated structures for sure)
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Dare2 wrote:But what has me curious is what is the advantage of using this at all? :)
I want to cast the address of a string to a string to avoid the speed penalty of a function call to PeekS().

Edit: Something like this, just a bit more elegant.

Code: Select all

String.s = "Apple pie"
Address = @String
*StringPtr.s

!mov eax, [v_Address]
!mov [p_StringPtr], eax

Debug *StringPtr
Last edited by Trond on Sat Mar 11, 2006 9:22 pm, edited 1 time in total.
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

That is functionally the same as PeekS(*ZString) as the offset is 0. :) If the offset was not zero, it might be a problem as the address assignment was to *ZString and therefore the first 4 bytes contains the address of the string.

Too many pointers. :)

Trond's way should work, IMO. PeekS(*ZString) does work, so does the offset approach (being 0).

But, Trond, why? You have a trick you're working on?

Edit: Ah. (Posted same time)
@}--`--,-- A rose by any other name ..
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

avoid the speed penalty of a function call to PeekS()
If you can manage that I'd like to know how, but afaik you can't get something for nothing.
That is functionally the same as PeekS(*ZString) as the offset is 0
Yes, but I'm just showing the generic way to get data from structures using pointers. Most real-world applications have more complicated structures.
BERESHEIT
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

netmaestro wrote:If you can manage that I'd like to know how, but afaik you can't get something for nothing.
Life's a begger that way. :)
@}--`--,-- A rose by any other name ..
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Code: Select all

AString.s = "Apple pie" 
*ZString.String = @AString 
Debug PeekS(*ZString) ; This should be the same as
Debug *ZString\s      ; this, since the offset is 0.
                      ; By using this structured pointer a lot of time can
                      ; saved since we don't have to call PeekS(), which in
                      ; turn most likely creates a temporary string buffer
                      ; (it's not simply a cast).
                      ; But, for some reason, the version without PeekS()
                      ; doesn't work. I thought at first it was a bug, but
                      ; I didn't want to say anything about that since I've
                      ; always used PeekS() before and I'm not much into using
                      ; pointers this way.
                      ; Now I think it's a bug.
netmaestro wrote:
avoid the speed penalty of a function call to PeekS()
If you can manage that I'd like to know how, but afaik you can't get something for nothing.
You can do it like this, but I thought that maybe there was a more elegant way using a structured pointer. Remember to declare the destination string as *pointer to a string, not a string, or you'll get som hard to trace memory leaks.

Code: Select all

PointerToString.l = @"Apple Pie"
*StringPtr.s 

!mov eax, [v_PointerToString]
!mov [p_StringPtr], eax 

Debug *StringPtr
That is functionally the same as PeekS(*ZString) as the offset is 0
Yes, but I'm just showing the generic way to get data from structures using pointers. Most real-world applications have more complicated structures.[/quote]I thought this structure was made exclusively for this very purpose, a speedup gain from avoiding PeekS(), so it would be as real-world as neccessary already?
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Truth is, I think your original way should work (Debug *ZString\s).

Just wondering now if assigning addresses to the string structure and then accessing the string would be faster than PeekS. Can't really tell because can't test. :)
@}--`--,-- A rose by any other name ..
Nik
Addict
Addict
Posts: 1017
Joined: Fri May 13, 2005 11:45 pm
Location: Germany
Contact:

Post by Nik »

Code: Select all

Structure ComplexStruct
Val1.q
Val2.d
Val3.f
Val4.l
Val5.s
EndStructure

Procedure.l DoSthWithStruct(*PStruct.ComplexStruct)
Debug *PStruct\Val1
Debug *PStruct\Val2+1.4
Debug *PStruct\Val2 ;* *PStruct\Val3
Debug *PStruct\Val5
EndProcedure


MyStruct.ComplexStruct
With MyStruct
\Val1=1858485
\Val2=1.6
\Val3=2.0
\Val5="Hallo Welt!"
EndWith

DoSthWithStruct(@MyStruct)
This is how to deal with Structures in Memory the smart way, no need to use Offset of in simple cases like this, it's only needed with mor complicated functions for example when sorting a structured List.

Code: Select all

AString.s = "Apple pie"
ZString.String
ZString\s=AString.s
*PString.String=@ZString
Debug *PString\s
works too!
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Nik wrote:

Code: Select all

AString.s = "Apple pie"
ZString.String
ZString\s=AString.s
*PString.String=@ZString
Debug *PString\s
works too!
It does work, but it's not much faster than using PeekS() since you do a copy of the string when you do ZString\s=AString.s. I wanted to cast a long to a string, and don't copy it.
Just wondering now if assigning addresses to the string structure and then accessing the string would be faster than PeekS. Can't really tell because can't test.
Definetely copying a pointer then accessing the string from the pointer would be faster than copying the entire string:

Code: Select all

AString.s = "Apple pie" 

#Tries = 10000000

; Fast way of doing it (don't copy the string, just move the pointer)
time = GetTickCount_()
For I = 0 To #Tries
  !mov  edx, [v_AString]
  !mov  [p_InSane], edx
  *InSane.s
Next
MessageRequester("", Str(GetTickCount_()-time))

; PeekS() way of doing it
time = GetTickCount_()
For I = 0 To #Tries
  *InSane.s = PeekS(@AString)
Next
MessageRequester("", Str(GetTickCount_()-time))
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Trond wrote:Definetely copying a pointer then accessing the string from the pointer would be faster than copying the entire string
Ding ding. :? Quite right. :)
@}--`--,-- A rose by any other name ..
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Too many pointers. This help, though?

Code: Select all

Structure tooMany
  StructureUnion
    Zstring.string
    ptr.l
  EndStructureUnion
EndStructure

  
test.tooMany
test\ptr = AllocateMemory(20)
PokeS(test\ptr,"Peaches and Pears")

Debug test\Zstring\s

FreeMemory(test\ptr)
Glad you started this discussion, because now I see advantage. :)
@}--`--,-- A rose by any other name ..
Post Reply