Qbasicnews.com

Full Version: strings not being passed correctly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
this is kind of strange, but i sent strings to a sub, and it's not receiving them correctly... it does sometimes, but not consistently. the strings are hardcoded. when i changed the strings from byval to just nothing, it worked fine. (except it changed my original string Cry ) compiling with -e didn't give any runtime errors. any ideas? :???:
Quote:this is kind of strange, but i sent strings to a sub, and it's not receiving them correctly... it does sometimes, but not consistently. the strings are hardcoded. when i changed the strings from byval to just nothing, it worked fine. (except it changed my original string Cry ) compiling with -e didn't give any runtime errors. any ideas? :???:

Give me code example, I shall test it too.
Only if we can reproduce it, gimme some code :P

A string passed by value won't be copied to the stack, it's just a pointer to a zstring, if you change it you will be changing the original and if the original was a literal-string, it will be screwed up.

Also doing assignaments to the byval string can be dangerous as it is in C, the size is not known at compile-time, so the string must be big enough or memory will be overwritten.

Btw, is it using the multi-threaded runtime lib?
nope, not using multithreaded rtlib

Quote:A string passed by value won't be copied to the stack, it's just a pointer to a zstring, if you change it you will be changing the original and if the original was a literal-string, it will be screwed up.

that sounds like what i was doing, basically it was (not complete):

Code:
declare function mysub(byval a as string,byval b as string,byval c as string) as string
...
dim a as string,b as string="test"
a=mysub(b,"something","something else")
...
function mysub(byval a as string,byval b as string,byval c as string) as string
   b="("+b+")"
   if c="something else" then return b+a else return a+b
end function
You'll have to change the ByVal's to ByRef's so fbc won't puke.

Also, if you plan on working with the strings passed, but don't want the changes to take place beyond the routine, copy them to temporary local strings and work on those.

ie:
Code:
function mysub(byref a as string, byref b as string, byref c as string) as string

   dim tmpA as string
   dim tmpB as string
   dim tmpC as string

   tmpA = A
   tmpB = B
   tmpC = C

   tmpB = "(" + tmpB + ")"

   '' More local changes to strings we don't want to see outside of this routine

   if tmpC = "something else" then return tmpB + tmpA else return tmpA + tmpB

end function

It will add some overhead allocating these new strings and copying them, but if this was speed critical, you would be better to setup some unique integer constants and use those as conditionals, ie:

bad:
Code:
function mysub(byref a as string, byref b as string, byref c as string) as string

   if C = "this one" then
      return B
   elseif C = "that one" then
      return A
   elseif C = "the other one" then
      return C
   endif

end function

better:
Code:
#define THIS_ONE 1
#define THAT_ONE 2
#define THE_OTHER_ONE 3

function mysub(byval a as integer, byval b as integer, byval c as integer) as integer

   if C = THIS_ONE then
      return B
   elseif C = THAT_ONE then
      return A
   elseif C = THE_OTHER_ONE then
      return C
   endif

end function