Confusion over passing by reference vs passing by value - Printable Version +- QB64 Phoenix Edition (https://qb64phoenix.com/forum) +-- Forum: Chatting and Socializing (https://qb64phoenix.com/forum/forumdisplay.php?fid=11) +--- Forum: General Discussion (https://qb64phoenix.com/forum/forumdisplay.php?fid=2) +--- Thread: Confusion over passing by reference vs passing by value (/showthread.php?tid=559) Pages:
1
2
|
Confusion over passing by reference vs passing by value - hanness - 06-16-2022 Let's say that I have the following code: Code: (Select All) Option _Explicit This code has 4 main sections: Section 1: We set as variable "x" to 1 and pass it to a subroutine by reference to the variable "y". The subroutine adds one to the value of y. As expected, when we come out of the subroutine we find that the original variable "x" is now equal to 2. Section 2: We set as variable "x" to 1 and pass it to a subroutine by value to the variable "y". The subroutine adds one to the value of y. As expected, when we come out of the subroutine we find that the original variable "x" is unchanged because we passed the variable by value rather than by reference. But what about strings? In section 3 and 4, I'm doing the same thing but with a string. I pass it to a subroutine both by reference and by value but in both cases the original string variable is changed. Is this expected behavior? Is it possible to pass a string variable without affecting the original? I know that I could work around it by assigning the variable to another variable within the subroutine like this: Code: (Select All) Sub Test2 (b As String) This way, I'm not modifying the original string. I just wanted to make sure I'm not missing something obvious. RE: Confusion over passing by reference vs passing by value - DSMan195276 - 06-17-2022 To be honest it looks like a bug to me. I checked the behavior in QB45 to be sure, and using () to pass a string by value works as expected. Additionally it seems extra bugged because doing `(a$ + "")` also does not properly pass by value, and neither does `a$ + ""` (both of which pass by value in QB45). Functionally though the problem is just that it is not being properly copied before calling the function, so they're all basically the same issue. Edit: I created a GitHub issue for this: https://github.com/QB64-Phoenix-Edition/QB64pe/issues/116 RE: Confusion over passing by reference vs passing by value - SMcNeill - 06-17-2022 This is expected as strings are *always* passed by offsets. The solution is as you have found -- assign to a temp variable and don't modify the original. RE: Confusion over passing by reference vs passing by value - hanness - 06-17-2022 (06-17-2022, 03:07 AM)SMcNeill Wrote: This is expected as strings are *always* passed by offsets. The solution is as you have found -- assign to a temp variable and don't modify the original. Ah, very good. I wasn't aware that there was a difference between how the strings and numerical variables were handled in this regard. I learn new things every day. The trick is in trying to remember all of it :-) RE: Confusion over passing by reference vs passing by value - DSMan195276 - 06-17-2022 Quote:Ah, very good. I wasn't aware that there was a difference between how the strings and numerical variables were handled in this regard. FWIW I think that Steve and I have different opinions on this They're intended to all work the same, as they did in QB45 (there's a reason the Wiki doesn't say anything about strings and stuff working differently ). But that said it seems that after some investigation QB64 has significant problems regarding pass by value behavior for anything not "simple" like integers. I think it should be a bug considered for fixing, but either way it's probably best to avoid it for now. RE: Confusion over passing by reference vs passing by value - SMcNeill - 06-17-2022 (06-17-2022, 02:21 PM)DSMan195276 Wrote:Quote:Ah, very good. I wasn't aware that there was a difference between how the strings and numerical variables were handled in this regard. I don't think we're of different opinions -- I agree 100% with the statement that the way we handle it is currently bugged. My opinion on the issue however is: I'm not smart enough to sort out how the BLEEP to fix the problem, so it's best -- for now -- for folks to learn to avoid the possible pitfalls that can come from using the glitchy "pass via parentheses" method. Personally, I'd love to see the issue fixed. I just couldn't sort out how to fix it myself, so until it does get fixed, I figure it's best to just warn folks to be very careful with such code and to use temp or STATIC variables when possible to avoid the issue completely. RE: Confusion over passing by reference vs passing by value - madscijr - 06-18-2022 As Steve said, using temp variables is the tried and true simple solution. For more complex data structures like arrays of user-defined types, you could make copies of just the values you need to use, or if you want to get fancy, you could get into memcopy (spriggsy knows about that stuff), though I am more comfortable with keeping things simple where possible. RE: Confusion over passing by reference vs passing by value - Kernelpanic - 06-19-2022 Why is the transfer of the sentence "By value" unsuccessful? Row 48 -Satztest3 (satz3())- generates a compiler error. Code: (Select All) 'Unterschiede bei Uebergabe einer Variablen, Zahl und String Quote:internal\c\c_compiler\bin\c++.exe -w -DGLEW_STATIC -DFREEGLUT_STATIC -DDEPENDENCY_NO_SOCKETS -DDEPENDENCY_NO_PRINTER -DDEPENDENCY_NO_ICON -DDEPENDENCY_NO_SCREENIMAGE internal\c/qbx.cpp -c -o internal\c/qbx.o RE: Confusion over passing by reference vs passing by value - DSMan195276 - 06-19-2022 Unfortunately a lot of cases involving pass-by-value don't work correctly. The specific one you ran into I made a GitHub issue about a couple days ago: https://github.com/QB64-Phoenix-Edition/QB64pe/issues/118 RE: Confusion over passing by reference vs passing by value - Kernelpanic - 06-19-2022 (06-19-2022, 04:18 PM)DSMan195276 Wrote: Unfortunately a lot of cases involving pass-by-value don't work correctly. The specific one you ran into I made a GitHub issue about a couple days ago: Quote:How the copy happens depends on what kind of array it is - for most arrays the backing buffer can just be copied directly, for variable length strings the qbs objects need to be copied individually. Also not as one sentence. Code: (Select All) Print |