Pass by value of "big" parameter(s) ?

General FreeBASIC programming questions.
Post Reply
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Pass by value of "big" parameter(s) ?

Post by dodicat »

I have asked this before, regarding byval udt's
------------
From the help file:
"ByRef is better for passing huge objects like strings or big UDTs that should not be copied"
--------------
A warning is given (18) when passing "big" UDT's, it looks like "big" is when:
64 bit compiler, > 512 bytes
32 bit compiler, > 256 bytes
example

Code: Select all


#ifdef __FB_64BIT__ 
#define sz 513
#else
#define sz 257
#endif


type udt
    as zstring * sz z
end type

function dothis(byval u as udt) as udt
      print sizeof(u)
      return u
end function

dim as udt u=("hello")

 print dothis(u).z
 
sleep  
Could passing a big udt (giving the warning) cause a runtime error, i.e. are the 512/256 bytes the span of allocated memory for byval udt's, or is it simply advising that passing byval udt's always needs copying, but copying is really no problem?
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Pass by value of "big" parameter(s) ?

Post by fxm »

fxm wrote: Dec 12, 2017 17:36 There are 2 thresholds to activate this kind of compiler warning on the parameter(s) declared 'As ByVal', when compiling with '-w pedantic' option (presently not noted in documentation):
  • Parameter length > 16 bytes (fbc 32-bit) or 32 bytes (fbc 64-bit)
    => warning 17: The type length is too large, consider passing BYREF
  • Parameters list length > 256 bytes (fbc 32-bit) or 512 bytes (fbc 64-bit)
    => warning 18: The length of the parameters list is too large, consider passing UDT's BYREF
But in both cases, it's just a warning: obviously 'ByRef' is not imposed by the compiler!
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Pass by value of "big" parameter(s) ?

Post by fxm »

There is no limitation here on the size of memory (in the stack) used to pass the "big" parameter(s) by value. This is just a warning.

Alternatively, maybe use 'Byref x As Const datatype' instead (my favorite solution).

Copying the "big" parameters and passing them by reference only makes sense if the copy is not in the stack but in the heap (no more limitation due to stack size).
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Pass by value of "big" parameter(s) ?

Post by dodicat »

It is a very weak constant though.

Code: Select all

#cmdline "-w pedantic"
Type udt
    As zstring * 500 z
End Type


Sub dothis(byref u As Const udt)
    *Cptr(udt Ptr,@u).z="Goodbye, Press a key to end . . ."
End Sub


Dim As udt u=("Hello")
Print u.z
dothis(u)
Print u.z
Sleep  
But thanks for looking into this.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Pass by value of "big" parameter(s) ?

Post by fxm »

dodicat wrote: Nov 15, 2022 23:36 It is a very weak constant though.
No because you are forced to hack the variable type to bypass the CONST declaration.
fxm
Moderator
Posts: 12106
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Pass by value of "big" parameter(s) ?

Post by fxm »

Otherwise:

Code: Select all

#cmdline "-w pedantic"
Type udt
    As zstring * 500 z
End Type


Sub dothis(byref u As udt)
    print u.z
    u.z="Goodbye, Press a key to end . . ."
    print u.z
End Sub


Dim As udt u=("Hello")
Dim As udt ptr pu = New udt(u)
dothis(*pu)
Delete pu
Print u.z
Sleep
Post Reply