fte-team / fteqw

This is the official GitHub mirror for the FTEQW project.
https://www.fteqw.org/
179 stars 55 forks source link

Structure initialization generates garbage values #131

Open Xylemon opened 1 year ago

Xylemon commented 1 year ago

https://sourceforge.net/p/fteqw/tickets/85/

paril101 wrote on 2020-07-07:

This is compounded by nesting structures, but attempting to zero-fill with "v = (type) { }" generates garbage.

Here is an MVP:

struct nest_2
{
    int eger;
    float   ing;
    vector  pos;
};

struct nest_1
{
    nest_2  nested;
    int x;
    float   y;
};

struct nest_0
{
    nest_1  nested;
    float   x;
    int y;
};

void() test =
{
    nest_0 state;

    state = (nest_0) { };
    state.nested = (nest_1) { };
    state.nested.nested = (nest_2) { };
};

The output is seemingly nonsensical:

CHjMgmg

For some reason it attempts to initialize the vector first; is this a result of the way the structure is laid out? I expected it to be C-like, where state.nested.nested.eger would be offset 0, but there definitely seems to be something up here. Generating the clears by hand generates better code, and is what I expected to come out of this operation:

yf6h8g5

Xylemon commented 1 year ago

@eukara and @Shpoike you both should probably take a look at this.