Missing implicit array instantiation for non native objects [4.8.2]

Bugs that have been investigated and resolved somehow.

Moderator: GZDoom Developers

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 Reply
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

Missing implicit array instantiation for non native objects [4.8.2]

Post by Apeirogon »

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 19 times
Start a new game and type into console 'netevent bug' for a first version and 'netevent nobug' for a second.
Accensus
Posts: 2383
Joined: Thu Feb 11, 2016 9:59 am

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

Post by Accensus »

This seems like a bad idea. If I wanted implicit initialization, I'd use a struct instead.
User avatar
phantombeta
Posts: 2090
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

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

Post by phantombeta »

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.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

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

Post by Apeirogon »

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.
User avatar
phantombeta
Posts: 2090
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

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

Post by phantombeta »

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];.
User avatar
Apeirogon
Posts: 1605
Joined: Mon Jun 12, 2017 12:57 am

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

Post by Apeirogon »

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.
User avatar
phantombeta
Posts: 2090
Joined: Thu May 02, 2013 1:27 am
Operating System Version (Optional): Windows 10
Graphics Processor: nVidia with Vulkan support
Location: Brazil

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

Post by phantombeta »

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.
Post Reply

Return to “Closed Bugs [GZDoom]”