pointers in Monkey C - bug or by design?

this code

function f(arr)
    {
        arr[0]=11;
        arr[1]=12;
        arr[2]=13;
    }

    function test()
    {
        var x = [1,2,3];
        SYS.println(x);
        f(x);
        SYS.println(x);

    }

produces in console:
[1, 2, 3]
[11, 12, 13]

It means that f  modified external var x. Is it by design or bug?

sdk 4.1b, 4.0.6, 4.0.5...
eclipse CIQ plug in: 4.1.0.beta1
eclipse ver: 2021-09 (4.21.0) Build id: 20210910-1417
windows 10
minSdkVersion 2.4.0
java jre1.8.0_311

  • So, it's pass by reference.

    Seems like too basic and old behavior to assume it's a bug.

    One reason this makes sense is that it uses less memory.

  • The reason doesn't matter.

    If it is by design, will it stay forever in the future?

    It could be used for a lot of difficult problems in Monkey C and of course to save memory.

  • The behavior can't change without breaking everything. So, yeah, it will stay forever.

  • Actually, f() didn't modify x. It modified the contents of what x points to. If you had code in f() such as "arr = [4, 5, 6]", then the contents of x in test() would be unchanged.

    This is pretty standard behavior for many languages, like Python and Javascript, where primitives are passed by value and objects/arrays are passed by reference (so to speak).

  • IOW, x points to the same array before and after f(x). That's what's meant by "x didn't change".

    But the contents of the array that x points to have changed.

    Ofc, this behavior also points to the fact that arrays and objects are mutable in Monkey C.

  • Of course  I know how it works, but I've asked about such solution and everybody answered "it's impossible".

    How can in forum "mark somebody" ( in reply to, Travis.ConnectIQ)?

    Question is: is it the bug or by design and it won't change?

  • ???

    It's not clear what you are asking.

    The pass-by-reference for arrays is by design (obviously) and not a bug.

    It can't change (everything would break).

    If you want a copy of the array, make one.

  • How can in forum "mark somebody" ( in reply to, Travis.ConnectIQ)?

    Do you mean you want to quote someone from another thread? Go to that thread, select the text, select "Quote", then copy and paste the quoted text from the reply and copy it here. (You can cancel the original reply).

    Of course  I know how it works, but I've asked about such solution and everybody answered "it's impossible".

    I'm not sure what solution you're referring to?

    Do you have a link to a thread or comment? I'm super curious why you think this is a bug.

    The pass-by-reference for arrays is by design (obviously) and not a bug.

    It can't change (everything would break).

  • Everything in monkey c is "by value" so I've thought system makes copy of array (including contents).

    I can't find this thread... I asked about such problem:

    var arr_values_of_int[....];

    class x

    {

    var arr_including_int_from_arr_values[...];

    }

    class y

    {

    var arrays_of_x; //each x can have different combination in arr_including_int_from_arr_values but only from arr_values_of_int

    }

    now I want to change something in arr_values and this changes should be automaticity set in all  arr_including_int_from_arr_values.

    for example

    arr_values_of_int = [1,2,3];

    var x1, arr_including_int_from_arr_values=[1,3]

    var x2, arr_including_int_from_arr_values=[3,2]

    now I want to change arr_values_of_int[2] = 13 and after this simple operation should have:

    x1.arr_including_int_from_arr_values=[1,13]

    x2.arr_including_int_from_arr_values=[13,2]

  • Everything in monkey c is "by value" so I've thought system makes copy of array (including contents).

    Yeah, it's semantics. It depends on what you mean when you say "pass by value" and what the context of "reference" is. ("pass by reference" and "reference type" do not mean the same thing).

    In your example, x holds a reference to an array. That reference is "passed by value". You can't make test()'s x refer to another array in f(), but you can change the *contents* of the array that x refers to.

    Because of ambiguous language, it's equally valid to say:

    * In Monkey C, everything is passed by value.

    and

    * In Monkey C, primitives are passed by value and object references are also passed by value. (We should distinguish between "reference types" and "pass by reference").

    A good way of looking at this is C. Everything in C is "pass-by-value". But usually when you pass arrays and objects to a function, you are passing in a pointer. (So the pointer is still passed by value). In fact it's impossible in C to pass arrays by copy (although it is possible to pass structs by copy).

    In C++ and C# when you pass arguments by reference it means that the function can modify the value of the input arg, and has no bearing on whether the input arg is a pointer, reference, or primitive value.

    I'm not sure if I understand your example/question. Each of your arrays contains a bunch of primitives. Changing a primitive value in one array won't change a value in an unrelated array.

    Could you provide a full code fragment? Do you have an example of a language where it would work the way you expect?

    As a counter example:

    var x = [1, 2, 3];

    var y = x; // y and x point to the same array

    var z = [1, 2, 3];

    y[0] = 4; // y and x both point to an array with contents [4,2,3]

    // nothing happened to z (obviously)