methods of accessing bytes by pointer

General FreeBASIC programming questions.
Munair
Posts: 345
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

methods of accessing bytes by pointer

Postby Munair » Dec 07, 2017 11:13

Is there any (performance) difference between these two methods:

Code: Select all

dim s as zstring ptr
dim t as string

t = space(2)
s = strptr(t)
*s = "O"         ' METHOD 1
s[0] = "O"       ' METHOD 2
*(s + 1) = "K"   ' METHOD 1
s[1] = "K"       ' METHOD 2
print t
sleep
end
fxm
Posts: 7474
Joined: Apr 22, 2009 12:46
Location: Paris suburb, FRANCE

Re: methods of accessing bytes by pointer

Postby fxm » Dec 07, 2017 12:25

The pointer index syntax ' p[n] ' is just a shorthand for ' *(p + n) ', so there is no difference in the generated codes.
Munair
Posts: 345
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: methods of accessing bytes by pointer

Postby Munair » Dec 07, 2017 12:29

OK, thanks.
Munair
Posts: 345
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: methods of accessing bytes by pointer

Postby Munair » Dec 07, 2017 13:58

Hmm. FB seems to do funny things when reading writing bytes. When using pointers then ASC and CHR seem interchangable, at least on assignment. When accessing a string byte directly, only ASC is allowed. Consider the follwing code:

Code: Select all

dim t as string = space(3)
dim p as zstring ptr = strptr(t)
dim a as ubyte
dim c as string * 1

p[0] = "F"
p[1] = 111
p[2] = 111
print t

a = p[0] ' OK
c = p[0] ' OK

a = t[0] ' OK
c = t[0] ' NOT OK

' *** BUT ***
select case p[0]
   case &h41 to &h5A ' NOT OK
      print "caps"
end select

select case asc(p[0])
   case &h41 to &h5A '  OK
      print "caps"
end select

select case t[0]
   case &h41 to &h5A '  OK
      print "caps"
end select
end
Treating dereferenced pointers as characters and string bytes as values seems like the safe way to go.
MrSwiss
Posts: 2100
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: methods of accessing bytes by pointer

Postby MrSwiss » Dec 07, 2017 15:18

Munair wrote:ASC and CHR seem interchangable
They are complementary but, NOT interchangeable!
Asc("A") = 65
Chr(65) = "A"
Munair
Posts: 345
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: methods of accessing bytes by pointer

Postby Munair » Dec 07, 2017 15:26

MrSwiss wrote:
Munair wrote:ASC and CHR seem interchangable
They are complementary but, NOT interchangeable!
Asc("A") = 65
Chr(65) = "A"

I know that. But in my example it doesn't matter which one is assigned to the dereferenced pointer, whereas it does matter which one is used for Select Case (and probably If...Then).
MrSwiss
Posts: 2100
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: methods of accessing bytes by pointer

Postby MrSwiss » Dec 07, 2017 15:31

It's IMHO, high on time, that you start, using the FB-Manual, instead of: spamming the forum!
FB-Manual (online)
St_W
Posts: 1166
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: methods of accessing bytes by pointer

Postby St_W » Dec 07, 2017 15:39

You're using two different types of the [] indexing operator. One time you use string indexing (which returns an UBYTE) and the other time you use pointer indexing (which returns a ZSTRING in this case, because you are using a ZSTRING PTR).

In your example you can also try this:

Code: Select all

#print typeof(p[0])
#print typeof(t[0])

see also:
https://freebasic.net/wiki/wikka.php?wa ... tringIndex
https://freebasic.net/wiki/wikka.php?wa ... OpPtrIndex
Munair
Posts: 345
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: methods of accessing bytes by pointer

Postby Munair » Dec 07, 2017 15:43

MrSwiss wrote:It's IMHO, high on time, that you start, using the FB-Manual, instead of: spamming the forum!
FB-Manual (online)

What you call spamming may be valuable information for other trying to find similar answers that are not covered (clearly) by the manual. IF you can point me to a description for the above behaviour that you seem to avoid answering, I'd be happy to read it thrice. Thank you.
Last edited by Munair on Dec 07, 2017 15:50, edited 1 time in total.
Munair
Posts: 345
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: methods of accessing bytes by pointer

Postby Munair » Dec 07, 2017 15:46

St_W wrote:You're using two different types of the [] indexing operator. One time you use string indexing (which returns an UBYTE) and the other time you use pointer indexing (which returns a ZSTRING in this case, because you are using a ZSTRING PTR).

In your example you can also try this:

Code: Select all

#print typeof(p[0])
#print typeof(t[0])

see also:
https://freebasic.net/wiki/wikka.php?wa ... tringIndex
https://freebasic.net/wiki/wikka.php?wa ... OpPtrIndex
Yes, I'm aware of the type difference. But it's interesting that p[o] still accepts values instead of only characters as I demonstrated in the example. But when testing with Select Case, an explicit convergence to Asc is required. That seems a bit of an inconsistency to me.
Last edited by Munair on Dec 07, 2017 17:05, edited 1 time in total.
MrSwiss
Posts: 2100
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: methods of accessing bytes by pointer

Postby MrSwiss » Dec 07, 2017 15:52

Munair
Posts: 345
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: methods of accessing bytes by pointer

Postby Munair » Dec 07, 2017 16:02

MrSwiss wrote:See: Indexing Operators

Doesn't answer the behaviour I described. FYI: I have the most recent manual here on my hard disk and I use it a lot.( ;) )
St_W
Posts: 1166
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: methods of accessing bytes by pointer

Postby St_W » Dec 07, 2017 16:05

Munair wrote:What you call spamming may be valuable information for other trying to find similar answers that are not covered by the manual. IF you can point me to a description for the above behaviour that you seem to avoid answering, I'd be happy to read it thrice. Thank you.
Although I'd never say it in a way/tone like MrSwiss does, most of the information is indeed in the manual. Have a look at the two links I've posted previously.

The only thing that I cannot answer either (and that I couldn't find it in the manual) is the type conversion error for the select case. Because - as you've shown - the assignment to UBYTE works. It would be indeed interesting why the implicit conversion is applied for the assignment, but not for the select case. I only know the following page in the manual which describes implicit conversion, but does not mention strings:
https://www.freebasic.net/wiki/wikka.ph ... Conversion
It should be probably extended to also document implicit conversion behavior for other FB native datatypes.
Last edited by St_W on Dec 07, 2017 16:11, edited 1 time in total.
Munair
Posts: 345
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: methods of accessing bytes by pointer

Postby Munair » Dec 07, 2017 16:10

St_W wrote:The only thing that I cannot answer either (and that I couldn't find it in the manual) is the type conversion error for the select case. Because - as you've shown - the assignment to UBYTE works. It would be indeed interesting why the implicit conversion is applied for the assignment, but not for the select case.
That's exactly why I posted it. Not sure why it is considered spamming. :-)

Again, the observation wasn't about the type differences...
MrSwiss
Posts: 2100
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: methods of accessing bytes by pointer

Postby MrSwiss » Dec 07, 2017 16:13

Select Case, a specific convergence to Asc is required.
No, it just requires to write the "case" differently:

Code: Select all

' select case (using string, instead of numeral)
Select Case tstr
Case "Yes" : ...
Case "No"  : ...
Case Else : Print "not implemented"
End Select
Attention: you cannot use: 'Select Case As Const' with string! (numeric accelerator only)
Not sure why it is considered spamming. :-)
Maybe, because you opened 7 Threads, within 48 hours ???

Return to “General”

Who is online

Users browsing this forum: MrSwiss and 7 guests