SPONSORS:






User Tag List

Page 1 of 2 12 LastLast
Results 1 to 10 of 11
  1. #1
    Junior Member
    Join Date
    Jul 2002
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    pass mode of IN doesn\'t seem to work as expected.

    I am calling a function and passing it an array; the called function sorts the array for local use and then returns. The pass mode I am using for the array is "in" so that the sort won't be visible to the caller. But after the call, the caller sees the changed array! How does Silk define pass mode "in"? The documentation states
    "ArraySort modifies the array you pass in..."
    but I didn't expect this to happen after I specified it as an "in" argument. Underneath the covers the called functions must be passed a reference (or an address), but isn't this dangerous behavior?

    code snippet:
    [+] testcase Caller( ) appstate none
    [ ]
    [ ] const integer iASize = 10
    [ ] array [ iASize ] of integer aiMyArray
    [ ]
    [ ] aiMyArray = {10,8,6,4,2,1,3,5,7,9}
    [ ]
    [ ] print( " Before the call to foo, array is..." )
    [ ] PrintIt( aiMyArray )
    [ ]
    [ ] foo( aiMyArray )
    [ ]
    [ ] print( " After the call to foo, the array is..." )
    [ ] PrintIt( aiMyArray )
    [ ]
    [+] void foo( in array of integer aiMyArray )
    [ ]
    [ ] print( " " )
    [ ] print( " Sorting the array inside foo; the array's pass mode is IN." )
    [ ] print( " " )
    [ ] ArraySort( aiMyArray )
    [ ]
    [+] void PrintIt( in array of integer aiMyArray )
    [ ]
    [ ] integer i
    [ ] string s = " "
    [-] for i = 1 to ArraySize( aiMyArray )
    [ ]
    [ ] s += Str( aiMyArray[ i ] ) + ", "
    [ ]
    [ ] print( " " )
    [ ] print( s )
    [ ] print( " " )

    results:
    [ ] Before the call to foo, array is...
    [ ]
    [ ] 10, 8, 6, 4, 2, 1, 3, 5, 7, 9,
    [ ]
    [ ]
    [ ] Sorting the array inside foo; the array's pass mode is IN.
    [ ]
    [ ] After the call to foo, the array is...
    [ ]
    [ ] 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,



    ------------------
    a software developer interested in quality software
    a software developer interested in quality software

  2. #2
    Senior Member
    Join Date
    Jan 2002
    Location
    Des Moines, Iowa
    Posts
    289
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    Argument pass modes

    The following table describes the pass modes for function arguments.

    Pass mode Description
    in The default. Specifies an input argument, used when you only want to get the argument's value. Any changes made to this argument are not reflected outside of the function.
    out Specifies an output argument, used when you only want to set the argument's value. This argument is modified in a function and the new value is passed back out of the function when the function ends.
    inout Specifies an input and output argument, used when you want to get the argument's current value and you want the function to change that value and pass the new value out.


    ********************************************
    With the Pass mode of "IN" the value of the argument is only affected within the function using the value. If you don't send it back you can not make use of any changes you made to it.

    The Pass modes work very much like C language. You either want to send it back using return or declare the pass mode to be inout.

    Might I suggest that you look at using lists. These are Arrays with no ending bounds and have several functions already associated with them, including a ListSort function. They behave like arrays but don't have all the messy coding that goes with them.

    ------------------

  3. #3
    Senior Member
    Join Date
    Jul 2000
    Posts
    117
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    The quick answer is that while the behavior you're seeing is somewhat counterintuitive, it's most likely 'right'.

    Although I have no inside knowledge of how Segue implemented Arrays, in "real" programming languages (e.g. C/C++) arrays are pointers.

    Thus, unless you take steps to protect yourself (i.e. make a safe copy of the Array), you will be passing a pointer to the data and the data will be modified.

    As blacst suggests -- try Lists. Or, if order is important (and there is some implication in your question that it may be), take a look at Sets.

    I'd probably argue that a slight modification in the docs for Silk would be in order -- to call out the differences (assuming your observations and my assumptions are correct) when dealing with Array types.

    Hope this helps.

    ------------------

  4. #4
    Senior Member
    Join Date
    Aug 1999
    Location
    Cambridge, UK
    Posts
    470
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    I use lists all the time in preference to arrays - they are extremely flexible and easy to use.
    And what's more important, when you pass lists into a function, they obey the pass mode you've specified (unlike arrays it seems).
    Presumably if you're dealing with very large arrays/lists and are going for the best possible performance of your code, then arrays will be the best choice - but you will have to put in a lot of extra effort to use them exactly the way you want.

    ------------------

  5. #5
    Member
    Join Date
    Oct 2002
    Location
    Earth (I think!)
    Posts
    92
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    Uhm, I think she's asking...

    I don't expect my array to be changed after I call the function. But it is. The IN keyword is not working as described in the documentation.

    As already stated, IN implies that any changes the function makes to the parameter will not be reflected outside the function.
    It should make a copy and work with the copy.

    This isn't happening, the array isn't been passed as value. This is either poorly documented for arrays, or is a bug based on the description of the IN passmode.

    I don't think she was asking for a work-around, she was asking for clarification as to whether this is a bug.

    I'd have to say this is a bug.



    ------------------
    Let go Luke!

  6. #6
    Junior Member
    Join Date
    Jul 2002
    Posts
    5
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    Thanks, Shane, this is the point I was (unsuccessfully) trying to make. I don't need work-arounds for this problem, since I have already figured one out. And I know how pass modes work because I am a C++ coder. I wanted to alert the community about a Silk bug I think I discovered, and to see whether someone knew more about this than I, say, by pointing out newer documentation.

    Ah well...

    ------------------
    a software developer interested in quality software
    a software developer interested in quality software

  7. #7
    Senior Member
    Join Date
    Oct 2001
    Location
    NC, United States
    Posts
    148
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    wow thats a neat catch....

    by the way ..whats the work around?(if other than assigning to another variable)

    ------------------
    -gram
    -gram

  8. #8
    Senior Member
    Join Date
    Feb 2000
    Posts
    1,497
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    I don't see this one as a bug at all. The fact that the pass mode to foo is "in" doesn't mean that the mode will be automatically applied to the subsequent call to ArraySort() which only sees the name of the array.

    ArraySort documentation states that the array will be modified which makes sense because there is no return value.

    John


    ------------------

  9. #9
    Member
    Join Date
    Oct 2002
    Location
    Alameda, CA
    Posts
    50
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    <BLOCKQUOTE><font size="1" face="Verdana, Arial, Helvetica">quote:</font><HR>Originally posted by John J. Miller:
    I don't see this one as a bug at all. The fact that the pass mode to foo is "in" doesn't mean that the mode will be automatically applied to the subsequent call to ArraySort() which only sees the name of the array.

    ArraySort documentation states that the array will be modified which makes sense because there is no return value.
    <HR></BLOCKQUOTE>
    Passing an argument as 'in' should cause the array that foo has (and subsequently passes to ArraySort) to be a different copy of the array from the one the caller of foo refers to.

    This is not the behavior that is seen, as I read it.

    In addition to violating the way the documentation describes the behavior, it violates the way 'in' works in C++, which, to me, would make it a bug even if the Silk documentation didn't exist.

    I'm curious to hear more about how this could not be a bug.


    ------------------
    -Josh
    -Josh

  10. #10
    Senior Member
    Join Date
    Nov 2001
    Location
    UK, Oxford
    Posts
    150
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: pass mode of IN doesn\'t seem to work as expected.

    I would say it is definitely a bug:
    Consider this;
    if you assign a value to one of the array elements before you do the sort, then there are no side effects:
    aiMyArray[1] = 2002
    ArraySort( aiMyArray )

    if you comment out the assignment, suddenly the side effect is evidenced again.
    Perhaps this means that the programmers only create a local copy of the array when the first attempt to alter the array takes place, otherwise they don't bother (to save the expense of creating a local copy) and you are dealing with a reference to it.
    Perhaps they simply didn't consider sorting an array as altering it...?
    A bug I would say, although one with a workaround. Simply assign the first element of the array to itself before sorting.
    aiMyArray[1] = aiMyArray[1]
    ArraySort( aiMyArray )


    ------------------


    [This message has been edited by johntest (edited 10-30-2002).]
    Tempus fugit, Carpe Diem, et al

 

 
Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

vBulletin Optimisation provided by vB Optimise v2.6.0 Beta 4 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
User Alert System provided by Advanced User Tagging v3.0.9 (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Questions / Answers Form provided by vBAnswers (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
vBNominatevBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Feedback Buttons provided by Advanced Post Thanks / Like (Pro) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
Username Changing provided by Username Change (Free) - vBulletin Mods & Addons Copyright © 2016 DragonByte Technologies Ltd.
BetaSoft Inc.
Digital Point modules: Sphinx-based search
All times are GMT -8. The time now is 12:40 PM.

Copyright BetaSoft Inc.