Missing implicit array instantiation for non native objects [4.8.2]

Forum rules
Please don't bump threads here if you have a problem - it will often be forgotten about if you do. Instead, make a new thread here.

Post a reply

Smilies
:D :) :( :o :shock: :? 8-) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :geek: :ugeek: :!: :?: :idea: :arrow: :| :mrgreen: :3: :wub: >:( :blergh:
View more smilies

BBCode is OFF
Smilies are ON

Topic review
   

Expand view Topic review: Missing implicit array instantiation for non native objects [4.8.2]

Re: Missing implicit array instantiation for non native objects [4.8.2]

by phantombeta » Sun Sep 11, 2022 5:34 am

Apeirogon wrote: Sun Sep 11, 2022 5:30 am Ah yes, everything is a pointer in zscript...
Why it works with zscript arrays than?! array<int> list[3] supports list[0].push(1) without crashing.
Because not everything is a pointer in ZScript. It's just classes that are reference types, much like C# and Java. Ints, floats, strings, vectors, structs and dynamic arrays are all value types.

Re: Missing implicit array instantiation for non native objects [4.8.2]

by Apeirogon » Sun Sep 11, 2022 5:30 am

Ah yes, everything is a pointer in zscript...
Why it works with zscript arrays than?! array<int> list[3] supports list[0].push(1) without crashing.

Re: Missing implicit array instantiation for non native objects [4.8.2]

by phantombeta » Sun Sep 11, 2022 5:22 am

These aren't comparable. Classes in ZScript don't work the same way as in C++, they're always references, never values.
test whatever_stack[10]; is an array of class instances on the stack, test * whatever_heap = new test[10]; is an array of class instances on the heap. Neither is equivalent to the ZScript example in the OP, which is an array of pointers to class instances. The C++ version of vector_wrapper vec [3]; in your example would be vector_wrapper *vec [3];.

Re: Missing implicit array instantiation for non native objects [4.8.2]

by Apeirogon » Sun Sep 11, 2022 5:14 am

Unless we talking about different things, c++ do this.
https://godbolt.org/z/Kq4Y33qfM
Yes there are hidden obligatory constructor calls, but point is, its not like in zscript static array are just a malloc call and thats it.

Re: Missing implicit array instantiation for non native objects [4.8.2]

by phantombeta » Sun Sep 11, 2022 4:39 am

This is working exactly as it should. The compiler can't and shouldn't automatically create class instances for you, no language I'm aware of does that.

Re: Missing implicit array instantiation for non native objects [4.8.2]

by Accensus » Sun Sep 11, 2022 4:11 am

This seems like a bad idea. If I wanted implicit initialization, I'd use a struct instead.

Missing implicit array instantiation for non native objects [4.8.2]

by Apeirogon » Sun Sep 11, 2022 3:18 am

Probably more 'missing feature' than 'bug'.

C like arrays (type name[number]) are missing implicit instantiation of an objects if its type is a user defined object.
Next code

Code: Select all

class debug_build : eventhandler
{
    vector_wrapper vec[3];

    override void NetworkProcess (ConsoleEvent e) 
    {
        if(e.name == "bug")
        {
            vec[2].a.push(13);
            console.printf(""..vec[2].a[0]);
        }
    }
}

class vector_wrapper
{
    array<int> a;
}
would produces 'address zero' vm crash on line with push directive.

However this code

Code: Select all

class debug_build : eventhandler
{
    array<int> vec[3];

    override void NetworkProcess (ConsoleEvent e) 
    {
        if(e.name == "nobug")
        {
            vec[2].push(13);
            console.printf(""..vec[2][0]);
        }
    }
}
works fine and print 13 into console.

Test example
c_like_array_bug.zip
(837 Bytes) Downloaded 25 times
Start a new game and type into console 'netevent bug' for a first version and 'netevent nobug' for a second.

Top