methods of accessing bytes by pointer

General FreeBASIC programming questions.
Post Reply
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: methods of accessing bytes by pointer

Post by Munair »

Josep Roca wrote:> FreeBASIC also allows UBYTE = &h80 without any protest

Guess you mean BYTE...
Sorry, I actually meant UBYTE = &h100. Interestingly, "dim a as ubyte = 256" (result a = 0) gives a compiler warning: "Overflow in constant conversion", but "dim a as byte = 128" (result a = -128) seems perfectly fine, and "dim a as ubyte = -1"(result a = 255) also seems fine. In all cases bit shifting should go either with or without warning.
Josep Roca wrote:Isn't it easier just don't use const?
For pointers? Yes. Try this:

Code: Select all

dim as zstring ptr p
dim as const string s = "FreeBASIC"
p = strptr(s)
end
St_W
Posts: 1626
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: methods of accessing bytes by pointer

Post by St_W »

Josep Roca wrote:Isn't it easier just don't use const?
Yes, but the "issue" is that some of FB's internal functions return const datatypes.
Munair wrote:
St_W wrote:It could probably help if you could show as an actual use case (e.g. in your UTF8 code) and tell us exactly what you want to do and what doesn't work.
The issues are mentioned in this thread with code included. It is not that something doesn't work (anymore). It's about dealing with specific compiler limitations, which are covered in detail in this thread.
Maybe you could summarize the issues that are still unresolved, as the thread has become quite long and unclear.
Munair wrote:It shouldn't be an implicit conversion. Simply uncouple the pointer-const relationship.
So you would remove the feature altogether? Or introduce some non-strict compilation mode? I don't think that is a good idea as the const specification has a meaning and shouldn't be just ignored carelessly.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: methods of accessing bytes by pointer

Post by fxm »

St_W wrote:
Josep Roca wrote:Isn't it easier just don't use const?
Yes, but the "issue" is that some of FB's internal functions return const datatypes.
On this subject, a little quiz?
There is at least one FreeBASIC keyword that returns a constant data type (because I only know that one).
What is (are) it (they), and why?
Josep Roca
Posts: 564
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: methods of accessing bytes by pointer

Post by Josep Roca »

Maybe this one:

Code: Select all

Function f( ) ByRef As Const ZString
    '' This string literal (because statically allocated in memory) will be returned by reference, no copy will be created.
    Function = "abcd"
End Function
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: methods of accessing bytes by pointer

Post by fxm »

I was talking about FreeBASIC keywords (not a function from an example in documentation).
Obviously, with our own function, we do what we want.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: methods of accessing bytes by pointer

Post by dodicat »

...
Last edited by dodicat on Dec 12, 2017 13:36, edited 1 time in total.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: methods of accessing bytes by pointer

Post by fxm »

Enum is only a declaration and does not return anything.
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: methods of accessing bytes by pointer

Post by Munair »

Is there any (optimization) advantage in FUNCTION () AS CONST STRING over FUNCTION() AS STRING? In this example there seems to be no difference in behaviour:

Code: Select all

function t1() as const string
	dim s as string = "freebasic"
	return s
end function

function t2() as string
	dim s as string = "freebasic"
	return s
end function

dim s as string

s = t1()
s = ""

s = t2()
s = ""
end
In Pascal, const in procedure definitions can result in code optimization. Hence my question.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: methods of accessing bytes by pointer

Post by fxm »

Munair wrote:Is there any (optimization) advantage in FUNCTION () AS CONST STRING over FUNCTION() AS STRING?
The generated code is exactly the same.
The notion of constant datatype remains at the level of the only compiler.
Munair wrote:In Pascal, const in procedure definitions can result in code optimization.
To optimize the code of a function, it may be useful to return by reference (Byref As DataType), instead of returning by value (As DataType) by making a copy of the original.
To protect the original, so it is safer to return by constant reference (Byref As Constant DataType).
sancho3
Posts: 358
Joined: Sep 30, 2017 3:22

Re: methods of accessing bytes by pointer

Post by sancho3 »

fxm wrote:There is at least one FreeBASIC keyword that returns a constant data type (because I only know that one).
What is (are) it (they), and why?
The only one I can find that might fit is ermn. This is a pointer to a string with the name of the module in which an error occurred.
I suppose the keyword "const" also returns a constant.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: methods of accessing bytes by pointer

Post by fxm »

'Ermn' and 'Erfn' return non constant values (Zstring Ptr).
'Const' is a qualifier (not a function).
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: methods of accessing bytes by pointer

Post by Munair »

Although it doesn't return anything 'DATA' holds constant expressions, returned by READ. It is then converted into a variable. So I'd say READ.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: methods of accessing bytes by pointer

Post by fxm »

No, Read does not return anything.
The keyword I know really returns a constant value (not modifiable). This detail ('constant') is not noted in the documentation.
fxm
Moderator
Posts: 12107
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: methods of accessing bytes by pointer

Post by fxm »

In a previous post, MrSwiss proposed to pass a (non-constant) copy of the constant string instead of passing the constant string itself. So the copy could be modified (by error), but without any consequence on the original string.
Instead of declaring in procedure 'Byref s As Constant String', declare 'Byval s As String' and the copy will be implicitly done when passing the parameter, in a transparent way seen from the user:

Code: Select all

Type PChar As Zstring Ptr

Sub something1(Byref s As String)
  Dim As PChar pz
  pz = Strptr(s)
End Sub

'Sub something2(Byref s As Const String)
Sub something2(Byval s As String)
'  Dim As Const Zstring Ptr pz
  Dim As PChar pz
  pz = Strptr(s)
End Sub

Dim As String s1 = "string#1"
Dim As Const String s2 = "string#2"

Dim As Zstring * 10 zs1 = "ztring#1"
Dim As Const Zstring * 10 zs2 = "zstring#2"

something1(s1)
'something1(s2)   '' NOK, but anyway not safe code
something1(zs1)
'something1(zs2)  '' NOK, but anyway not safe code
something2(s1)
something2(s2)
something2(zs1)
something2(zs2)
Munair
Posts: 1286
Joined: Oct 19, 2017 15:00
Location: Netherlands
Contact:

Re: methods of accessing bytes by pointer

Post by Munair »

fxm wrote:No, Read does not return anything.
That's interesting.

Code: Select all

dim buffer as string
read buffer
Since the buffer can be modified, it must receive a copy of the data stored somehow. So how does FB do that?
Post Reply